Unit 6 SearchingSorting PartB
Unit 6 SearchingSorting PartB
DATA STRUCTURES
Sorting Techniques: Bubble Sort, Insertion Sort,
Selection Sort, Quick Sort, Merge Sort, Heap Sort,
Bucket Sort
Bubble Sort
• Sort list of n elements
• In each iteration, find the maximum elements in remaining
unsorted list and move it to end. Do this in max m-1 iteration by
comparing successive elements where m is the number of
elements remaining in unsorted list at any given stage.
begin BubbleSort(arr)
for all array elements
if arr[i] > arr[i+1]
swap(arr[i], arr[i+1])
end if
end for
return arr
end BubbleSort
Bubble Sort: Example
Bubble Sort: Example(cont’d)
Bubble Sort: Analysis
Worst case: If list is sorted in descending order, then
• Number of comparison: 1+2+3+…+n-1=n(n-1)/2
• Number of swaps: 1+2+3+…+n-1=n(n-1)/2
• Time complexity= O(n2)
7
Insertion Sort: Analysis
Worst Case: If list is sorted in descending order
• Number of comparison: 1+2+3+…+n-1 = n(n-1)/2
• Number of swaps: 1+2+3+…+n-1= n(n-1)/2
• Time complexity= O(n2)
1 2 3 4 5 6
Selection Sort: Analysis
Worst 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)
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)
Heap Sort
• Build heap using BuildHeap method for array elements
• In each iteration, delete minimum element from top and
store at last position of heap(so far unsorted array)
• In every iteration, one more element get added to sorted
half of the array
• Finally we will get array sorted in descending order
• To sort array in ascending order, use max-heap
Heap Sort: Algo
heapSort(int a[], int n) {
BuildHeap(a, n);
for(i=n; i>1; i--) {
a[i-1] = deleteMin(a,i);
}
}
Time Complexity:
• BuildHeap has time complexity: O(n)
• Deleting root element from heap and heapifying: O(log n)
• Deleting n elements therefore will take: O(n log n)
• Total time required: O(n log n)
Heap Sort: Example
5 2 4 6 1 3 BuildHeap
6 5 4 3 2 1
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 sort: Example
8 2 9 4 5 3 1 6
S 13
81 43 31 57
75
select pivot value
92 0
65 26
S1 0 31
S2 75
partition S
43 65
13 81
26 57 92
QuickSort(S1) and
S1 S2 QuickSort(S2)
0 13 26 31 43 57 65 75 81 92
S 0 13 26 31 43 57 65 75 81 92 S is sorted
Quick Sort: Best case
• Ideally, Pivot element should divide list in two parts
• Hence recurrence relation:
T(n) = 2T(n/2) + n
2
0 2
0 1 2 3 4
0 1 2 3 4
0 0 0 1 1 2 2 2 2 3 3 4 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.
58
12 37 18 9
20 52 63 64 36 47 88 99
20 12 52 63 64 36 37 47 58 18 88 9 99
Radix Sort: Example(2nd Pass)
20 12 52 63 64 36 37 47 58 18 88 9 99
12 36 52 63
9 18 20 37 47 58 64 88 99
9 12 18 20 36 37 47 52 58 63 64 88 99
Radix Sort: Example (Both Passes)
12 58 37 64 52 36 99 63 18 9 20 88 47
20 12 52 63 64 36 37 47 58 18 88 9 99
9 12 18 20 36 37 47 52 58 63 64 88 99
Radix Sort: Analysis
• If there is a fixed number p of bucket sort stages (six
stages in the case where the maximum value is 999999),
then radix sort is O( n )
• There are p bucket sort stages, each taking O( n ) time
• Strictly speaking, time complexity is O(pn), where p is the
number of digits (note that p = log10m, where m is the
maximum value in the list)
Radix Sort: For Words
• Note that only 10 buckets are needed regardless of
number of stages since the buckets are reused at each
stage
• Radix sort can apply to words
• Set a limit to the number of letters in a word
• Use 27 buckets (or more, depending on the letters/characters
allowed), one for each letter plus a “blank” character
• The word-length limit is exactly the number of bucket sort stages
needed