Sortings
Sortings
Sorting
Dr Deepak Gupta
Assistant Professor, SMIEEE
CSED, MNNIT Allahabad, Prayagraj
Email: [email protected]
Heap Sort
o Shape Property
o Order Property
o Shape Property: it mean that heap must be a complete binary tree.
o Order Property: it mean that for every node in the heap, the value stored
in that node is greater than or equal to the value in each of its children.
Types of Heap: There are two types of heap i.e. Max heap and Min heap.
HEAPIFY: Heapify is a procedure for manipulating the heap data structure
for maintaining the heap property.
Max-Heapify(A[ ], i)
1. leftchild = 2i ;
2. rightchild = 2i + 1 ;
3. if leftchild ≤ heap_size[A] and A[leftchild] > A[i]
4. largest = leftchild ;
5. else
6. largest = i ;
7. if rightchild ≤ heap_size[A] and A[rightchild] > A[largest]
8. largest = rightchild ;
9. if largest ≠ i
10. swap A[i] with A[largest] ;
11. Max-Heapify(A, largest);
Build MAX Heap: If we have to form a heap from some n elements, we can
start with an empty heap and insert the elements sequentially n times.
HEAP_SORT(int A[ ])
1. Build_Max_Heap(int A[ ], int n)
2. for( i = length[A] down to 2
do exchange A[1] A[i];
heap_size[A] = heap_size[A]-1;
MAX_Heapify(A, 1);
Heapify Illustration:
Array = {1, 3, 5, 4, 6, 13, 10, 9, 8, 15, 17}
3 5
4 6 13 10
9 8 15 17
Heapify 6: Swap 6 and 17. 1
3 5
4 17 13 10
9 8 15 6
3 5
9 17 13 10
4 8 15 6
Heapify 5: Swap 13 and 5. 1
3 13
9 17 5 10
4 8 15 6
Heapify 3: Swap 3 and 17, again swap 3 and 15.
1
17 13
9 15 5 10
4 8 3 6
Heapify 1: First Swap 1 and 17, again swap 1 and 15 and finally swap 1 and 6.
17
1 13
9 15 5 10
4 8 3 6
Heapify 1: First Swap 1 and 17, again swap 1 and 15 and finally swap 1 and 6.
17
15 13
9 1 5 10
4 8 3 6
Heapify 1: First Swap 1 and 17, again swap 1 and 15 and finally swap 1 and 6.
17
15 13
9 6 5 10
4 8 3 1
let's take an unsorted array and try to sort it using heap sort.
First, we have to construct a heap from the given array and convert it into max
heap.
After converting the given heap into max heap, the array
elements are -
After swapping the array element 89 with 11, and converting the heap into max-
heap, the elements of array are -
In the next step, again, we have to delete the root
element (81) from the max heap. To delete this node,
we have to swap it with the last node, i.e. (54). After
deleting the root element, we again have to heapify it to
convert it into max heap.
After swapping the array element 81 with 54 and converting the heap into max-
heap, the elements of array are -
In the next step, we have to delete the root element (76)
from the max heap again. To delete this node, we have
to swap it with the last node, i.e. (9). After deleting the
root element, we again have to heapify it to convert it
into max heap.
After swapping the array element 76 with 9 and converting the heap into max-
heap, the elements of array are -
In the next step, again we have to delete the root
element (54) from the max heap. To delete this node,
we have to swap it with the last node, i.e. (14). After
deleting the root element, we again have to heapify it to
convert it into max heap.
After swapping the array element 54 with 14 and converting the heap into max-
heap, the elements of array are -
In the next step, again we have to delete the root
element (22) from the max heap. To delete this node,
we have to swap it with the last node, i.e. (11). After
deleting the root element, we again have to heapify it to
convert it into max heap.
After swapping the array element 22 with 11 and converting the heap into max-
heap, the elements of array are -
In the next step, again we have to delete the root
element (14) from the max heap. To delete this node,
we have to swap it with the last node, i.e. (9). After
deleting the root element, we again have to heapify it to
convert it into max heap.
After swapping the array element 14 with 9 and converting the heap into max-
heap, the elements of array are -
In the next step, again we have to delete the root
element (11) from the max heap. To delete this node,
we have to swap it with the last node, i.e. (9). After
deleting the root element, we again have to heapify it to
convert it into max heap.
After swapping the array element 11 with 9, the elements of array are -
Now, heap has only one element left. After deleting it,
heap will be empty.
The time complexity of heap sort is O(n logn) in all three cases (best case,
average case, and worst case).
Complexity of Build a Heap
Merge sort is the sorting technique that follows the divide and conquer
approach. It works by recursively dividing the input array into smaller
subarrays and sorting those subarrays then merging them back together to
obtain the sorted array.
Divide: Divide the list or array recursively into two halves until it can no
more be divided.
Conquer: Each subarray is sorted individually using the merge sort
algorithm. i.e. Conquer by recursively sorting the two subarrays A[p .. q] and
A[q + 1 .. r].
Merge: The sorted subarrays are merged back together in sorted order. The
process continues until all elements from both subarrays have been merged.
Procedure MERGE( A, p, q, r): The procedure assumes that the sub array
A[p,…,q] and A[q+1,…,r] are in sorted order. It merges them to form a single
sorted sub array that replaces the current sub array A[p,…,r].
// Merge two subarrays L and R into A while (i < n1 && j < n2) {
void merge(int A[ ], int beg, int mid, int end) { if (L[i] <= R[j]) {
//Create L←A[beg,...,mid] and R←A[mid+1,...,end] A[k] = L[i];
int n1 = mid - beg + 1; i++;
int n2 = end - mid; } else {
A[k] = R[j];
int L[n1], R[n2]; j++;
}
for (int i = 0; i < n1; i++) k++;
L[i] = A[beg + i]; }
for (int j = 0; j < n2; j++) /* When we run out of elements in either L or R,
R[j] = A[mid + 1 + j]; pick up the remaining elements and put in
A[beg...end]*/
// Maintain current index of sub-arrays and main while (i < n1) {
array A[k] = L[i];
int i, j, k; i++;
i = 0; k++;
j = 0; }
k = beg; while (j < n2) {
A[k] = R[j];
/*Until we reach either end of either L or R, pick j++;
larger among elements L and R and place them in k++;
the correct position at A[beg…end]*/ }
Divide
Step-1
Step-2 Step-8
Conquer
Step-7
Step-13
Combine
Step-14
Complexity Analysis
•Divide: Just compute q as the average of p and r, which takes constant time i.e. Θ(1).
Quick Sort is a sorting algorithm based on the Divide and Conquer algorithm
that picks an element as a pivot and partitions the given array around the
picked pivot by placing the pivot in its correct position in the sorted array.
Quick_sort( A, low, up)
1. if low<up
2. then pivloc = partition(A, low, up)
3. Quick_sort(A, low, pivloc-1);
4. Quick_sort(A, pivloc+1, up);
Example:
48 44 19 59 72 80 42 65 82 8 95 68
pivot i=1 j=11
48 44 19 59 72 80 42 65 82 8 95 68
pivot i=1 j=11
19 < 48, increment i
48 44 19 59 72 80 42 65 82 8 95 68
pivot i=2 j=11
59 > 48, stop i at 3
48 44 19 59 72 80 42 65 82 8 95 68
pivot i=3 j=11
68 > 48, decrement j
48 44 19 59 72 80 42 65 82 8 95 68
pivot i=3 j=11
95 > 48, decrement j
48 44 19 59 72 80 42 65 82 8 95 68
pivot i=3 j=10
8 < 48, stop j at 9
Since, i < j, exchange a[3] and a[9]
48 44 19 59 72 80 42 65 82 8 95 68
pivot i=3 j=9
48 44 19 8 72 80 42 65 82 59 95 68
pivot i=3 j=9
48 44 19 8 72 80 42 65 82 59 95 68
pivot i=3 j=9
48 44 19 8 72 80 42 65 82 59 95 68
pivot i=4 j=8
82 > 48, decrement j
48 44 19 8 72 80 42 65 82 59 95 68
pivot i=4 j=8
48 44 19 8 72 80 42 65 82 59 95 68
pivot i=4 j=7
48 44 19 8 42 80 72 65 82 59 95 68
pivot i=4 j=6
80 > 48, stop i at 5
48 44 19 8 42 80 72 65 82 59 95 68
Pivot i=5
j=5
80 > 48, decrement j
48 44 19 8 42 80 72 65 82 59 95 68
Pivot i=5
j=5
42 < 48, stop j at 4
i is not less than j, so no exchange
i is increment
48 44 19 8 42 80 72 65 82 59 95 68
Pivot j=4 i=5
Now i > j, so stop movement of I and j.
J is the location for pivot
Exchange A[0] and A[4]
48 44 19 8 42 80 72 65 82 59 95 68
Pivot j=4 i=6
42 44 19 8 48 80 72 65 82 59 95 68
Left sub list Pivot Right sub list
•Divide: Just compute pivot element, which takes linear time i.e. Θ(n).
•Conquer: In best case, recursively solve 2 subproblems, each of size n/2,
which is 2T(n/2).
•Combine: No need to do anything.
Average Case Complexity - It occurs when the array elements are in jumbled order that is
not properly ascending and not properly descending. The average case time complexity of
quicksort is Θ(n lg n).
Worst Case Complexity - In quick sort, worst case occurs when the pivot element is either
greatest or smallest element. Suppose, if the pivot element is always the last element of the
array, the worst case would occur when the given array is sorted already in ascending or
descending order. The worst-case time complexity of quicksort is Θ(n2).
Counting Sort
Counting_sort( A)
1. for i=1 to K
do C[i] = 0;
2. for j=1 to n
do C[A[j]] = C[A[j]]+1;
3. for i=2 to K
do C[i] = C[i] + C[i-1];
4. for j=n downto 1
do B[C[A[j]]] = A[j];
C[A[j]] = C[A[j]] -1;
Complexity - In counting sort, complexity is O(n+K).
If K is very small then O(n) i..e linear time.
Email id: [email protected]/ [email protected]
Mobile: +91-9485230593/ 9999778726