3 - Sorting Algos
3 - Sorting Algos
DATA STRUCTURES
Sorting Techniques: Bubble Sort, Insertion Sort, Selection
Sort, Quick Sort, Merge Sort, Heap Sort, Bucket Sort
begin BubbleSort(arr)
for all array elements
if arr[i] > arr[i+1]
Bubble Sort
swap(arr[i], arr[i+1])
• Sort list of n elements
• In each iteration, find the maximum elements in remaining end
BubbleSort
end if
end for
return arr
Insertion Sort
j←j-1
i←i+1
• Single element is considered to be sorted • Add one more
element in each iteration and insert in existing sorted list.
Make space for newly picked element by moving elements
towards right.
j←i
while j > 0 and A[j-1] > A[j]
swap A[j] and A[j-1]
end while
end while
Selection Sort
position, starting from first position • In each iteration, list of
unsorted elements shrink from front
23456
1 1
1 1
Best Case:
• Number of comparison: n-1+n-2+…+2+1 = n(n-1)/2• Number
of swaps: 1+1+1+…+1(n-1 times)= n • Time complexity =
O(n2)
• Build heap using BuildHeap method for array elements
Heap Sort
Time Complexity:
Delete minimum and move at a[4] 4 5 6 2 1 Delete minimum and move at a[3] 6
54321
1
2
Merge Sort
• Divide array into two halves, recursively sort left and right halves,
then merge two halves
• Keep dividing until only one element remains
Merge
Merge
12345689
Merge Sort: Algo
mergeSort(Arr, start, end) {
if(i == j) return;
else {
mid= (start+end)/2;
mergeSort(Arr, start, mid);
mergeSort(Arr, mid+1, end);
merge(Arr, start, mid, end); // merge two sorted half }
}
Merge Algo: Analysis
• Merging sorted array of total length n will take: O(n) •
Recurrence relation for time complexity of mergeSort:
T(n)=2T(n/2) + n
Quick Sort
• Using a pivot element, divide list in two halves which are a[i] <
pivot and a[i] > pivot
• In each iteration, we find right position for pivot element picked.
• Run quick sort for two halves recursively • Pivot
elements can be picked based on multiple techniques
• Basic technique is to pick first element
31 57
Quick Sort: Example 0 13 43 26 31 57
65 75 81 92
S 0 65 81 92 13 43 26 31 57 75
S is sorted
S select pivot value1381
43
75
92 0
26
65
S2QuickSort(S1) andQuickSort(S2)
S1
Quick Sort: Best case
• Ideally, Pivot element should divide list in two parts • Hence
recurrence relation:
T(n) = 2T(n/2) + n
2
• Solving this gives: T(n) = O(n )
Comparison-based Sorting
• Several sorting algorithms have been discussed and the best
ones, so far:
• Heap sort and Merge sort: O( n log n )
2
• Quick sort (best one in practice): O( n log n ) on average, O( n ) worst
case
Counting Sort
• Idea: suppose the values are in the range 0..m-1; start with m
empty buckets numbered 0 to m-1, scan the list and increase
the count of elements in the bucket
3344
2
0 2
0
1
2
3
4
0
1
2
3
4
Counting Sort: Algo
CountingSort(S) { //(values in S are between 0 and m-1 ) for(j=0; j<m;
j++) // initialize m buckets b[j] = 0;
for(i=0; i<n; i++) // place elements in their b[S[i]] = b[S[i]] + 1; //
appropriate buckets
i=0;
for(j=0; j<m; j++) { // place elements in buckets for(r=1; r<=b[j]+1; r++)
{ // back in S S[i] =j;
i=i + 1;
}
}
}
Counting Sort: Analysis
• Bucket initialization: O( m )
• From array to buckets: O( n )
• From buckets to array: O( n )
• Since m will likely be small compared to n, Bucket
sort is O( n )
• Strictly speaking, time complexity is O ( n + m )
Sorting integers
• Can we perform counting sort on any array of
(non-negative) integers?
• Yes, but note that the number of buckets will depend on
the maximum integer value
• If you are sorting 1000 integers and the maximum value
is 999999, you will need 1 million buckets! • Time
complexity is not really O( n ) because m is much > than n. Actual
time complexity is O( m ) • Can we do better?
Bucket Sort
• Idea: Divide n elements into M buckets. Sort each bucket
independently using insertion sort. Finally sequentially access all
the elements from buckets starting from first bucket. Resultant
array is sorted array.
• Array to be sorted can have any non-negative integers and
should be uniformly distributed i.e. should not be the case that
all elements go into one bucket.
Bucket Sort: Example
Radix Sort
• Idea: repeatedly sort by digit—perform multiple bucket
sorts on S starting with the rightmost digit • If maximum
value is 999999, only ten buckets (not 1 million) will be
necessary • Use this strategy when the keys are
integers, and there is a reasonable limit on their values •
Number of passes (bucket sort stages) will depend on the
number of digits in the maximum value
12 58 37 64 52 36 99 63 18 9 20 88 47
st
Radix Sort: Example (1 Pass) 20 12 52
63 64 36 37 47 58 18 88 9 99
58
12 9
20 18
37
52 63 64 36 47
99
88
20 12 52 63 64 36 37 47 58 18 88 9 99
nd
Radix Sort: Example(2 Pass) 9 12 18
20 36 37 47 52 58 63 64 88 99912
36
52
63
18 20
37 47
58
64 88 99
12 58 37 64 52 36 99 63 18 9 20 88 4720 12 52 63 64 36 37
47 58 18 88 9 99