0% found this document useful (0 votes)
15 views36 pages

Sortings

The document provides an overview of various sorting algorithms, including Heap Sort, Merge Sort, and Quick Sort, detailing their procedures and complexities. Heap Sort involves building a max heap and sorting elements through repeated root deletion, while Merge Sort uses a divide and conquer approach to sort subarrays. Quick Sort selects a pivot to partition the array and recursively sorts the partitions, with all algorithms exhibiting a time complexity of O(n log n) in their respective best, average, and worst cases.

Uploaded by

Sachit Jain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views36 pages

Sortings

The document provides an overview of various sorting algorithms, including Heap Sort, Merge Sort, and Quick Sort, detailing their procedures and complexities. Heap Sort involves building a max heap and sorting elements through repeated root deletion, while Merge Sort uses a divide and conquer approach to sort subarrays. Quick Sort selects a pivot to partition the array and recursively sorts the partitions, with all algorithms exhibiting a time complexity of O(n log n) in their respective best, average, and worst cases.

Uploaded by

Sachit Jain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 36

Data Structures

Sorting

Dr Deepak Gupta
Assistant Professor, SMIEEE
CSED, MNNIT Allahabad, Prayagraj
Email: [email protected]
Heap Sort

A heap is a binary tree that satisfies the following properties:

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: Heap sort is used to sort the elements.

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

Heapify 4: Swap 4 and 9. 1

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

This is final Max-heap.


Heap Sort Illustration:

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 -

Next, we have to delete the root element (89) 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 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.

After completion of sorting, the array elements are -

Now, the array is completely sorted.

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

So, we can build a max heap in linear time.


Merge Sort

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-3 Step-5 Step-9 Step-11

Step-4 Step-6 Step-10 Step-12

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).

•Conquer: Recursively solve 2 subproblems, each of size n/2, which is 2T(n/2).

•Combine: MERGE on an n-element subarray takes Θ(n) time.

We can show that this recurrence has the solution

T(n) = Θ(n lg n).


Quick Sort

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

44 < 48, increment i

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

Increment i and decrement j

48 44 19 8 72 80 42 65 82 59 95 68
pivot i=3 j=9

72 > 48, stop i at 4

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

65 > 48, decrement j

48 44 19 8 72 80 42 65 82 59 95 68
pivot i=4 j=7

42 < 48, stop j at 6


Since, i < j, exchange a[4] and a[6]
48 44 19 8 72 80 42 65 82 59 95 68
pivot i=4 j=6

Increment i and decrement j

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

Pivot placed at proper place

42 44 19 8 48 80 72 65 82 59 95 68
Left sub list Pivot Right sub list

We will continue in the same way.


Complexity Analysis

•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.

we can show that this recurrence has the solution


T(n) = Θ(n lg n).
Best Case Complexity - In Quicksort, the best-case occurs when the pivot element is the
middle element or near to the middle element. The best-case time complexity of quicksort is
Θ(n lg n).

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

Input: A[1,…,n] where A[j] €{1,2,…,K}


Output: B[1,…,n] //sorting of A
Auxiliary Storage: C[1,…,K]

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

You might also like