Download presentation
Presentation is loading. Please wait.
Published byΘεόκλεια Βάμβας Modified over 6 years ago
1
Quicksort Algorithm Given an array of n elements (e.g., integers):
If array only contains one element, return Else pick one element to use as pivot. Partition elements into two sub-arrays: Elements less than or equal to pivot Elements greater than pivot Quicksort two sub-arrays Return results
2
Quicksort Example 2
3
Quicksort void quicksort(int list[],int start,int end) {
int pivot,i,j,k, newPos; if( start < end) { k = (start+end)/2; swap (&list[start], &list[k]) pivot = list[start]; //il pivot è l'elemento centrale i = start; j = end; newPos = start; while(i <= j) { if (list[i] <= pivot){ newPos++; swap(&list[i], &list[newPos]); } i++;} list[start] = list [newPos]; list[newPos] = pivot; quicksort(list,m,j-1); quicksort(list,j+1,end); } Initial call: qsort(A, 0, n-1); 3
4
More detailed example We are given array of n integers to sort: 40 20
10 80 60 50 7 30 100
5
Pick Pivot Element There are a number of ways to pick the pivot element. In this example, we will use the first element in the array: 40 20 10 80 60 50 7 30 100 In very early versions of quicksort, the leftmost element of the partition would often be chosen as the pivot element. Unfortunately, this causes worst-case behavior on already sorted arrays, which is a rather common use-case. The problem was easily solved by choosing either a random index for the pivot, choosing the middle index of the partition or (especially for longer partitions) choosing the median of the first, middle and last element of the partition for the pivot (in this case the media between ).
6
Partitioning Array Given a pivot, partition the elements of the array such that the resulting array consists of: One sub-array that contains elements >= pivot Another sub-array that contains elements < pivot The sub-arrays are stored in the original data array. Partitioning loops through, swapping elements below/above pivot.
7
40 20 10 80 60 50 7 30 100 pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] too_big_index too_small_index
8
While data[too_big_index] <= data[pivot] ++too_big_index
40 20 10 80 60 50 7 30 100 pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] too_big_index too_small_index
9
While data[too_big_index] <= data[pivot] ++too_big_index
40 20 10 80 60 50 7 30 100 pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] too_big_index too_small_index
10
While data[too_big_index] <= data[pivot] ++too_big_index
40 20 10 80 60 50 7 30 100 pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] too_big_index too_small_index
11
While data[too_big_index] <= data[pivot] ++too_big_index
While data[too_small_index] > data[pivot] --too_small_index 40 20 10 80 60 50 7 30 100 pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] too_big_index too_small_index
12
While data[too_big_index] <= data[pivot] ++too_big_index
While data[too_small_index] > data[pivot] --too_small_index 40 20 10 80 60 50 7 30 100 pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] too_big_index too_small_index
13
While data[too_big_index] <= data[pivot] ++too_big_index
While data[too_small_index] > data[pivot] --too_small_index If too_big_index < too_small_index swap data[too_big_index] and data[too_small_index] 40 20 10 80 60 50 7 30 100 pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] too_big_index too_small_index
14
While data[too_big_index] <= data[pivot] ++too_big_index
While data[too_small_index] > data[pivot] --too_small_index If too_big_index < too_small_index swap data[too_big_index] and data[too_small_index] 40 20 10 30 60 50 7 80 100 pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] too_big_index too_small_index
15
While data[too_big_index] <= data[pivot] ++too_big_index
While data[too_small_index] > data[pivot] --too_small_index If too_big_index < too_small_index swap data[too_big_index] and data[too_small_index] While too_small_index > too_big_index, go to 1. 40 20 10 30 60 50 7 80 100 pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] too_big_index too_small_index
16
While data[too_big_index] <= data[pivot] ++too_big_index
While data[too_small_index] > data[pivot] --too_small_index If too_big_index < too_small_index swap data[too_big_index] and data[too_small_index] While too_small_index > too_big_index, go to 1. 40 20 10 30 60 50 7 80 100 pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] too_big_index too_small_index
17
While data[too_big_index] <= data[pivot] ++too_big_index
While data[too_small_index] > data[pivot] --too_small_index If too_big_index < too_small_index swap data[too_big_index] and data[too_small_index] While too_small_index > too_big_index, go to 1. 40 20 10 30 60 50 7 80 100 pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] too_big_index too_small_index
18
While data[too_big_index] <= data[pivot] ++too_big_index
While data[too_small_index] > data[pivot] --too_small_index If too_big_index < too_small_index swap data[too_big_index] and data[too_small_index] While too_small_index > too_big_index, go to 1. 40 20 10 30 60 50 7 80 100 pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] too_big_index too_small_index
19
While data[too_big_index] <= data[pivot] ++too_big_index
While data[too_small_index] > data[pivot] --too_small_index If too_big_index < too_small_index swap data[too_big_index] and data[too_small_index] While too_small_index > too_big_index, go to 1. 40 20 10 30 60 50 7 80 100 pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] too_big_index too_small_index
20
While data[too_big_index] <= data[pivot] ++too_big_index
While data[too_small_index] > data[pivot] --too_small_index If too_big_index < too_small_index swap data[too_big_index] and data[too_small_index] While too_small_index > too_big_index, go to 1. 40 20 10 30 60 50 7 80 100 pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] too_big_index too_small_index
21
While data[too_big_index] <= data[pivot] ++too_big_index
While data[too_small_index] > data[pivot] --too_small_index If too_big_index < too_small_index swap data[too_big_index] and data[too_small_index] While too_small_index > too_big_index, go to 1. 40 20 10 30 7 50 60 80 100 pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] too_big_index too_small_index
22
While data[too_big_index] <= data[pivot] ++too_big_index
While data[too_small_index] > data[pivot] --too_small_index If too_big_index < too_small_index swap data[too_big_index] and data[too_small_index] While too_small_index > too_big_index, go to 1. 40 20 10 30 7 50 60 80 100 pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] too_big_index too_small_index
23
While data[too_big_index] <= data[pivot] ++too_big_index
While data[too_small_index] > data[pivot] --too_small_index If too_big_index < too_small_index swap data[too_big_index] and data[too_small_index] While too_small_index > too_big_index, go to 1. 40 20 10 30 7 50 60 80 100 pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] too_big_index too_small_index
24
While data[too_big_index] <= data[pivot] ++too_big_index
While data[too_small_index] > data[pivot] --too_small_index If too_big_index < too_small_index swap data[too_big_index] and data[too_small_index] While too_small_index > too_big_index, go to 1. 40 20 10 30 7 50 60 80 100 pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] too_big_index too_small_index
25
While data[too_big_index] <= data[pivot] ++too_big_index
While data[too_small_index] > data[pivot] --too_small_index If too_big_index < too_small_index swap data[too_big_index] and data[too_small_index] While too_small_index > too_big_index, go to 1. 40 20 10 30 7 50 60 80 100 pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] too_big_index too_small_index
26
While data[too_big_index] <= data[pivot] ++too_big_index
While data[too_small_index] > data[pivot] --too_small_index If too_big_index < too_small_index swap data[too_big_index] and data[too_small_index] While too_small_index > too_big_index, go to 1. 40 20 10 30 7 50 60 80 100 pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] too_big_index too_small_index
27
While data[too_big_index] <= data[pivot] ++too_big_index
While data[too_small_index] > data[pivot] --too_small_index If too_big_index < too_small_index swap data[too_big_index] and data[too_small_index] While too_small_index > too_big_index, go to 1. 40 20 10 30 7 50 60 80 100 pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] too_big_index too_small_index
28
While data[too_big_index] <= data[pivot] ++too_big_index
While data[too_small_index] > data[pivot] --too_small_index If too_big_index < too_small_index swap data[too_big_index] and data[too_small_index] While too_small_index > too_big_index, go to 1. 40 20 10 30 7 50 60 80 100 pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] too_big_index too_small_index
29
While data[too_big_index] <= data[pivot] ++too_big_index
While data[too_small_index] > data[pivot] --too_small_index If too_big_index < too_small_index swap data[too_big_index] and data[too_small_index] While too_small_index > too_big_index, go to 1. 40 20 10 30 7 50 60 80 100 pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] too_big_index too_small_index
30
While data[too_big_index] <= data[pivot] ++too_big_index
While data[too_small_index] > data[pivot] --too_small_index If too_big_index < too_small_index swap data[too_big_index] and data[too_small_index] While too_small_index > too_big_index, go to 1. Swap data[too_small_index] and data[pivot_index] 40 20 10 30 7 50 60 80 100 pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] too_big_index too_small_index
31
While data[too_big_index] <= data[pivot] ++too_big_index
While data[too_small_index] > data[pivot] --too_small_index If too_big_index < too_small_index swap data[too_big_index] and data[too_small_index] While too_small_index > too_big_index, go to 1. Swap data[too_small_index] and data[pivot_index] 7 20 10 30 40 50 60 80 100 pivot_index = 4 [0] [1] [2] [3] [4] [5] [6] [7] [8] too_big_index too_small_index
32
Partition Result 7 20 10 30 40 50 60 80 100 [0] [1] [2] [3] [4] [5] [6] [7] [8] <= data[pivot] > data[pivot]
33
Recursion: Quicksort Sub-arrays
7 20 10 30 40 50 60 80 100 [0] [1] [2] [3] [4] [5] [6] [7] [8] <= data[pivot] > data[pivot]
34
Quicksort Analysis Assume that keys are random, uniformly distributed.
What is best case running time?
35
Quicksort Analysis Assume that keys are random, uniformly distributed.
What is best case running time? Recursion: Partition splits array in two sub-arrays of size n/2 Quicksort each sub-array
36
Quicksort Analysis Assume that keys are random, uniformly distributed.
What is best case running time? Recursion: Partition splits array in two sub-arrays of size n/2 Quicksort each sub-array Depth of recursion tree?
37
Quicksort Analysis Assume that keys are random, uniformly distributed.
What is best case running time? Recursion: Partition splits array in two sub-arrays of size n/2 Quicksort each sub-array Depth of recursion tree? O(log2n)
38
Quicksort Analysis Assume that keys are random, uniformly distributed.
What is best case running time? Recursion: Partition splits array in two sub-arrays of size n/2 Quicksort each sub-array Depth of recursion tree? O(log2n) Number of accesses in partition?
39
Quicksort Analysis Assume that keys are random, uniformly distributed.
What is best case running time? Recursion: Partition splits array in two sub-arrays of size n/2 Quicksort each sub-array Depth of recursion tree? O(log2n) Number of accesses in partition? O(n)
40
Quicksort Analysis Assume that keys are random, uniformly distributed.
Best case running time: O(n log2n)
41
Quicksort Analysis Assume that keys are random, uniformly distributed.
Best case running time: O(n log2n) Worst case running time?
42
Quicksort: Worst Case Assume first element is chosen as pivot.
Assume we get array that is already in order: 2 4 10 12 13 50 57 63 100 pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] too_big_index too_small_index
43
While data[too_big_index] <= data[pivot] ++too_big_index
While data[too_small_index] > data[pivot] --too_small_index If too_big_index < too_small_index swap data[too_big_index] and data[too_small_index] While too_small_index > too_big_index, go to 1. Swap data[too_small_index] and data[pivot_index] 2 4 10 12 13 50 57 63 100 pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] too_big_index too_small_index
44
While data[too_big_index] <= data[pivot] ++too_big_index
While data[too_small_index] > data[pivot] --too_small_index If too_big_index < too_small_index swap data[too_big_index] and data[too_small_index] While too_small_index > too_big_index, go to 1. Swap data[too_small_index] and data[pivot_index] 2 4 10 12 13 50 57 63 100 pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] too_big_index too_small_index
45
While data[too_big_index] <= data[pivot] ++too_big_index
While data[too_small_index] > data[pivot] --too_small_index If too_big_index < too_small_index swap data[too_big_index] and data[too_small_index] While too_small_index > too_big_index, go to 1. Swap data[too_small_index] and data[pivot_index] 2 4 10 12 13 50 57 63 100 pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] too_big_index too_small_index
46
While data[too_big_index] <= data[pivot] ++too_big_index
While data[too_small_index] > data[pivot] --too_small_index If too_big_index < too_small_index swap data[too_big_index] and data[too_small_index] While too_small_index > too_big_index, go to 1. Swap data[too_small_index] and data[pivot_index] 2 4 10 12 13 50 57 63 100 pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] too_big_index too_small_index
47
While data[too_big_index] <= data[pivot] ++too_big_index
While data[too_small_index] > data[pivot] --too_small_index If too_big_index < too_small_index swap data[too_big_index] and data[too_small_index] While too_small_index > too_big_index, go to 1. Swap data[too_small_index] and data[pivot_index] 2 4 10 12 13 50 57 63 100 pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] too_big_index too_small_index
48
While data[too_big_index] <= data[pivot] ++too_big_index
While data[too_small_index] > data[pivot] --too_small_index If too_big_index < too_small_index swap data[too_big_index] and data[too_small_index] While too_small_index > too_big_index, go to 1. Swap data[too_small_index] and data[pivot_index] 2 4 10 12 13 50 57 63 100 pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] too_big_index too_small_index
49
While data[too_big_index] <= data[pivot] ++too_big_index
While data[too_small_index] > data[pivot] --too_small_index If too_big_index < too_small_index swap data[too_big_index] and data[too_small_index] While too_small_index > too_big_index, go to 1. Swap data[too_small_index] and data[pivot_index] 2 4 10 12 13 50 57 63 100 pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] <= data[pivot] > data[pivot]
50
Quicksort Analysis Assume that keys are random, uniformly distributed.
Best case running time: O(n log2n) Worst case running time? Recursion: Partition splits array in two sub-arrays: one sub-array of size 0 the other sub-array of size n-1 Quicksort each sub-array Depth of recursion tree?
51
Quicksort Analysis Assume that keys are random, uniformly distributed.
Best case running time: O(n log2n) Worst case running time? Recursion: Partition splits array in two sub-arrays: one sub-array of size 0 the other sub-array of size n-1 Quicksort each sub-array Depth of recursion tree? O(n)
52
Quicksort Analysis Assume that keys are random, uniformly distributed.
Best case running time: O(n log2n) Worst case running time? Recursion: Partition splits array in two sub-arrays: one sub-array of size 0 the other sub-array of size n-1 Quicksort each sub-array Depth of recursion tree? O(n) Number of accesses per partition?
53
Quicksort Analysis Assume that keys are random, uniformly distributed.
Best case running time: O(n log2n) Worst case running time? Recursion: Partition splits array in two sub-arrays: one sub-array of size 0 the other sub-array of size n-1 Quicksort each sub-array Depth of recursion tree? O(n) Number of accesses per partition? O(n)
54
Quicksort Analysis Assume that keys are random, uniformly distributed.
Best case running time: O(n log2n) Worst case running time: O(n2)!!!
55
Quicksort Analysis Assume that keys are random, uniformly distributed.
Best case running time: O(n log2n) Worst case running time: O(n2)!!! What can we do to avoid worst case?
56
Improving Performance of Quicksort
Improved selection of pivot. For sub-arrays of size 3 or less, apply brute force search: Sub-array of size 1: trivial Sub-array of size 2: if(data[first] > data[second]) swap them Sub-array of size 3: if(data[second] > data[third]) swap them if(data[first] > data[third]) swap them
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.