Back to 课程

Computer-Science-A-level-Ocr

0% Complete
0/0 Steps
  1. 3-3-networks
    8 主题
  2. 3-2-databases
    7 主题
  3. 3-1-compression-encryption-and-hashing
    4 主题
  4. 2-5-object-oriented-languages
    7 主题
  5. 2-4-types-of-programming-language
    4 主题
  6. 2-3-software-development
    5 主题
  7. 2-2-applications-generation
    6 主题
  8. 2-1-systems-software
    8 主题
  9. 1-3-input-output-and-storage
    2 主题
  10. 1-2-types-of-processor
    3 主题
  11. 1-1-structure-and-function-of-the-processor
    1 主题
  12. structuring-your-responses
    3 主题
  13. the-exam-papers
    2 主题
  14. 8-2-algorithms-for-the-main-data-structures
    4 主题
  15. 8-1-algorithms
    10 主题
  16. 7-2-computational-methods
    11 主题
  17. 7-1-programming-techniques
    14 主题
  18. 6-5-thinking-concurrently
    2 主题
  19. 6-4-thinking-logically
    2 主题
  20. 6-3-thinking-procedurally
    3 主题
  21. 6-2-thinking-ahead
    1 主题
  22. 6-1-thinking-abstractly
    3 主题
  23. 5-2-moral-and-ethical-issues
    9 主题
  24. 5-1-computing-related-legislation
    4 主题
  25. 4-3-boolean-algebra
    5 主题
  26. 4-2-data-structures
    10 主题
  27. 4-1-data-types
    9 主题
  28. 3-4-web-technologies
    16 主题
课 Progress
0% Complete

Quick sort

What is a quick sort?

  • A quick sort is another example of a divide and conquer algorithm that reduces the size of the problem into smaller problems that are more easily solvable by an algorithm

  • Below is an illustration of the quick sort

quick-sort-1
quick-sort-2
quick-sort-3

Figure 2: Performing the Quick sort

Time complexity of quick sort

  • Quick sort is a divide and conquer algorithm that splits the problem into smaller subproblems using a pivot value

  • The pivot divides the list into two parts: values less than the pivot and values greater than the pivot

  • Each sub list is then sorted recursively until the list is completely ordered

Best and average case

  • If the pivot divides the list roughly in the middle, the algorithm performs efficiently

  • Each level of recursion performs n operations to partition the list

  • There are approximately log n levels of recursion

  • This gives a best and average case time complexity of O(n log n)

Worst case

  • If the pivot is consistently placed at one extreme (for example, always the smallest or largest value), one partition becomes empty

  • This causes the recursion to degenerate into a single chain of calls

  • Each recursive call processes n – 1 elements, leading to n × (n – 1) operations, or O(n²) overall

  • Poor pivot selection or already sorted input data can lead to this worst-case scenario

  • In practice, randomised or median-of-three pivot selection is used to avoid the worst case

  • If recursion depth becomes too large, a stack overflow can occur due to too many recursive calls

Space complexity of a quick sort

  • Quick sort requires additional memory as copies of lists are made and put on the stack. The overall space complexity is therefore O(n) with O(n) additional space required

Tracing a quick sort

  • The trace table below shows the process of calling the quick sort and moving the left and right pointers up and down the list to find elements to swap and then find the correct position of the pivot

  • Once the pivot is in place, quick sort is recursively called on both sublists on either side of the pivot. Note that a second call to quick sort may not exist if the pivot value ends on either extreme of the list

  • The process repeats, calling quick sort on progressively smaller sublists until all values are in the correct order

Trace Table for the Quick sort

<tr class=”

list

operation

done

pivot

list[pivot]

left

list[left]

right

list[right]

[8, 2, 6, 1, 7, 9, 1, 4]

QUICKSORT

False

0

8

1

2

7

4

 

INC LEFT

 

 

 

1

2

 

 

 

 

 

 

 

2

6

 

 

 

 

 

 

 

3

1

 

 

 

 

 

 

 

4

7

 

 

 

 

 

 

 

5

9

 

 

 

DEC RIGHT

 

 

 

 

 

7

4

[8, 2, 6, 1, 7, 4, 1, 9]

SWAP

 

 

 

5

4

7

9

 

INC LEFT

 

 

 

6

1

 

 

 

 

 

 

 

7

9

 

 

 

DEC RIGHT

 

 

 

 

 

6

1

[1, 2, 6, 1, 7, 4, 8, 9]

SWAP PIVOT

True

6

8

7

9

0

1

[1, 2, 6, 1, 7, 4]

QUICKSORT

False

0

1

1

2

5

4

 

INC LEFT

 

 

 

1

2

 

 

 

DEC RIGHT

 

 

 

 

 

5

4

Responses

您的邮箱地址不会被公开。 必填项已用 * 标注