Computer Science AS OCR
-
1-1-structure-and-function-of-the-processor as5 主题
-
1-2-types-of-processor as3 主题
-
1-3-input-output-and-storage as2 主题
-
2-1-systems-software as8 主题
-
2-3-software-development as5 主题
-
2-4-types-of-programming-language as4 主题
-
3-1-compression-encryption-and-hashing as3 主题
-
3-2-databases as3 主题
-
3-3-networks as8 主题
-
3-4-web-technologies as13 主题
-
html as
-
css as
-
css-styling as
-
javascript as
-
variables-and-constants-in-javascript as
-
outputs-in-javascript as
-
selection-in-javascript- as
-
for-loops-in-javascript- as
-
while-loops-in-javascript- as
-
strings-in-javascript- as
-
operators-in-javascript- as
-
nested-statements-in-javascript as
-
functions-and-procedures-in-javascript as
-
html as
-
4-1-data-types as8 主题
-
4-2-data-structures as4 主题
-
4-3-boolean-algebra as1 主题
-
5-1-computing-related-legislation as4 主题
-
5-2-moral-and-ethical-issues as9 主题
-
6-1-thinking-abstractly as3 主题
-
6-2-thinking-ahead as1 主题
-
6-3-thinking-procedurally as3 主题
-
6-4-thinking-logically as2 主题
-
6-5-thinking-concurrently as2 主题
-
7-1-programming-techniques as9 主题
-
8-1-standard-algorithms-and-big-o-notation as8 主题
quick-sort as
Exam code:H046
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



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
|
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