Data Structures Notes 155 177
Data Structures Notes 155 177
Topics:
1. Priority Queue.
2. Quick Sort
3. HeapSort
4. Merge Sort
5. Radix sort
1. Priority Queue
Definition: Apriority queue is a data structure in which each element is
assigned a priority. The priority of the element will be used to determine the
order in which the elements will be processed.
The general rules of processing the elements of a priority queue are:
1. An element with higher priority is processed before an element with a
lower priority.
2. Two elements with the same priority are processed on a first-come-
first-served (FCFS) basis.
Priority queues are widely used in operating systems to execute the highest
priority process first. The priority of the process may be set based on the
CPU time it requires to get executed completely. For example, if there are
three processes, where the first process needs 5 ns to complete, the second
process needs 4 ns, and the third process needs 7 ns, then the second
process will have the highest priority and will thus be the first to be
executed.
Priority queue is a type of queue in which every element has a key
associated to it and the queue returns the element according to these keys,
unlike the traditional queue which works on first come first serve basis.
Thus, a max-priority queue returns the element with maximum key
first whereas, a min-priority queue returns the element with the
smallest key first.
Dr. G. Nagaraju, Assistant Professor, Department of CSE(AIML & IOT), VNRVJIET, Hyderabad
Priority queues are used in many algorithms like Huffman Codes, Prim's
algorithm, etc. It is also used in scheduling processes for a computer, etc.
There are two ways to implement the priority queue 1. List 2. Heap.
Heaps are great for implementing a priority queue because of the largest and
smallest element at the root of the tree for a max-heap and a min-heap
respectively. We use a max-heap for a max-priority queue and a min-heap
for a min-priority queue.
Maximum/Minimum
Dr. G. Nagaraju, Assistant Professor, Department of CSE(AIML & IOT), VNRVJIET, Hyderabad
Returning an element from an array is a constant time taking process, so it
is a Θ(1) process.
Extract Maximum/Minimum
This is like the pop of a queue, we return the element as well as delete it
from the heap. So, we have to return and delete the root of a heap. Firstly,
we store the value of the root in a variable to return it later from the
function and then we just make the root equal to the last element of the
heap. Now the root is equal to the last element of the heap, we delete the
last element easily by reducing the size of the heap by 1.
Doing this, we have disturbed the heap property of the root but we have not
touched any of its children, so they are still heaps. So, we can
call Heapify on the root to make the tree a heap again.
All the steps are constant time taking process except the Heapify operation,
it will take O(logn) time and thus the Extract Maximum/Minimum is going
to take O(logn) time.
2. Quick Sort
Dr. G. Nagaraju, Assistant Professor, Department of CSE(AIML & IOT), VNRVJIET, Hyderabad
The quick sort algorithm works as follows:
1. Select an element pivot from the array elements.
2. Rearrange the elements in the array in such a way that all elements
that are less than the pivot appear before the pivot and all elements
greater than the pivot element come after it (equal values can go either
way). After such a partitioning, the pivot is placed in its final position.
This is called the partition operation.
3. Recursively sort the two sub-arrays thus obtained. (One with sub-list
of values smaller than that of the pivot element and the other having
higher value elements.)
Algorithm:
Quicksort Algorithm
Step 1: Make the left most index value pivot.
Step 2: Partition the array using pivot value.
Step 3: Apply quick sort on left paratitioned array.
Step 4: Apply quick sort on right partitioned array.
Quick Sort Partiton Algorithm
Step 1: Choose the lowest index value has pivot
Step 2: Take two variables to point to left and right of the list excluding
pivot.
Step 3: Left points to the low index
Step 4: Right points to the high index
Step 5: While value at left is less than pivot, move right.
Step 6: While value at right is greater than pivot, move left.
Step 7: If left <= right then swap left and right
Step 8: If left >= right then swap point where they met is new pivot.
Dr. G. Nagaraju, Assistant Professor, Department of CSE(AIML & IOT), VNRVJIET, Hyderabad
Example: Sort the elements given in the following array using quick sort
algorithm 27, 10, 36, 18, 25, 45
Dr. G. Nagaraju, Assistant Professor, Department of CSE(AIML & IOT), VNRVJIET, Hyderabad
int A[10],i,n;
printf("\n Enter array size\n");
scanf("%d",&n);
printf("\n Enter array elements\n");
for(i=0;i<n;i++)
{
scanf("%d",&A[i]);
}
quicksort(A,0,n-1);
printf("\n After sorting array elements are\n");
for(i=0;i<n;i++)
{
printf("%d\n",A[i]);
}
return 0;
}
if(low<=high)
{
i=low;
j=high;
pivot = low;
while(i<=j)
{
while(A[i]<=A[pivot] && i<=j) i++;
Dr. G. Nagaraju, Assistant Professor, Department of CSE(AIML & IOT), VNRVJIET, Hyderabad
while(A[j] >A[pivot] && j >=i) j--;
if(i<j)
{
temp=A[j];
A[j] = A[i];
A[i] = temp;
}
}
temp = A[j];
A[j] = A[pivot];
A[pivot] = temp;
quicksort(A,low,j-1);
quicksort(A,j+1,high);
}
}
Advantage and Drawback
• Advantage: It does not require any extra space for sorting array elements.
• Drawback: It takes more time for sorting array elements.
Time Complexities
1. Best and average case time complexity is O(nlogn)
2. Worst case time complexity is O(n2).
In the best case, every time we partition the array, we divide the list
into two nearly equal pieces. That is, the recursive call processes the sub-
array of half the size. At the most, only logn nested calls can be made before
we reach a sub-array of size 1. It means the depth of the call tree is O(log n).
And because at each level, there can only be O(n), the resultant time is given
as O(nlog n) time.
Practically, the efficiency of quick sort depends on the element which
is chosen as the pivot. Its worst-case efficiency is given as O(n2). The worst
case occurs when the array is already sorted (either in ascending or
descending order) and the left-most element is chosen as the pivot.
Dr. G. Nagaraju, Assistant Professor, Department of CSE(AIML & IOT), VNRVJIET, Hyderabad
3. Heap Sort
Heap is a complete binary tree.
Max heap: It is a complete binary tree in which every parent of the node
greater than its descendants. Such a heap is called Max heap.
Min Heap: It is a complete binary tree in which every parent of the node is
less than its descendants. Such a heap is called Min heap.
A heap is defined as a complete binary tree, all its elements can be stored
sequentially in an array. It follows the same rules as that of a complete
binary tree. That is, if an element is at position i in the array, then its left
child is stored at position 2i and its right child at position 2i+1. Conversely,
an element at position i has its parent stored at position i/2.
1. Being a complete binary tree, all the levels of the tree except the last
level are completely filled.
2. The height of a binary tree is given as log2 n, where n is the number of
elements.
3. Heaps (also known as partially ordered trees) are a very popular data
Dr. G. Nagaraju, Assistant Professor, Department of CSE(AIML & IOT), VNRVJIET, Hyderabad
structure for implementing priority queues.
The first step says that insert the element in the heap so that the heap is a
complete binary tree. So, insert the new value as the right child of node 27
in the heap.
Dr. G. Nagaraju, Assistant Professor, Department of CSE(AIML & IOT), VNRVJIET, Hyderabad
Now, as per the second step, let the new value rise to its appropriate place in
H so that H becomes a heap as well. Compare 99 with its parent node value.
If it is less than its parent’s value, then the new node is in its appropriate
place and H is a heap. If the new value is greater than that of its parent’s
node, then swap the two values. Repeat the whole process until H becomes a
heap. This is illustrated in Figure.
Exercise: Build a max heap H from the given set of numbers: 45, 36, 54,
27, 63, 72, 61, and 18. Also draw the memory representation of the heap.
Algorithm
Step 1: [Add the new value and set its POS]
SETN=N+1, POS=N
Step 2: SET HEAP[N] = VAL
Step 3: [Find appropriate location of VAL]
Dr. G. Nagaraju, Assistant Professor, Department of CSE(AIML & IOT), VNRVJIET, Hyderabad
Repeat Steps 4 and 5 while POS>1
Step 4: SET PAR = POS/2
Step 5: IF HEAP[POS] <= HEAP[PAR],
then Goto Step 6.
ELSE
SWAP HEAP[POS], HEAP[PAR]
POS = PAR
[END OF IF]
[END OF LOOP]
Step 6: RETURN
Deleting an Element from a Binary Heap
Consider a max heap H having n elements. An element is always deleted
from the root of the heap. So, deleting an element from the heap is done in
the following three steps:
1. Replace the root node’s value with the last node’s value so that H is still a
complete binary tree but not necessarily a heap.
2. Delete the last node.
3. Sink down the new root node’s value so that H satisfies the heap
property. In this step, interchange the root node’s value with its child
node’s value (whichever is largest among its children).
Dr. G. Nagaraju, Assistant Professor, Department of CSE(AIML & IOT), VNRVJIET, Hyderabad
Algorithm
Step 1: [Remove the last node from the heap]
SET LAST = HEAP[N], SETN=N-1
Step 2: [Initialization]
SET PTR=1, LEFT=2, RIGHT=3
Step 3: SET HEAP[PTR] = LAST
Step 4: Repeat Steps 5 to 7 while LEFT <= N
Step 5: IF HEAP[PTR] >= HEAP[LEFT] AND HEAP[PTR] >= HEAP[RIGHT]
Go to Step 8
[END OF IF]
Step 6: IF HEAP[RIGHT] <= HEAP[LEFT]
SWAP HEAP[PTR], HEAP[LEFT]
SET PTR = LEFT
ELSE
SWAP HEAP[PTR], HEAP[RIGHT]
SET PTR = RIGHT
[END OF IF]
Step 7: SET LEFT=2* PTR and RIGHT = LEFT+1
[END OF LOOP]
Step 8: RETURN
Applications of Binary Heaps
1. Sorting an array using heapsort algorithm.
2. Implementing priority queues
Dr. G. Nagaraju, Assistant Professor, Department of CSE(AIML & IOT), VNRVJIET, Hyderabad
void RestoreHeapUp(int *heap,int index)
{
int val;
val = heap[index];
while((index>1)&&(heap[index/2]<val)) //check parents value
{
heap[index]=heap[index/2];
index = index/2;
}
heap[index]=val;
}
Dr. G. Nagaraju, Assistant Professor, Department of CSE(AIML & IOT), VNRVJIET, Hyderabad
int main(int argc, char *argv[]) {
int Heap[MAX],n,i,j,temp;
printf("\n Enter array size");
scanf("%d",&n);
printf("\n Enter array elements:\n");
for(i=1;i<=n;i++)
{
scanf("%d",&Heap[i]);
RestoreHeapUp(Heap,i); //Heapify
}
// Delete the root element and heapify the heap
printf("\n After building heap array elements are\n");
for(i=1;i<=n;i++)
{
printf("\n%d",Heap[i]);
}
j=n;
for(i=1;i<=j;i++)
{
temp=Heap[1];
Heap[1]=Heap[n];
Heap[n]=temp;
n=n-1;
RestoreHeapDown(Heap,1,n); //Heapify
}
n=j;
printf("\n After sorting array elements are\n");
for(i=1;i<=n;i++)
{
printf("\n%d",Heap[i]);
Dr. G. Nagaraju, Assistant Professor, Department of CSE(AIML & IOT), VNRVJIET, Hyderabad
}
return 0;
}
Time Complexities:
1. Best case time complexity is O(n).
The best case for heapsort would happen when all elements in the list to be
sorted are identical. In such a case, for 'n' number of nodes-
• Removing each node from the heap would take only a constant
runtime, O(1). There would be no need to bring any node down or
bring max valued node up, as all items are identical.
• Since we do this for every node, the total number of moves would be n
* O(1).
Therefore, the runtime in the best case would be O(n)
2. Worst case and average time complexity is O(nlogn)
5. Radix Sort
• Radix sort is a linear sorting algorithm for integers and uses the
concept of sorting names in alphabetical order.
• When we have a list of sorted names, the radix is 26 (or 26 buckets)
because there are 26 letters in the English alphabet. So radix sort is
also known as bucket sort.
• It works by sorting the elements based on each digit or letter from the
least significant digit to the most significant digit. Radix sort is often
used for sorting strings, integers, or other data types where the order
of individual digits or letters is significant.
• Words are first sorted according to the first letter of the name, During
the second pass, names are grouped according to the second letter,
After the second pass, names are sorted on the first two letters. This
process is continued till the nth pass, where n is the length of the
name with maximum number of letters.
• When radix sort is used on integers, sorting is done on each of the
digits in the number. The sorting procedure proceeds by sorting the
least significant to the most significant digit. While sorting the
numbers, we have ten buckets, each for one digit (0, 1, 2, …, 9) and
the number of passes will depend on the length of the number having
maximum number of digts.
Dr. G. Nagaraju, Assistant Professor, Department of CSE(AIML & IOT), VNRVJIET, Hyderabad
Step 4: Repeat Step 5 while PASS <= NOP-1
Step 5: SET I = 0 and INITIALIZE buckets
Step 6: Repeat Steps 7 to 9 while I < N-1
Step 7: SET DIGIT = digit at PASSth place in A[I]
Step 8: Add A[I] to the bucket numbered DIGIT
Step 9: INCEREMENT bucket count for bucket numbered
DIGIT
[END OF LOOP]
Step 1 : Collect the numbers in the bucket
[END OF LOOP]
Step 11: END
Example: Sort the numbers given below using radix sort.
345, 654, 924, 123, 567, 472, 555, 808, 911
Sol: In the first pass, the numbers are sorted according to the digit at ones
place. The buckets are pictured upside down as shown below.
After this pass, the numbers are collected bucket by bucket. The numbers
are 911, 472, 123, 654, 924, 345, 555, 567, 808. The new list thus formed
is used as an input for the next pass. In the second pass, the numbers are
sorted according to the digit at the tens place. The buckets are pictured
upside down.
Dr. G. Nagaraju, Assistant Professor, Department of CSE(AIML & IOT), VNRVJIET, Hyderabad
The collected numbers after pass-2 are 808, 911, 123, 924, 345, 654, 555,
567, 472. In the third pass, the numbers are sorted according to the digit at
the hundreds place. The buckets are pictured upside down
Dr. G. Nagaraju, Assistant Professor, Department of CSE(AIML & IOT), VNRVJIET, Hyderabad
int largest(int A[],int n)
{
int large,i;
large=A[0];
for(i=1;i<=n;i++)
{
if(A[i]>large)
{
large=A[i];
}
}
return large;
}
Dr. G. Nagaraju, Assistant Professor, Department of CSE(AIML & IOT), VNRVJIET, Hyderabad
rem = (A[i]/div)%size;
bucket[rem][bucket_count[rem]]=A[i];
bucket_count[rem] +=1;
}
//collect the number after PASS pass
i=0;
for(k=0;k<size;k++)
{
for(j=0;j<bucket_count[k];j++)
{
A[i]=bucket[k][j];
i++;
}
}
div = div*size;
}
}
int main(int argc, char *argv[]) {
int i,n,A[10];
printf("\n Enter array size");
scanf("%d",&n);
printf("\n Enter array elements:\n");
for(i=0;i<n;i++)
{
scanf("%d",&A[i]);
}
Radixsort(A,n-1);
printf("\n After sorting array elements are\n");
for(i=0;i<n;i++)
{
Dr. G. Nagaraju, Assistant Professor, Department of CSE(AIML & IOT), VNRVJIET, Hyderabad
printf("\n%d",A[i]);
}
return 0;
}
5. Merge Sort
Dr. G. Nagaraju, Assistant Professor, Department of CSE(AIML & IOT), VNRVJIET, Hyderabad
Call mergeSort(arr, m+1, r)
4. Merge the two halves sorted in step 2 and 3:
Call merge(arr, l, m, r)
Merge Algorithm
MERGE (ARR, BEG, MID, END)
Step 1: [INITIALIZE] SETI= BEG,J= MID+1, INDEX = 0
Step 2: Repeat while (I <= MID) AND (J<=END)
IF ARR[I] < ARR[J]
SET TEMP[INDEX] = ARR[I]
SETI=I+1
ELSE
SET TEMP[INDEX] = ARR[J]
SETJ=J+1
[END OF IF]
SET INDEX = INDEX+1
[END OF LOOP]
Step 3: [Copy the remaining elements of right sub-array, if any]
IF I > MID
Repeat while J <= END
SET TEMP[INDEX] = ARR[J]
SET INDEX = INDEX+1, SETJ=J+1
[END OF LOOP]
[Copy the remaining elements of left sub-array, if
any]
ELSE
Repeat whileI<= MID
SET TEMP[INDEX] = ARR[I]
SET INDEX = INDEX+1, SETI=I+1
[END OF LOOP]
[END OF IF]
Dr. G. Nagaraju, Assistant Professor, Department of CSE(AIML & IOT), VNRVJIET, Hyderabad
Step 4: [Copy the contents of TEMP back to ARR] SET K= 0
Step 5: Repeat whileK< INDEX
SET ARR[K] = TEMP[K]
SETK=K+1
[ END OF LOOP]
Step 6: END
Example:
Dr. G. Nagaraju, Assistant Professor, Department of CSE(AIML & IOT), VNRVJIET, Hyderabad
Summary of all sorting techniques
Time Complexity
Algorithm
Best case Average case Worst case
Merge Sort O(nlogn) O(nlogn) O(nlogn)
Dr. G. Nagaraju, Assistant Professor, Department of CSE(AIML & IOT), VNRVJIET, Hyderabad