Download as pdf or txt
Download as pdf or txt
You are on page 1of 60

6/9/2023

Insertion Sort

Sorting Algorithms

2 Sorting Algorithms

1
6/9/2023

Insertion Sort Insertion Sort


Function : INSERTION-SORT (A)
 Example 1

12 11 13 5 6

 Example 2

4 3 2 10 12 1 5 6

3 Sorting Algorithms 4 Sorting Algorithms

2
6/9/2023

Insertion Sort Merge Sort


 This algorithm is one of the simplest algorithms with a  Divide the array into two halves
simple implementation  Sort each half
 Basically, Insertion sort is efficient for small data values  Merge the sorted halves back together.
 Insertion sort is adaptive in nature, i.e. it is appropriate  Repeat the process until the entire array is sorted.
for data sets that are already partially sorted.

5 Sorting Algorithms 6 Sorting Algorithms

3
6/9/2023

Merge Sort Merge Sort


Merge(low,mid,high){ If(h>mid)
MergeSort(low,high) { for k=j to high
h=low; i=low; j=mid+1;
{ { b[i]=a[k]
while ( (h<=mid) and (j<=high) )
i=i+1
if(low<high) { if(a[h]<=a[j]) }
{ b[i]=a[h] }
{ else
h=h+1
mid=(low+high)/2 }
{ for k=h to mid
{ b[i]=a[k]
MergeSort(low,mid) else i=i+1
MergeSort(mid+1,high) { b[i]=a[j] }
}
j=j+1
Merge(low,mid,high) for k=low to high
}
} i=i+1
a[k]=b[k]
} }//End of Merge
}//End of while

7 Sorting Algorithms 8 Sorting Algorithms

4
6/9/2023

Merge Sort (Example) Merge Sort (Example)


98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

9 Sorting Algorithms 10 Sorting Algorithms

5
6/9/2023

Merge Sort (Example) Merge Sort (Example)


98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42

98 23 45 14 98 23 45 14

98 23

11 Sorting Algorithms 12 Sorting Algorithms

6
6/9/2023

Merge Sort (Example) Merge Sort (Example)


98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42

98 23 45 14 98 23 45 14

98 23 98 23

23

Merge Merge

13 Sorting Algorithms 14 Sorting Algorithms

7
6/9/2023

Merge Sort (Example) Merge Sort (Example)


98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42

98 23 45 14 98 23 45 14

98 23 98 23 45 14

23 98 23 98

Merge

15 Sorting Algorithms 16 Sorting Algorithms

8
6/9/2023

Merge Sort (Example) Merge Sort (Example)


98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42

98 23 45 14 98 23 45 14

98 23 45 14 98 23 45 14

23 98 23 98 14

Merge Merge

17 Sorting Algorithms 18 Sorting Algorithms

9
6/9/2023

Merge Sort (Example) Merge Sort (Example)


98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42

98 23 45 14 98 23 45 14

98 23 45 14 98 23 45 14

23 98 14 45 23 98 14 45

Merge

Merge
19 Sorting Algorithms 20 Sorting Algorithms

10
6/9/2023

Merge Sort (Example) Merge Sort (Example)


98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42

98 23 45 14 98 23 45 14

98 23 45 14 98 23 45 14

23 98 14 45 23 98 14 45

14 14 23

Merge Merge
21 Sorting Algorithms 22 Sorting Algorithms

11
6/9/2023

Merge Sort (Example) Merge Sort (Example)


98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42

98 23 45 14 98 23 45 14

98 23 45 14 98 23 45 14

23 98 14 45 23 98 14 45

14 23 45 14 23 45 98

Merge Merge
23 Sorting Algorithms 24 Sorting Algorithms

12
6/9/2023

Merge Sort (Example) Merge Sort (Example)


98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42

98 23 45 14 98 23 45 14 6 67

23 98 14 45 23 98 14 45

14 23 45 98 14 23 45 98

25 Sorting Algorithms 26 Sorting Algorithms

13
6/9/2023

Merge Sort (Example) Merge Sort (Example)


98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42

98 23 45 14 6 67 98 23 45 14 6 67

23 98 14 45 23 98 14 45 6

14 23 45 98 14 23 45 98
Merge Merge

27 Sorting Algorithms 28 Sorting Algorithms

14
6/9/2023

Merge Sort (Example) Merge Sort (Example)


98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42

98 23 45 14 6 67 98 23 45 14 6 67 33 42

23 98 14 45 6 67 23 98 14 45 6 67

14 23 45 98 14 23 45 98
Merge

29 Sorting Algorithms 30 Sorting Algorithms

15
6/9/2023

Merge Sort (Example) Merge Sort (Example)


98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42

23 98 14 45 6 67 23 98 14 45 6 67 33

14 23 45 98 14 23 45 98
Merge Merge

31 Sorting Algorithms 32 Sorting Algorithms

16
6/9/2023

Merge Sort (Example) Merge Sort (Example)


98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42 23 98 14 45 6 67 33 42

14 23 45 98 14 23 45 98
Merge

Merge
33 Sorting Algorithms 34 Sorting Algorithms

17
6/9/2023

Merge Sort (Example) Merge Sort (Example)


98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42 23 98 14 45 6 67 33 42

14 23 45 98 6 14 23 45 98 6 33

Merge Merge
35 Sorting Algorithms 36 Sorting Algorithms

18
6/9/2023

Merge Sort (Example) Merge Sort (Example)


98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42 23 98 14 45 6 67 33 42

14 23 45 98 6 33 42 14 23 45 98 6 33 42 67

Merge Merge
37 Sorting Algorithms 38 Sorting Algorithms

19
6/9/2023

Merge Sort (Example) Merge Sort (Example)


98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42 23 98 14 45 6 67 33 42

14 23 45 98 6 33 42 67 14 23 45 98 6 33 42 67

39 Merge 40 Merge

20
6/9/2023

Merge Sort (Example) Merge Sort (Example)


98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42 23 98 14 45 6 67 33 42

14 23 45 98 6 33 42 67 14 23 45 98 6 33 42 67

6 14 6 14 23

41 Merge 42 Merge

21
6/9/2023

Merge Sort (Example) Merge Sort (Example)


98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42 23 98 14 45 6 67 33 42

14 23 45 98 6 33 42 67 14 23 45 98 6 33 42 67

6 14 23 33 6 14 23 33 42

43 Merge 44 Merge

22
6/9/2023

Merge Sort (Example) Merge Sort (Example)


98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42 23 98 14 45 6 67 33 42

14 23 45 98 6 33 42 67 14 23 45 98 6 33 42 67

6 14 23 33 42 45 6 14 23 33 42 45 67

45 Merge 46 Merge

23
6/9/2023

Merge Sort (Example) Merge Sort


98 23 45 14 6 67 33 42  Well-suited for sorting large datasets due to its
guaranteed worst-case time complexity of O(n log n).
98 23 45 14 6 67 33 42
 Well-suited where the data to be sorted is too large to
98 23 45 14 6 67 33 42
fit into memory.
98 23 45 14 6 67 33 42  Can be adapted to handle different input distributions,
23 98 14 45 6 67 33 42 such as partially sorted, nearly sorted, or completely
unsorted data.
14 23 45 98 6 33 42 67
 Parrallelizable
6 14 23 33 42 45 67 98
 Space Complexity ?
47 Merge 48 Sorting Algorithms

24
6/9/2023

Radix sort Radix sort


 Radix sort is a non-comparative integer sorting algorithm
that sorts data with integer keys by grouping keys by the
individual digits which share the same significant position
and value.

49 Sorting Algorithms 50 Sorting Algorithms

25
6/9/2023

Radix sort Radix sort


 Radix sort is a non-comparative integer sorting algorithm Least significant digit (LSD)
that sorts data with integer keys by grouping keys by the
individual digits which share the same significant position Definition: Each key is first figuratively dropped into one
and value. level of buckets corresponding to the value of the rightmost
 There are two classification of Radix sort digit. Each bucket preserves the original order of the keys as
1. Least significant digit (LSD) the keys are dropped into the bucket. There is a one-to-one
2. Most significant digit (MSD) correspondence between the buckets and the values that can
be represented by the rightmost digit. Then, the process
repeats with the next neighboring more significant digit until
there are no more digits to process.

51 Sorting Algorithms 52 Sorting Algorithms

26
6/9/2023

Radix sort Radix sort


In other words Example
Original, unsorted list:
1. Take the least significant digit of each key 170, 45, 75, 90, 802, 2, 24, 66
2. Group the keys based on that digit, but otherwise keep the
original order of keys.
3. Repeat the grouping process with each more significant
digit.

53 Sorting Algorithms 54 Sorting Algorithms

27
6/9/2023

Radix sort Radix sort


Example Example
Original, unsorted list: Original, unsorted list:
170, 45, 75, 90, 802, 2, 24, 66 170, 45, 75, 90, 802, 2, 24, 66

Sorting by least significant digit (1s place) gives: Sorting by least significant digit (1s place) gives:
170, 90, 802, 2, 24, 45, 75, 66 170, 90, 802, 2, 24, 45, 75, 66

Notice that we keep 802 before 2, because 802 occurred before 2 in the
original list, and similarly for pairs 170 & 90 and 45 & 75.

55 Sorting Algorithms 56 Sorting Algorithms

28
6/9/2023

Radix sort Radix sort


Example Example
Original, unsorted list: Original, unsorted list:
170, 45, 75, 90, 802, 2, 24, 66 170, 45, 75, 90, 802, 2, 24, 66

Sorting by least significant digit (1s place) gives: Sorting by least significant digit (1s place) gives:
170, 90, 802, 2, 24, 45, 75, 66 170, 90, 802, 2, 24, 45, 75, 66

Sorting by next digit (10s place) gives: Sorting by next digit (10s place) gives:
802, 2, 24, 45, 66, 170, 75, 90 802, 2, 24, 45, 66, 170, 75, 90

Notice that 802 again comes before 2 as 802 comes before 2 in the
previous list.

57 Sorting Algorithms 58 Sorting Algorithms

29
6/9/2023

Radix sort Radix sort


Example Iterative version using queues
Original, unsorted list: A simple version of an LSD radix sort can be achieved using queues as
170, 45, 75, 90, 802, 2, 24, 66 buckets.

Sorting by least significant digit (1s place) gives:


170, 90, 802, 2, 24, 45, 75, 66

Sorting by next digit (10s place) gives:


802, 2, 24, 45, 66, 170, 75, 90

Sorting by most significant digit (100s place) gives:


2, 24, 45, 66, 75, 90, 170, 802

59 Sorting Algorithms 60 Sorting Algorithms

30
6/9/2023

Radix sort Radix sort


Iterative version using queues Iterative version using queues
1. The integers are enqueued into an array of ten separate queues 2. The queues are dequeued back into an array of integers, in increasing
based on their digits from right to left. Computers often represent order. Using the same numbers, the array will look like this after the
integers internally as fixed-length binary digits. Here, we will do first pass:
something analogous with fixed-length decimal digits. So, using the 170, 090, 802, 002, 024, 045, 075, 066
numbers from the previous example, the queues for the 1st pass
would be:
0: 170, 090
1: none
2: 802, 002
3: none
4: 024
5: 045, 075
6: 066
7–9: none

61 Sorting Algorithms 62 Sorting Algorithms

31
6/9/2023

Radix sort Radix sort


Iterative version using queues Iterative version using queues
2. The queues are dequeued back into an array of integers, in increasing 4. For the third pass:
order. Using the same numbers, the array will look like this after the Queues:
first pass: 0: 002, 024, 045, 066, 075, 090
170, 090, 802, 002, 024, 045, 075, 066 1: 170
2–7: none
3. For the second pass: 8: 802
Queues: 9: none
0: 802, 002
1: none Array: Array:
2: 024 802, 002, 024, 045, 066, 170, 075, 090 002, 024, 045, 066, 075, 090, 170, 802 (sorted)
3: none (note that at this point only 802 and 170
4: 045 are out of order)
5: none
6: 066
7: 170, 075
63
8: none
Sorting Algorithms 64 Sorting Algorithms
9: 090

32
6/9/2023

Radix sort Radix sort


Iterative version using queues General algorithm for Radix sort
4. For the third pass:
Queues:
1. Repeat thru step 6 for each digit in the key
0: 002, 024, 045, 066, 075, 090 2. Initialize the pockets
1: 170
2–7: none 3. Repeat thru step 5 until the end of the linked list
8: 802
9: none 4. Obtain the next digit of the key
5. Insert the record in the appropriate pocket
Array:
002, 024, 045, 066, 075, 090, 170, 802 (sorted) 6. Combine the pockets to form a new linked list

65 Sorting Algorithms 66 Sorting Algorithms

33
6/9/2023

Radix sort Radix sort


Input : 42, 23, 74, 11, 65, 57, 94, 36, 99, 87, 70, 81, 61 Input : 42, 23, 74, 11, 65, 57, 94, 36, 99, 87, 70, 81, 61

67 Sorting Algorithms 68 Sorting Algorithms

34
6/9/2023

Quick Sort Example


Given an array of n elements (e.g., integers):
If array only contains one element, return We are given array of n integers to sort:
Else
pick one element to use as pivot. 40 20 10 80 60 50 7 30 100

Partition elements into two sub-arrays:


Elements less than or equal to pivot
Elements greater than pivot
Quicksort two sub-arrays
Return results

35
6/9/2023

Pick Pivot Element Partitioning Array

There are a number of ways to pick the pivot element. In this example, we will use the
first element in the array: Given a pivot, partition the elements of the array such that
the resulting array consists of:
40 20 10 80 60 50 7 30 100 1. One sub-array that contains elements >= pivot
2. 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.

36
6/9/2023

1. While data[low_index] <= data[pivot]


++low_index

pivot_index = 0 40 20 10 80 60 50 7 30 100

[0] [1] [2] [3] [4] [5] [6] [7] [8] pivot_index = 0 40 20 10 80 60 50 7 30 100

[0] [1] [2] [3] [4] [5] [6] [7] [8]


low_index high_index
low_index high_index

37
6/9/2023

1. While data[low_index] <= data[pivot] 1. While data[low_index] <= data[pivot]


++low_index ++low_index

pivot_index = 0 40 20 10 80 60 50 7 30 100
pivot_index = 0 40 20 10 80 60 50 7 30 100
[0] [1] [2] [3] [4] [5] [6] [7] [8]
[0] [1] [2] [3] [4] [5] [6] [7] [8]
low_index high_index
low_index high_index

38
6/9/2023

1. While data[low_index] <= data[pivot] 1. While data[low_index] <= data[pivot]


++low_index ++low_index
2. While data[high_index] > data[pivot] 2. While data[high_index] > data[pivot]
--high_index --high_index

pivot_index = 0 40 20 10 80 60 50 7 30 100
pivot_index = 0 40 20 10 80 60 50 7 30 100
[0] [1] [2] [3] [4] [5] [6] [7] [8]
[0] [1] [2] [3] [4] [5] [6] [7] [8]

low_index high_index
low_index high_index

39
6/9/2023

1. While data[low_index] <= data[pivot] 1. While data[low_index] <= data[pivot]


++low_index ++low_index
2. While data[high_index] > data[pivot] 2. While data[high_index] > data[pivot]
--high_index --high_index
3. If low_index < high_index 3. If low_index < high_index
swap data[low_index] and data[high_index] swap data[low_index] and data[high_index]

pivot_index = 0 40 20 10 80 60 50 7 30 100 pivot_index = 0 40 20 10 30 60 50 7 80 100

[0] [1] [2] [3] [4] [5] [6] [7] [8] [0] [1] [2] [3] [4] [5] [6] [7] [8]

low_index high_index low_index high_index

40
6/9/2023

1. While data[low_index] <= data[pivot] 1. While data[low_index] <= data[pivot]


++low_index ++low_index
2. While data[high_index] > data[pivot] 2. While data[high_index] > data[pivot]
--high_index --high_index
3. If low_index < high_index 3. If low_index < high_index
swap data[low_index] and data[high_index] swap data[low_index] and data[high_index]
4. While high_index > low_index, go to 1. 4. While high_index > low_index, go to 1.

pivot_index = 0 40 20 10 30 60 50 7 80 100 pivot_index = 0 40 20 10 30 60 50 7 80 100

[0] [1] [2] [3] [4] [5] [6] [7] [8] [0] [1] [2] [3] [4] [5] [6] [7] [8]

low_index high_index low_index high_index

41
6/9/2023

1. While data[low_index] <= data[pivot] 1. While data[low_index] <= data[pivot]


++low_index ++low_index
2. While data[high_index] > data[pivot] 2. While data[high_index] > data[pivot]
--high_index --high_index
3. If low_index < high_index 3. If low_index < high_index
swap data[low_index] and data[high_index] swap data[low_index] and data[high_index]
4. While high_index > low_index, go to 1. 4. While high_index > low_index, go to 1.

pivot_index = 0 40 20 10 30 60 50 7 80 100 pivot_index = 0 40 20 10 30 60 50 7 80 100

[0] [1] [2] [3] [4] [5] [6] [7] [8] [0] [1] [2] [3] [4] [5] [6] [7] [8]

low_index high_index low_index high_index

42
6/9/2023

1. While data[low_index] <= data[pivot] 1. While data[low_index] <= data[pivot]


++low_index ++low_index
2. While data[high_index] > data[pivot] 2. While data[high_index] > data[pivot]
--high_index --high_index
3. If low_index < high_index 3. If low_index < high_index
swap data[low_index] and data[high_index] swap data[low_index] and data[high_index]
4. While high_index > low_index, go to 1. 4. While high_index > low_index, go to 1.

pivot_index = 0 40 20 10 30 60 50 7 80 100 pivot_index = 0 40 20 10 30 60 50 7 80 100

[0] [1] [2] [3] [4] [5] [6] [7] [8] [0] [1] [2] [3] [4] [5] [6] [7] [8]

low_index high_index low_index high_index

43
6/9/2023

1. While data[low_index] <= data[pivot] 1. While data[low_index] <= data[pivot]


++low_index ++low_index
2. While data[high_index] > data[pivot] 2. While data[high_index] > data[pivot]
--high_index --high_index
3. If low_index < high_index 3. If low_index < high_index
swap data[low_index] and data[high_index] swap data[low_index] and data[high_index]
4. While high_index > low_index, go to 1. 4. While high_index > low_index, go to 1.

pivot_index = 0 40 20 10 30 7 50 60 80 100 pivot_index = 0 40 20 10 30 7 50 60 80 100

[0] [1] [2] [3] [4] [5] [6] [7] [8] [0] [1] [2] [3] [4] [5] [6] [7] [8]

low_index high_index low_index high_index

44
6/9/2023

1. While data[low_index] <= data[pivot] 1. While data[low_index] <= data[pivot]


++low_index ++low_index
2. While data[high_index] > data[pivot] 2. While data[high_index] > data[pivot]
--high_index --high_index
3. If low_index < high_index 3. If low_index < high_index
swap data[low_index] and data[high_index] swap data[low_index] and data[high_index]
4. While high_index > low_index, go to 1. 4. While high_index > low_index, go to 1.

pivot_index = 0 40 20 10 30 7 50 60 80 100 pivot_index = 0 40 20 10 30 7 50 60 80 100

[0] [1] [2] [3] [4] [5] [6] [7] [8] [0] [1] [2] [3] [4] [5] [6] [7] [8]

low_index high_index low_index high_index

45
6/9/2023

1. While data[low_index] <= data[pivot] 1. While data[low_index] <= data[pivot]


++low_index ++low_index
2. While data[high_index] > data[pivot] 2. While data[high_index] > data[pivot]
--high_index --high_index
3. If low_index < high_index 3. If low_index < high_index
swap data[low_index] and data[high_index] swap data[low_index] and data[high_index]
4. While high_index > low_index, go to 1. 4. While high_index > low_index, go to 1.

pivot_index = 0 40 20 10 30 7 50 60 80 100 pivot_index = 0 40 20 10 30 7 50 60 80 100

[0] [1] [2] [3] [4] [5] [6] [7] [8] [0] [1] [2] [3] [4] [5] [6] [7] [8]

low_index high_index low_index high_index

46
6/9/2023

1. While data[low_index] <= data[pivot] 1. While data[low_index] <= data[pivot]


++low_index ++low_index
2. While data[high_index] > data[pivot] 2. While data[high_index] > data[pivot]
--high_index --high_index
3. If low_index < high_index 3. If low_index < high_index
swap data[low_index] and data[high_index] swap data[low_index] and data[high_index]
4. While high_index > low_index, go to 1. 4. While high_index > low_index, go to 1.

pivot_index = 0 40 20 10 30 7 50 60 80 100 pivot_index = 0 40 20 10 30 7 50 60 80 100

[0] [1] [2] [3] [4] [5] [6] [7] [8] [0] [1] [2] [3] [4] [5] [6] [7] [8]

low_index high_index low_index high_index

47
6/9/2023

1. While data[low_index] <= data[pivot] 1. While data[low_index] <= data[pivot]


++low_index ++low_index
2. While data[high_index] > data[pivot] 2. While data[high_index] > data[pivot]
--high_index --high_index
3. If low_index < high_index 3. If low_index < high_index
swap data[low_index] and data[high_index] swap data[low_index] and data[high_index]
4. While high_index > low_index, go to 1. 4. While high_index > low_index, go to 1.
5. Swap data[high_index] and data[pivot_index]

pivot_index = 0 40 20 10 30 7 50 60 80 100 pivot_index = 0 40 20 10 30 7 50 60 80 100

[0] [1] [2] [3] [4] [5] [6] [7] [8] [0] [1] [2] [3] [4] [5] [6] [7] [8]

low_index high_index low_index high_index

48
6/9/2023

Partition Result
1. While data[low_index] <= data[pivot]
++low_index
2. While data[high_index] > data[pivot]
--high_index
3. If low_index < high_index
7 20 10 30 40 50 60 80 100
swap data[low_index] and data[high_index]
4. While high_index > low_index, go to 1. [0] [1] [2] [3] [4] [5] [6] [7] [8]
5. Swap data[high_index] and data[pivot_index]

7 20 10 30 40 50 60 80 100 <= data[pivot] > data[pivot]


pivot_index = 4

[0] [1] [2] [3] [4] [5] [6] [7] [8]

low_index high_index

49
6/9/2023

Recursion: Quicksort Sub-arrays Quicksort Analysis


Assume that keys are random, uniformly distributed.
What is best case running time?

7 20 10 30 40 50 60 80 100

[0] [1] [2] [3] [4] [5] [6] [7] [8]

<= data[pivot] > data[pivot]

50
6/9/2023

Quicksort Analysis Quicksort Analysis

Assume that keys are random, uniformly Assume that keys are random, uniformly
distributed. distributed.
What is best case running time? What is best case running time?
Recursion: Recursion:
1. Partition splits array in two sub-arrays of size n/2 1. Partition splits array in two sub-arrays of size n/2
2. Quicksort each sub-array 2. Quicksort each sub-array
Depth of recursion tree?

51
6/9/2023

Quicksort Analysis Quicksort Analysis

Assume that keys are random, uniformly Assume that keys are random, uniformly
distributed. distributed.
What is best case running time? What is best case running time?
Recursion: Recursion:
1. Partition splits array in two sub-arrays of size n/2 1. Partition splits array in two sub-arrays of size n/2
2. Quicksort each sub-array 2. Quicksort each sub-array
Depth of recursion tree? O(log2n) Depth of recursion tree? O(log2n)
Number of accesses in partition?

52
6/9/2023

Quicksort Analysis Quicksort Analysis

Assume that keys are random, uniformly Assume that keys are random, uniformly
distributed. distributed.
What is best case running time? Best case running time: O(n log2n)
Recursion:
1. Partition splits array in two sub-arrays of size n/2
2. Quicksort each sub-array
Depth of recursion tree? O(log2n)
Number of accesses in partition? O(n)

53
6/9/2023

Quicksort Analysis Quicksort: Worst Case


Assume first element is chosen as pivot.
Assume that keys are random, uniformly Assume we get array that is already in order:
distributed.
Best case running time: O(n log2n)
Worst case running time?
pivot_index = 0 2 4 10 12 13 50 57 63 100

[0] [1] [2] [3] [4] [5] [6] [7] [8]

low_index high_index

54
6/9/2023

1. While data[low_index] <= data[pivot] 1. While data[low_index] <= data[pivot]


++low_index ++low_index
2. While data[high_index] > data[pivot] 2. While data[high_index] > data[pivot]
--high_index --high_index
3. If low_index < high_index 3. If low_index < high_index
swap data[low_index] and data[high_index] swap data[low_index] and data[high_index]
4. While high_index > low_index, go to 1. 4. While high_index > low_index, go to 1.
5. Swap data[high_index] and data[pivot_index] 5. Swap data[high_index] and data[pivot_index]

pivot_index = 0 2 4 10 12 13 50 57 63 100 pivot_index = 0 2 4 10 12 13 50 57 63 100

[0] [1] [2] [3] [4] [5] [6] [7] [8] [0] [1] [2] [3] [4] [5] [6] [7] [8]

low_index high_index low_index high_index

55
6/9/2023

1. While data[low_index] <= data[pivot] 1. While data[low_index] <= data[pivot]


++low_index ++low_index
2. While data[high_index] > data[pivot] 2. While data[high_index] > data[pivot]
--high_index --high_index
3. If low_index < high_index 3. If low_index < high_index
swap data[low_index] and data[high_index] swap data[low_index] and data[high_index]
4. While high_index > low_index, go to 1. 4. While high_index > low_index, go to 1.
5. Swap data[high_index] and data[pivot_index] 5. Swap data[high_index] and data[pivot_index]

pivot_index = 0 2 4 10 12 13 50 57 63 100 pivot_index = 0 2 4 10 12 13 50 57 63 100

[0] [1] [2] [3] [4] [5] [6] [7] [8] [0] [1] [2] [3] [4] [5] [6] [7] [8]

low_index high_index low_index high_index

56
6/9/2023

1. While data[low_index] <= data[pivot] 1. While data[low_index] <= data[pivot]


++low_index ++low_index
2. While data[high_index] > data[pivot] 2. While data[high_index] > data[pivot]
--high_index --high_index
3. If low_index < high_index 3. If low_index < high_index
swap data[low_index] and data[high_index] swap data[low_index] and data[high_index]
4. While high_index > low_index, go to 1. 4. While high_index > low_index, go to 1.
5. Swap data[high_index] and data[pivot_index] 5. Swap data[high_index] and data[pivot_index]

pivot_index = 0 2 4 10 12 13 50 57 63 100 pivot_index = 0 2 4 10 12 13 50 57 63 100

[0] [1] [2] [3] [4] [5] [6] [7] [8] [0] [1] [2] [3] [4] [5] [6] [7] [8]

low_index high_index low_index high_index

57
6/9/2023

Quicksort Analysis
1. While data[low_index] <= data[pivot]
++low_index
2. While data[high_index] > data[pivot] Assume that keys are random, uniformly
--high_index
3. If low_index < high_index
distributed.
swap data[low_index] and data[high_index] Best case running time: O(n log2n)
4. While high_index > low_index, go to 1. Worst case running time?
5. Swap data[high_index] and data[pivot_index]
Recursion:
1. Partition splits array in two sub-arrays:
pivot_index = 0 2 4 10 12 13 50 57 63 100 • one sub-array of size 0
• the other sub-array of size n-1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
2. Quicksort each sub-array
Depth of recursion tree?
<= data[pivot] > data[pivot]

58
6/9/2023

Quicksort Analysis Quicksort Analysis


Assume that keys are random, uniformly distributed.
Assume that keys are random, uniformly Best case running time: O(n log2n)
distributed. Worst case running time?
Recursion:
Best case running time: O(n log2n) 1. Partition splits array in two sub-arrays:
Worst case running time? • one sub-array of size 0
Recursion: • the other sub-array of size n-1
1. Partition splits array in two sub-arrays: 2. Quicksort each sub-array
• one sub-array of size 0 Depth of recursion tree? O(n)
• the other sub-array of size n-1 Number of accesses per partition?
2. Quicksort each sub-array
Depth of recursion tree? O(n)

59
6/9/2023

Quicksort Analysis Quicksort Analysis


Assume that keys are random, uniformly distributed.
Best case running time: O(n log2n) Assume that keys are random, uniformly
Worst case running time? distributed.
Recursion:
1. Partition splits array in two sub-arrays:
Best case running time: O(n log2n)
• one sub-array of size 0 Worst case running time: O(n2)!!!
• the other sub-array of size n-1
2. Quicksort each sub-array
Depth of recursion tree? O(n)
Number of accesses per partition? O(n)

60

You might also like