0% found this document useful (0 votes)
97 views23 pages

Data Structures Notes 155 177

The document discusses various sorting algorithms: priority queue, quick sort, heap sort, and merge sort. It provides details on how each algorithm works, including pseudocode for quick sort. Priority queues use a heap to retrieve the maximum/minimum element in O(1) time. Quick sort uses a divide and conquer approach, partitioning elements around a pivot in O(nlogn) average time but O(n^2) worst case. Heap sort uses a binary heap to first build a max or min heap, then extract elements in sorted order in O(nlogn) time.

Uploaded by

Ruthvik Racha
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)
97 views23 pages

Data Structures Notes 155 177

The document discusses various sorting algorithms: priority queue, quick sort, heap sort, and merge sort. It provides details on how each algorithm works, including pseudocode for quick sort. Priority queues use a heap to retrieve the maximum/minimum element in O(1) time. Quick sort uses a divide and conquer approach, partitioning elements around a pivot in O(nlogn) average time but O(n^2) worst case. Heap sort uses a binary heap to first build a max or min heap, then extract elements in sorted order in O(nlogn) time.

Uploaded by

Ruthvik Racha
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/ 23

UNIT-IV

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.

There are mainly 4 operations we want from a priority queue:

1. Insert → To insert a new element in the queue.

2. Maximum/Minimum → To get the maximum and the minimum element


from the max-priority queue and min-priority queue respectively.

3. Extract Maximum/Minimum → To remove and return the maximum


and the minimum element from the max-priority queue and min-priority
queue respectively.

4. Increase/Decrease key → To increase or decrease key of any element in


the queue.

Maximum/Minimum

We know that the maximum (or minimum) element of a priority queue is at


the root of the max-heap (or min-heap). So, we just need to return the
element at the root of the heap.

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

• Quick sort is also known as partition exchange sort


• Quick sort, sorts the element using divide and conquer strategy.
Quick sort first divides the large array into two smaller subarrays: The
lowest elements and high elements. Quick sort can then recursively
sort the sub arrays.

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

C Program to implement the quick sort


#include <stdio.h>
#include <stdlib.h>
#include<math.h>
void quicksort(int[],int,int);

int main(int argc, char *argv[]) {

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;
}

void quicksort(int A[],int low,int high)


{
int i,j,pivot,temp;

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.

A binary heap is a useful data structure in which elements can be added


randomly but only the element with the highest value is removed in case of
max heap and lowest value in case of min heap.
Inserting a New Element in a Binary Heap
Consider a max heap H with n elements. Inserting a new value into the heap
is done in the following two steps:
1. Add the new value at the bottom of H in such a way that H is still a
complete binary tree but not necessarily a heap.
2. Let the new value rise to its appropriate place in H so that H now
becomes a heap as well.
To do this, compare the new value with its parent to check if they are in the
correct order. If they are, then the procedure halts, else the new value and
its parent’s value are swapped and Step 2 is repeated.
Consider the max heap given in Figure and insert 99 in it

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.

The memory representation of H can be given as shown in below

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

Deleting a root node from the max heap as follows

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

C Program to implement heap sort


#include <stdio.h>
#include <stdlib.h>
#define MAX 100

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;
}

void RestoreHeapDown(int *heap,int index, int n)


{
int val,j;
val = heap[index];
j=index*2;
while(j<=n)
{
if((j<n)&&(heap[j]<heap[j+1]))
j++;
if(heap[j]<val)
break;
heap[j/2]=heap[j];
j=j*2;
}
heap[j/2]=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.

Algorithm for RadixSort (ARR, N)


Step 1: Find the largest number in ARR as LARGE
Step 2: [INITIALIZE] SET NOP = Number of digits in LARGE
Step 3: SET PASS = 0

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

C Program to implement Radix sort


#include <stdio.h>
#include <stdlib.h>
#define size 10

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;
}

void Radixsort(int A[],int n)


{
int bucket[size][size],bucket_count[size];
int i,j,k,rem,nop=0,div=1,large,pass;
large=largest(A,n);
while(large>0)
{
nop++;
large=large/size;
}
for(pass=0;pass<nop;pass++)
{
for(i=0;i<size;i++)
bucket_count[i]=0;
for(i=0;i<=n;i++)
{

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;
}

Complexity of Radix sort

• The time complexity of radix sort is given by the formula,T(n) =


O(d*(n+b)), where d is the number of digits, n is the number of
elements in the list, and b is the base or bucket size used, which is
normally base 10 for decimal representation.
• In practical implementations, radix sort is often faster than other
comparison-based sorting algorithms, such as quicksort or merge
sort, for large datasets, especially when the keys have many digits.
However, its time complexity grows linearly with the number of digits,
and so it is not as efficient for small datasets.

5. Merge Sort

• Merge sort uses divide and conquer strategy.


• It divides input array in two halves, calls itself for the two halves and
then merges the two sorted halves.
• Divide means partitioning the n-element array to be sorted into two
sub-arrays of n/2 elements. If A is an array containing zero or one
element, then it is already sorted. However, if there are more elements
in the array, divide A into two sub-arrays, A and A2, each containing
about half of the elements of A.
• Conquer means sorting the two sub-arrays recursively using merge
sort.
• Combine means merging the two sorted sub-arrays of size n/2 to
produce the sorted array of n elements.

Merge sort Algorithm


MergeSort(arr[], l, r)
If r > l
1. Find the middle point to divide the array into two halves:
middle m = (l+r)/2
2. Call mergeSort for first half:
Call mergeSort(arr, l, m)
3. Call mergeSort for second half:

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:

Advantage and Drawback:


Advantage: It takes less for sorting the elements.
Drawback: It require extra space (extra memory) for sorting the elements.
Time Complexities
1. Best, average and worst case time complexity is O(nlogn)

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)

Quick sort O(nlogn) O(nlogn) O(n2)

Heap Sort O(nlogn) O(nlogn) O(nlogn)

Radix sort O(n+k) O(n+k) O(n.k)

Dr. G. Nagaraju, Assistant Professor, Department of CSE(AIML & IOT), VNRVJIET, Hyderabad

You might also like