Sorting
Sorting
Unit-2: Sorting
(a)In-place sorting and not-in-place sorting - In in-place sorting algorithm we use fixed additional
space for producing the output (modifies only the given list under consideration). It sorts the list
only by modifying the order of the elements within the list. e.g. Bubble sort, Comb sort, Selection
sort, Insertion sort, Heap sort, and Shell sort. In not-in-place sorting, we use equal or more
additional space for arranging the items in the list. Merge-sort is an example of not-in-place
sorting.
(b) Stable sorting and Unstable sorting– In stable sorting algorithm the order of two objects with
equal keys in output remains same after sorting as they appear in the input array to be sorted. e.g.
Merge Sort, Insertion Sort, Bubble Sort, and Binary Tree Sort. If a sorting algorithm, after sorting
the contents, changes the sequence of similar content in which they appear, it is called unstable
Sorting. e.g. Quick Sort, Heap Sort, and Selection sort
(c) Internal sorting and External sorting- If the input data is such that it can be adjusted in the
main memory at once or, when all data is placed in-memory it is called internal sorting e.g. Bubble
Sort, Insertion Sort, Quick Sort. If the input data is such that it cannot be adjusted in the memory
entirely at once, it needs to be stored in a hard disk, floppy disk, or any other storage device. This
is called external sorting. External sorting algorithms generally fall into two types, distribution
sorting, which resembles quick sort, and external merge sort, which resembles merge sort. The
latter typically uses a hybrid sort-merge strategy
(d) Adaptive Sorting and Non-Adaptive Sorting- A sorting algorithm is said to be adaptive, if it
takes advantage of already 'sorted' elements in the list that is to be sorted. That is, while sorting if
the source list has some element already sorted, adaptive algorithms will take this into account
and will try not to re-order them. A non-adaptive algorithm is one which does not take into
account the elements which are already sorted. They try to force every single element to be re-
ordered to confirm their sortedness.
(e) Comparison based and non-comparison based - Algorithms, which sort a list, or an array,
based only on the comparison of pairs of numbers, and not any other information (like what is
being sorted, the frequency of numbers etc.), fall under this category. Elements of an array are
compared with each other to find the sorted array. e.g. Bubble Sort, Selection Sort, Quick Sort,
Merge Sort, Insertion Sort. In non-comparison based sorting, elements of array are not compared
with each other to find the sorted array. e.g. Radix Sort, Bucket Sort, Counting Sort.
Here, in the subsequent portion of this chapter, we will discuss about following Sorting Techniques
and their variants.
1. Bubble Sort
2. Selection Sort
3. Insertion Sort
4. Heap Sort
5. Merge Sort
6. Quick Sort
7. Counting Sort
8. Radix Sort
9. Bucket Sort
The Sorting Techniques can broadly be categorised (based on time complexity) into
- Order of n2 (Bubble Sort, Selection Sort, Insertion Sorts),
- Order of nlog n (Heap Sort, Quick Sort, Merge Sort)
- Order of n (Counting Sort, Bucket Sort and Radix Sort)
The document discusses about various cases related to these sorting and strategies to improve the
run time.
2.2. Bubble Sort:
2.2.1. Working of Bubble Sort:
Consider a situation when we pour some detergent in the water and shake the water, the bubbles
are formed. The bubbles are formed of different sizes. Largest volume bubbles have a tendency of
coming to the water surface faster than smaller ones.
Bubble sort, which is also known as sinking sort, is a simple Brute force Sorting technique that
repeatedly iterates through the item list to be sorted. The process compares each pair of adjacent
items and swaps them if they are in the wrong order. The algorithm starts at the beginning of the
data set. It compares the first two elements, and if the first is greater than the second, it swaps
them. It continues doing this for each pair of adjacent elements to the end of the data set. It then
starts again with the first two elements, repeating with one less comparison than the last pass.
Bubble sort can be used to sort a small number of items (where its asymptotic inefficiency is not a
concern).
In Bubble Sort Algorithm largest element is fixed in the first iteration followed by second largest
element and so on so forth. In the process 1st element is compared with 2nd and if previous
element is larger than the next one, elements are exchanged with each other. Then 2nd and 3rd
elements are compared and if second is larger than the 3 rd, they are exchanged. The process
repeats and finally (n-1)st element gets compared with nth and if previous one is larger than the
next one, they are exchanged. This completes the first iteration. As a result largest element
occupies its appropriate position in the Sorted array i.e. last position.
In the next iteration, the same process is repeated but one less comparison is performed than
previous iteration (as largest number is already in place). As a result of second iteration, second
largest number reaches to its appropriate position in the sorted array.
The similar iterations are repeated n-1 times. The result is the sorted array of n elements.
In bubble sort time complexity for the case when elements are already sorted is θ(n2) , because
fixed number of iterations need has to be performed.
There are two scenarios possible –
Case 1 - When elements are already sorted: Bubble Sort does not perform any swapping.
Case 2 - When elements are sorted after k-1 passes: In the kth pass no swapping occurs.
A small change in Bubble Sort Algorithm above makes it optimized. If no swap happens in some
iteration means elements are sorted and we should stop comparisons. This can be done by the
use of a flag variable.
ALGORITHM Bubble Sort Optimized (A [ ],N)
BEGIN:
FOR i=1 TO N-1 DO
FLAG =1
FOR j= 1 To N-i DO
IF A[j] >A[j+1] THEN
Exchange (A[j], A[j+1])
FLAG=0
IF FLAG ==1 THEN
RETURN
END;
Optimized Bubble Sort Analysis
If elements are already sorted then it takes Ω(N) time because after the first pass flag remains
unchanged, meaning that no swapping occurs. Subsequent passes are not performed. A total of
N-1 comparisons are performed in the first pass and that is all.
If elements become sorted after k-1 passes, kth pass finds no exchanges. It takes θ(N*k) effort.
Optimized Bubble Sort (First Pass)
Normally we use sorting algorithms for array because it is difficult to convert array algorithms
into Linked List.
There are two reasons for this -
1-Linked List cannot be traversed back.
2- Elements cannot be accessed directly (traversal is required to reach to the element).
Bubble Sort Algorithm can be easily modified for Linked List due to two reasons -
1 - In Bubble Sort no random access needed
2 - In Bubble Sort move only forward direction.
ALGORITHM Bubble Sort Link list(START)
BEGIN:
Q=NULL
FLAG=TRUE
WHILE TRUE DO
FLAG=FALSE
T=START
WHILE TNext ! = Q DO
IF TInfo>TNextinfo THEN
Exchange (T, TNext)
FLAG=TRUE
T=TNext
Q=T
END;
Algorithm Complexity
If elements are already sorted then it takes Ω(N) time because after the first pass flag remains
unchanged, meaning that no swapping occurs. Subsequent passes are not performed. A total of
N-1 comparisons are performed in the first pass and that is all.
If elements become sorted after k-1 passes, kth pass finds no exchanges. It takes θ(N*k) effort.
2.3. Selection Sort:
Selection sort is nothing but a variation of Bubble Sort because in selection sort swapping occurs
only one time per pass.
In every pass, choose largest or smallest element and swap it with last or first element. If the
smallest element was taken for swap, position of first element gets fixed. The second pass starts
with the second element and smallest element is found out of remaining N-1 Elements and
exchanged with the second element. In the third pass the smallest element is found out of N-2
elements (3rd to Nth element) and exchanged with the third element and so on so forth. The same
is performed for N-1 passes.
Number of comparisons in this Algorithm is just the same as that of Bubble Sort but number of
swaps in this ‘N’ (as compared to N*(N-1)/2 Swaps in Bubble Sort.
The First Pass
The Second Pass
In first pass
Min =1;
FOR j=2 TO N DO
IF A[j]<A[min] THEN
Min = j;
Exchange (A[1], A[Min])
Total N-1 passes of similar nature
ALGORITHM Selection Sort(A[ ],N)
BEGIN:
FOR i=1 TO N-1 DO
Min=i
FOR j= i+1 TO N DO
IF A[j] <A[Min] THEN
Min = i
Exchange(A[i],A[Min])
END;
Time Analysis (Total (N-1) iterations)
N-1 comparisons in first iteration
N-2 comparison in second iteration
N-3 comparison in third iteration
…
1 Comparison in (N-1)st iteration
Total = (N-1) + (N-2) + (N-3) + … +1
= N(N-1)/2 = N2/2 – N/2
1 Exchange per iteration hence total exchanges are N-1. Total effort for N-1 iterations=3 *(N-1)
Total Effort = 3*(N–1) + N2/2 – N/2 = N2/2 + 5/2*N - 3
As there is no best case possible as all iterations are compulsory, Complexity should be
written in Theta notation i.e. θ(N2)
Space Complexity remains θ(1) as only 3 variables (i, j, Min) are used in the logic.
As selection sort takes only O(N) swaps in worst case, it is the best suitable where we need
minimum number of writes on disk. If write operation is costly then selection sort is the obvious
choice. In terms of write operations the best known algorithm till now is cycle sort, but cycle sort
is unstable algorithm.
2.3.2. Cycle Sort
Cycle sort is an in-place, unstable, comparison sort that is theoretically optimal in terms of the
total number of writes to the original array. It is optimal in terms of number of memory writes. It
minimizes the number of memory writes (Each value is either written zero times, if it’s already in
its correct position, or written one time to its correct position.)
It is based on the idea that array to be sorted can be divided into cycles. Cycles can be visualized as
a graph. We have N nodes and an edge directed from node i to node j if the element at i th index
must be present at jth index in the sorted array.
Example- Arr[]={10,5,2,3}
10 5 2 3
Step 3-
Cycle =0
Item=5
Pos=cycle
I=pos+1
WHILE i<n DO //n is number of item
IF arr[i]<item THEN
Pos++
Swap(arr[pos],item)
Step 4-
Cycle =0
Item=2
Pos=cycle
I=pos+1
WHILE i<n DO //n is number of item
IF arr[i]<item THEN
Pos++
Swap(arr[pos],item)
Read the story and try to convert it into technical form. For this problem reframes as-
Given two integer array of same size (let arr1, arr2), check if number can be arranged in a way
that
Arr 1[i] > Arr2[i] for all values of i
2-Identify Problem Constraints
4. Implementation-
ALGORITHM CheckHire(Arr1[ ], Arr2[ ], N)
BEGIN:
BubbleSort(Arr1)
BubbleSort(Arr2)
FOR i=0 TO N DO
IF Arr1[i ] < Arr2[i] THEN
WRITE(“Not hire”)
RETURN
WRITE(“Hire”)
END;
Complexity-O(N2)
1 The number of swapping needed to sort the numbers 8, 22, 7, 9, 31, 5, 13 in ascending
order, using Bubble Sort is
A 13
B 12
C 11
D 10
AN C
2 Consider a situation where swap operation is very costly. Which of the following sorting
algorithms should be preferred so that the number of swap operations is minimized in
general?
A Selection
B Merge
C Heap
D Quick
AN C
3 How many comparisons are needed to sort an array of length 5 if a straight selection sort
is used and array is already in the opposite order?
A 1
B 5
C 10
D 20
AN C
•Joey loves to eat Pizza. But he is worried as the quality of pizza made by most of the restaurants
is deteriorating. The last few pizzas ordered by him did not taste good :(. Joey is feeling extremely
hungry and wants to eat pizza. But he is confused about the restaurant from where he should
order. As always he asks Chandler for help.
•Chandler suggests that Joey should give each restaurant some points, and then choose the
restaurant having maximum points. If more than one restaurant has same points, Joey can
choose the one with lexicographically smallest name.
•Joey has assigned points to all the restaurants, but can't figure out which restaurant satisfies
Chandler's criteria. Can you help him out?
Input format
•Next N lines contain Name of Restaurant and Points awarded by Joey, separated by a space.
Restaurant name has no spaces, all lowercase letters and will not be more than 20 characters.
Output format
Print the name of the restaurant that Joey should choose.
Constraints
There are N people in city, who wants to visit Kingdom of Dreams. The road of reaching of
Kingdom is not so safe. So, they go to kingdom only in a security vehicle which can accommodate
at most 2 people.(There is only one security vehicle available in the town as it is quite costly and
unique).People started to hire this vehicle to reach safely by driving it by themselves. Every part
of journey from town to kingdom or from kingdom to town has some cost associated with it
which is given by an array A[] elements. Array A[] has n elements, where A(i) represents the cost
ith person has to pay if they travel alone in the vehicle. If two people travel in vehicle, the cost of
travel will be the maximum of cost of two people.Calculate the minimum total cost so that all N
people can reach Kingdom safely.
Input format
•The first line contains, T, denoting the number of test cases. Each test case contains 2 lines each.
The first line has an integer N, denoting the number of persons. Next line contains N space
separated distinct integers denoting the cost of ith person.
Output format
For each test case, print the minimum cost required so that all people can reach kingdom.
2.4. Insertion Sort
Consider a situation where playing cards are lying on the floor in the arbitrary manner. In case we
want these cards to be sorted, we can choose one of the cards and place that in hand. Every time
we pick a card from pile, we can insert that at the right place in the hand. This way we will have
sorted cards in the hand and a card arbitrarily chosen from lot will be inserted at the right place in
the cards in hand.
A, 2, 3, 4, 5, 6, 7 | K, 10, J, 8, 9, Q
Red colored card set is sorted, Black Colored card set unsorted. Try inserting a card from unsorted
set in the sorted set.
A, 2, 3, 4, 5, 6, 7, 8 | K, 10, J, 9, Q
Red colored card set is sorted, Black Colored card set unsorted. Try inserting a card from unsorted
set in the sorted set.
A, 2, 3, 4, 5, 6, 7, 8, 9 | K, 10, J, Q
Red colored card set is sorted, Black Colored card set unsorted. Try inserting a card from unsorted
set in the sorted set.
The process like this continues and finally we can have all the cards sorted in the hand.
2.4.2. Concept of Insertion Sort:
Insertion sort is a simple sorting algorithm that is relatively efficient for small lists and mostly
sorted lists.
It considers two halves in the same array: Sorted and unsorted. To start with only one element is
taken in the sorted list (first element) and N-1 elements in the unsorted list (2nd to Nth element). It
works by taking elements from the unsorted list one by one and inserting them in their correct
position into the sorted list. See the example below wherein we have taken a small set of numbers
containing 23, 1, 10, 5, 2. In the first pass we will consider the sorted parts to contain only 23 and
unsorted part containing 1, 10, 5, 2. A number (1) is picked from unsorted part and inserted in
sorted part at the right place. Now sorted part contains 2 elements. A number from unsorted part
(10) is picked and inserted in the sorted part. And so on so forth until all the elements from the
unsorted part have been picked and inserted in the sorted part. The diagram below shows the
step by step process:
Scenario–1
Insertion sort is best suited for online algorithms because it sorts the elements as it receives it (if
numbers of elements are known). If not known then it is required to store those elements into link
list.
Insertion sort performs better than even Quick sort where we sort small amount of data.
Scenario–2
When numbers of elements are almost sorted
Let us suppose that out of N elements N-1 elements sorted and only 1 element is out of place. The
Insertion sort takes O(N) time in this scenario.
2.4.4. Problems
Problem–1
Sort the given elements and compute time complexity needed. Suggest which sorting algorithm
will take minimum time to sort these elements.
Arr[]={43,33,64,54,86,75}
Solution-
From above it is clear that alternate elements are not sorted.
Outer loop runs n-1 times (Number of iterations)
Inner loop runs for 1 time or 2 time.
It means total time taken O(n)
Problem–2
Sort the given elements and compute time complexity needed. Suggest which sorting algorithm
will take minimum time to sort these elements.
Arr[]={5,5,5,5,5,5,5,5,5,5}
Solution-
From above it is clear that all the elements are same.
Outer loop runs n-1 times
Inner loop runs for one time per iteration.
It means total time taken O(n)
Inversion in insertion sort:
Inversion is defined as
A pair of elements (arr[i], arr[j]) is said to be inversion if arr[i]>arr[j] for i<j
If array sorted in ascending order – 0 inversion
If array sorted in descending order – n (n+1)/2 inversion
Problem–3
If the number of elements is n and it is given that number of inversion is O(n) then how much time
insertion sort takes to sort the elements.
Solution-
Control goes in inner loop only n times so insertion sort takes O(n) times.
Binary Insertion Sort
Use binary search logic in order to find the position where element to be inserted. It takes O(logk)
times in place of k times.
It is used where cost of comparison is more than cost of swapping. Consider the scenario of
sorting the names.
ALGORITHM insertionSort(ARR, N)
BEGIN:
FOR i=2 to N DO
j=i-1
temp = ARR[i]
loc = BinarySearch (ARR, temp, 0, j)
WHILE j>=loc DO
ARR[j+1] =ARR[j]
J=J–1
ARR[j+1]=temp
END;
2.4.5. Variation of Insertion Sort (Shell Sort)
Shell sort is a variation of insertion sort. It improves complexity of insertion by dividing it into
number of parts and then apply insertion sort.
It works on two facts about insertion sort-
1-Works better for less number of elements.
2-Elements are less distant towards their final positions.
There is a comparison between Hibard Shell Sort(n1.5) and Donald Shell Sort takes O(n2). It is most
widely gap sequence suggested by Thomas Hibard which is (2k-1 ……. 31 .. 15 .. 7 .. 3 .. 1) which
gives time complexity O(n √n)
2-Donald Knuth Gap Sequence- (1, 4, 13, …)
3- A gap sequence like (64, 32, 16, …) is not good because it increases time complexity.
2.4.6. Competitive Coding– Problem Statement-
There is a given Binary Search Tree in which any two of the nodes of the Binary Search Tree are
exchanged. Your task is to fix these nodes of the binary search tree.
Note: The BST will not have duplicates.
Examples:
Input Tree:
15
/ \
12 10
/\
4 35
In the above tree, nodes 35 and 10 must be swapped to fix the tree.
Following is the output tree
15
/ \
12 35
/\
4 10
Input format: Given nodes of binary search tree
Design Logic:
3-Compare both arrays and find out exchanged nodes and fix these node.
Implementation-
ALGORITHM bst(ROOT)
BEGIN:
ARRAY A[]
Inorder(ROOT,A)
Copy(B,A)
Insertion_sort(A,N)
FOR i=0 TO N DO
IF A[i] != B[i] THEN
Find(ROOT,A[i],B[i])
BREAK
RETURN ROOT
Find(ROOTleft,x,y)
IF !root THEN
RETURN
Find(ROOT->left, x, y)
IF ROOT ->data == x THEN
ROOT ->data = y
ELSE IF ROOT ->data == y
ROOT ->data = x
Find(ROOT ->right, x, y)
END;
Complexity-O(n)
2.4.7. Unsolved Coding problems on Insertion Sort:
Input:
N=5
arr[] = { 1,3,2,4,5}
Output: 1 2 3 4 5
Example 2:
Input:
N=7
arr[] = {7,6,5,4,3,2,1}
Output: 7 6 5 4 3 2 1
Task: Read no input and don’t print anything. Your task is to complete the
function insert() and insertionSort() where insert() takes the array, it's size and an index i
and insertion_Sort() uses insert function to sort the array in ascending order using insertion sort
algorithm.
Expected Time Complexity: O(Nlog n).
Expected Auxiliary Space: O(1).
Constraints:
1 <= N <= 1000
1 <= arr[i] <= 1000
2. Competitive Coding Problem Statement (code chef)
Given an array sort it using insertion sort and binary search in it to find the position to place the
element. Additionally you need to count the number of times recursive calls done by binary search
function to find the position.
Input:
First line will contain NN, size of the array.
Next Line contains n array elements a[i]a[i].
Output:
For each test case, output sorted array and binary search calls counter modulo
1000000007.
Constraints
1≤N≤100001≤N≤10000
0≤M≤1090≤M≤109
Sample Input:
5
54321
Sample Output:
Sorted array: 1 2 3 4 5
7
3 In the best case scenario while implementing Insertion sort algorithm, How many
comparisons occur in inner loop?
A n-1
B n/2
C 1
D None of these
AN C
4 In the worst case scenario the elements in the Insertion Sort algorithm will be?
A Strictly Increasing order
B Strictly Decreasing Order
C ZigZag order
D None of these
AN B
DL M
5 In the best case algorithm the elements in Insertion Sort algorithm will be?
A Strictly Increasing order
B Strictly Decreasing Order
C ZigZag order
D None of these5
AN A
DL E
6 What would be the space complexity and auxiliary space complexity when we implement
Insertion Sort Algorithm?
A O(n), O(1)
B O(1), O(n)
C O(1), O(1)
D None of these
AN C
7 Which of the following properties is satisfied by Insertion Sort algorithm?
A It is in place algorithm
B It has best case complexity of O(n)
C Overall complexity is n log n
D Both A and B
AN D
DL M
8 Consider the following array 34 15 29 8. How many number of comparisons required in the
third pass to sort the array using bubble sort?
A 1
B 2
C 0
D 3
AN 1
DL M
9 Consider the following array 10 9 11 6 15 2. What would be the worst case complexity of
the sorting operation on the array using bubble sort?
A O(n^2)
B O(n)
C O(1)
D O(log n)
AN A
DL E
10 Consider the following array 10 9 11 6 15 2. How the array looks like after the second pass
if applies bubble sort on the array.
A 9 10 6 11 2 15
B 9 6 10 2 11 15
C 6 9 2 10 11 15
D 9 6 10 2 11 15
AN B
DL M
11 Consider the following array 10 9 11 6 15 2. How the array looks like after the third pass if
applies bubble sort on the array.
A 9 10 6 11 2 15
B 9 6 10 2 11 15
C 6 9 2 10 11 15
D 9 6 10 2 11 15
AN C
DL M
Scenario Consider the following code given below, answer the following questions from 12 to
15
Insertion_sort(A)
{
for j = 2 to A.length
key = A[j]
// insert A[j] into sorted sequence of A[i..... j-1]
i = j-1;
while(i >0 and A[i] > key)
A[i+1] = A[i];
i = i -1;
A[i+1] = key;
}
12 How many numbers of swapping is required in the worst case scenario in the above
algorithm? Assuming there are n elements?
A 1
B n2
C n-1
D None of these
AN B
DL M
13 How many numbers of comparisons are required in the worst case scenario in the above
algorithm? Assuming there are n elements?
A n2
B 0
C 1
D n-1
AN A
DL M
14 How many numbers of swapping is required in the best case scenario in the above
algorithm? Assuming there are n elements?
A n2
B 0
C 1
D n-1
AN B
DL M
15 How many numbers of comparisons are required in the best case scenario in the above
algorithm? Assuming there are n elements?
A n2
B 0
C 1
D n-1
AN D
DL M
16 If we use Binary Search instead of linear search while implementing Insertion sort ,then
total number of swapping involved will be?
A O(log n)
B O(n)
C O(n2)
D None of these
AN C
DL M
17 If we use Binary Search instead of linear search while implementing Insertion sort ,then
total number of comparisons and time complexity will be?
A O(log n) and O(nlog n)
B O(n) and O(nlog n)
C O(log n) and O(n2)
D None of these
AN A
DL M
18 If we use a Doubly Linked list instead of linear search while implementing Insertion sort ,
then total number of comparisons involved will be?
A O(log n)
B O(n)
C O(n2)
D None of these
AN C
DL M
19 If we use a Doubly Linked list instead of linear search while implementing Insertion sort,
then total number of swapping involved will be?
A O(log n)
B O(n)
C O(n2)
D None of these
AN B
DL M
21 Which of the following algorithms will in its typical implementation give best performance
when applied on an array that is sorted or almost sorted? Assume there to be one or two
misplaces?
A Counting Sort
B Insertion Sort
C Bucket Sort
D Quick Sort
AN B
DL M
22 Bubble sort can be categorized into which of the following?
A Greedy Algorithm
B Dynamic Programming
C Brute force technique
D Divide and conquer
AN C
DL E
2.5. Heap Sort
A Knock out tournament is organized where first round is the quarter final in which there are 8 teams CSK,
Mumbai Indians, Delhi Capitals , Kolkata Knight Riders, Punjab Kings, Rajasthan Royals, RCB, Sunrisers
Hyderabad participated.
After the first round four teams will reach in semi final round where two semi finals will be played.
From the semi Final round, two team will reach in final. Now the winner of the final will take
This analogy resemble the concept of Heap(Max-Heap).
It is a type of binary tree in which all levels are completely filled except possibly the last level .
Also last level might or might not be filled completely. If last level is not full then all the nodes should be
filled from the left.
2.5.3.Heap:
A Binary heap is a complete Binary Tree which makes it suitable to be implemented using array.
A Binary Heap is categorized into either Min-Heap or Max-Heap.
In a Min Binary Heap, the value at the root node must be smallest among all the values present in Binary
Heap. This property of Min-Heap must be true repeatedly for all nodes in Binary Tree.
In a Max Binary Heap the value at the root node must be largest among all the values present in Binary
Heap. This property of Max-Heap must be true repeatedly for all nodes in Binary Tree.
As heap is a complete binary tree therefore height of tree having N nodes will always O(log n).
If Heap can be implemented using Array. Assume that Array indexes start from 1.
You can access a parent node or a child nodes in the array with indices below.
A parent node|parent(i) = i / 2
When you look at the node of index 4, the relation of nodes in the tree corresponds to the indices of the
array below. If i = 4, Left Child will be at 2 * 4 that is 8th position and Right Child will be at (2*4 + 1) 9th
position.
Also if the index of left child is 4 then index of its parent will be 4/2 =2.
2.5.3.2 Heap Operations
Following two operations are used to construct a heap from an arbitrary array:
a) MaxHeapify–In a given complete binary tree if a node at index k does not fulfill max-heap property
while its left and right subtree are max heap, MaxHeapify arrange node k and all its subtree to
satisfy maxheap property.
b) BuildMaxHeap–This method builds a Heap from the given array. So BuildMaxHeap use MaxHeapify
function to build a heap from the
MaxHeapify(A,i,N) is a subroutine.
When it is called, two subtrees rooted at Left(i) and Right(i) are max-heaps, but A[i] may not satisfy the
max-heap property.
MaxHeapify(A,i,N) makes the subtree rooted at A[i] become a max-heap by letting A*i+ “float down”.
Example:
c) Start from the first index of non-leaf node whose index is given by n/2 where n is the size of an array.
d) Set current element having index k as largest.
e) The left child index will be 2*k and the right child index will be 2*k + 1 (when array index starts from 1).
If left Child value is greater than current Element (i.e. element at kth index), set leftChildIndex as largest
index.
If rightChild value is greater than element at largest index, set rightChildIndex as largest index.
g) Repeat the steps from (c) to (f) until the subtrees are also get heapify.
ALGORITHM MaxHeapify(A[ ], k, N)
BEGIN:
L = Left(k) //Left(k)=2*k
R = Right(k) //Right(k)=2*k+1
largest = L
ELSE
largest = k
largest ← R
IF largest != k THEN
END;
Left child of orange marked node
is 2 and right child is 8.
Steps:
ALGORITHM BuildMaxHeap(A[ ], N)
BEGIN:
MaxHeapify(A, i, N)
END;
Analysis
As we know that time complexity of Heapify operation depends on the height of the tree i.e. H and H
should be log N when there are N nodes in the heap.
The height ’h’ increases as we move upwards along the tree. Build-Heap runs a loop from the index of the
last internal node N/2 with height=1, to the index of root(1) with height = lg(n). Hence, Heapify takes
different time for each node, which is θ(h).
Finally, we have got the max-heap in fig.(f).
2. Insert operation:
a) First update the size of the tree by adding 1 in the given size.
b) Then insert the new element at the end of the tree.
c) Now perform Heapify operation to place the new element at its correct position in the tree and
make the tree either as max heap or min heap.
BEGIN:
N = N + 1;
A[ N ] = item;
ReHeapifyUp( A[ ], N, k)
END;
Algorithm: ReHeapifyUp( A[ ], N, k)
BEGIN:
parentindex = k/ 2;
IF parentindex> 0 THEN
IF A[k]>A[parentindex] THEN
Exchnage(A[k], A[parentindex])
ReHeapifyUp(A, N, parentindex)
END;
Analysis:
3 Delete Operation:
a) First exchange theroot element with the last element in the heap..
b) Then remove the last element by decreasing the size of the tree by 1.
c) Now perform Heapify operation to make the tree either as max heap or min heap.
Method-2 To delete an element at any position from Max Heap.
Algorithm: DelHeap(A[ ], N)
BEGIN:
END;
Analysis:
Heap Sort is a popular and efficient sorting algorithm to sort the elements.
Step1: In the Max-Heap largest item is always stored at the root node.
Step 2: Now exchange the root element with the last element in the heap.
Step 4: Now performMax-Heapify operation so that highest element should arrive at root. Repeat these
steps until all the items are sorted.
Example:
Step1: In the Max-Heap largest item is always stored at the root node.
Step 2 Now exchange the root element with the last element.
Step1: Replace
A[1] with A[last].
Step4 Now perform Max-Heapify operation so that highest element should arrive at root.
L = 2*k
R = L+1
Call Max-Heapify()
IF L ≤ heap-size(A) AND A[L] > A[ k ] THEN
largest = L
else
largest = k
largest = R
IF largest != k THEN
ALGORITHM HeapSort(A[ ], N)
BEGIN:
BuildMaxHeap(A, N)
FOR j = N to 2 STEP – 1 DO
END;
Analysis
Heap Sort has θ(nlog n) time complexities for all the cases ( best case, average case, and worst case).
As heap is the complete binary tree so the height of a complete binary tree containing n elements is log n.
During the sorting step, we exchange the root element with the last element and heapify the root element.
For each element, this again takes log n worst time because we might have to bring the element all the way
from the root to the leaf. Since we repeat this n times, the heapsort step is also nlog n.
1. Heapsort: One of the best sorting methods being in-place and log(N) time complexity in all scenarios.
2. Selection algorithms: Finding the min, max, both the min and max, median, or even the kth largest
element can be done in linear time (often constant time) using heaps.
3. Priority Queues: Heap Implemented priority queues are used in Graph algorithms like Prim’s Algorithm
and Dijkstra’s algorithm. A heap is a useful data structure when you need to remove the object with the
highest (or lowest) priority. Schedulers, timers
4. Graph algorithms: By using heaps as internal traversal data structures, run time will be reduced by
polynomial order. Examples of such problems are Prim's minimal
5. Because of the lack of pointers, the operations are faster than a binary tree. Also, some more
complicated heaps (such as binomial) can be merged efficiently, which is not easy to do for a binary tree.
2.5.6. Variant of Heap Sort
First, we will see some Variant of Heap sort:- Some variants of heapsort are as follows:-
1. Ternary Heap Sort:- Here we use ternary heap instead of binary heap. A ternary heap is one where
each node is having three children. It uses fewer numbers of comparison and swapping as compare
to Binary Heap Sort.
A ternary heap is a data structure which is part of the heap family. It inherits the properties from
ternary tree and the heap data structure.
In both cases, a ternary heap has to satisfy the heap property, which is every level of tree has to be filled, or
if the last level is not filled, the leaves are distributed from left to right.
You can access a parent node or a child nodes in the array with indices below.
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.SO this queue does not follow the concept of FCFS(First Come First
Serve).
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.
Heap Implemented priority queues are used in Graph algorithms like Prim’s Algorithm and Dijkstra’s
algorithm. A heap is a useful data structure when you need to remove the object with the highest (or
Therefore, with the help of heap following operations of the priority queue can be implemented:
a) First update the size of the tree by adding 1 in the given size.
b) Then insert the new element at the end of the tree.
c) Now perform Heapify operation to place the new element at its correct position in the tree and
make the tree either as max heap or min heap.
b) Implementation of PrintMax()/PrintMin():
As we know that the largest element is present at the root node of max heap and smallest
element is present at root in min heap. So simply print the root node to get either lagest
element(i.e element with highest priority or key) or smallest element (i.e element with lowest
priority or key)
c) Implementation of DeleteMax/DeleteMin():
d) First exchange theroot element with the last element in the heap..
e) Then remove the last element by decreasing the size of the tree by 1.
f) Now perform Heapify operation to make the tree either as max heap or min heap.
Time Complexity: O(log N) where N is the number of elements in heap.
d) Implementation of UpdatePriority():
Using this operation, we can
i) Either decrease or increase the priority or key of any element then
ii) Using the heapify procedure place that update priority element at its correct position.
To maintain the priority queue using array, it take O(Nlog N) time while O(log N) using Heap.
1 Consider a max heap, represented by the array: 40, 30, 20, 10, 15, 16, 17, 8, 4. (Gate 2015)
Array Index 1 2 3 4 5 6 7 8 9
Value 40 30 20 10 15 16 17 8 4
AN B
DL E
2 An operator delete (i) for a Binary Heap Data Structure is to be designed to delete the item in the ith
node. Assume that the heap is implemented in an array and i refers to the ith index of the array. If the
heap tree has depth d (number of edges on the path from the root to the farthest leaf), then what is
the time complexity to re-fix the heap efficiently after the removal of the element? (Gate 2016)
A O (1)
AN B
DL E
3 Consider the following array of elements <89, 19, 50, 17, 12, 15, 2, 5, 7, 11, 6, 9, 100>
The minimum number of interchanges needed to convert it into a max-heap is (Gate 2015)
A 4
B 5
C 2
D 3
AN D
DL E
4 Consider a binary max-heap implemented using an array. Which one of the following array
represents a binary max-heap? (Gate 2009)
A {25,12,16,13,10, 8,14}
C {25,14,16,13,10, 8,12}
D {25,14,12,13,10, 8,16}
AN C
DL E
4 Consider a binary max-heap implemented using an array. What is the content of the array after
two delete operations on the correct answer to the previous question? (Gate 2009)
A {14, 13,12,10, 8}
B {14,12,13, 8,10}
C {14,13, 8, 12,10}
D {14,13,12, 8,10}
AN C
DL E
6 Consider a max heap, represented by the array: 41, 31, 21, 11, 16, 17, 18, 9, 5. Now consider that a
value 35 is inserted into this heap. After insertion, the new heap is
AN A
DL E
7 Consider the following array of elements 90, 20, 51, 18, 13, 16, 3, 6, 8, 12, 7, 10, 101 The minimum
number of interchanges needed to convert it into a max-heap is ?
A 2
B 4
C 3
D 6
AN C
AN C
9 Given a binary-max heap. The elements are stored in an array as 26, 15, 17, 12, 11, 9, 13. What is the
content of the array after two delete operations?
A 15,14,9,13,11
B 15,13,14,11,9
C 15,14,13,9,11
D 15,14,13,11,9
AN C
DL E
10. Consider a max heap, represented by the array: 42, 32, 22, 12, 17, 18, 19, 10, 6. Now consider that a
value 36 is inserted into this heap. After insertion, the new heap is
AN A
D None of these
AN C
12 Which of the following is not a complete binary tree?
D None of these
AN C
Q-1) What are the minimum and maximum number of elements in a heap of height h?
Q-2) Is there a min-heap with seven distinct elements so that the preorder traversal of
Q-3) Show the steps for deleting an arbitrary element from min heap.
Q-5) Is there any optimized method to improve the time complexity for finding the Kth smallest element in
min heap.
Q-2) Show the steps to find the Kth smallest element in min heap. Also calculate the time complexity.
Q-4) Given a sequence of numbers: 1, 2, 3, 4, 5, 6, 7, 8, 9
b. Also draw the tree that will be formed after calling Dequeue() on this heap
Q-5) Given an array: [3, 9, 5, 4, 8, 1, 5, 2, 7, 6]. Apply heapify over this to make a min heap and sort the
elements in decreasing order?
Q-6) In Heap-Sort once a root element has been put in its final position, how much time does it take to re-
heapify the structure so that the next removal can take place?
Q-1) Given a big file containing 10 billion of numbers, how can you find the 20 maximum numbers from
that file?
Q-2) Implementation of Smooth Sort algorithm? (Given that it uses bottom up strategy, depth first and post
order traversal).
Q-3) Implement the Max-Heapify operation ,insert operation and delete operation for ternary heap.
Merge sort is based on divide and conquer approach.It kept on dividing the elements present in
array until it cannot be divided further and then we combine all these elements in order to get
elements in sorted order.
Analogy 1: King Conquering an entire territory: Suppose you are a king and you wish to conquer
an entire territory, your army is awaiting your orders. So, for a feasible approach rather than
attacking the entire territory at once, find the weaker territory and conquer them one by one. So,
ultimately we take a large land, split up into small problems. Capturing smaller territories is like
solving small problems and then capturing as a whole.
Analogy 2.Searching a page number in book: To straightaway go to any page number of a book let
say page 423. I do know the fact that page numbers are serialized from 1 to 710. Initially, I would
just randomly open the book at any location. Suppose page 678 opens, I know my page won’t be
found there. So, my search would be limited to the other half of the book. Now, if I pick any page
from other half let say 295. That means that portion of the book is eliminated so I am left with
portion of 296- 677 pages. In this way we are reducing our search space. If for the same brute
force is used the navigation from page 1 till end would lead to many iterations. So, this gives an
idea how merge sort can be applied by the same concept of splitting into smaller sub problems.
Figure 2: Elaboration on searching a page number in book
Analogy 3: Given a loaf of bread, you need to divide it into 1/8th1/8th1/8th pieces, without
using any measuring tape:
One day a man decided to distribute a loaf of bread to eight beggars, he wanted to distribute it in
equal proportion among them. He was not having any knife with him so he decided to divide it in
two equal halves, now he divided the first ½ into two equal halves that is into two ¼ equal halves,
further dividing these ¼ pieces will result in getting four equal 1/8 pieces. Similarly, the second ½
bread loaf can be further divided into two ¼ pieces and then further it can be divided into four 1/8
equal pieces. Hence, total eight equal size pieces is divided among 8 beggars :
In Divide and Conquer approach, we break a problem into sub-problems. When the solution to
each sub-problem is ready, we merge the results obtained from these subproblems to get the
solution to the main problem . Let’s take an example of sorting an array A. Here, the sub problem
is to sort the sub problem after dividing it into two parts. If q, is the mid index, then the array will
be recursively divided into two halves that is from A*p…………q+ and then from A*q+1…………r+. We
can merge these and combine the list in order to get data in sorted order.
In various comparison based sorting algorithm, the worst case complexity is O (n 2). This is because
an element is compared multiple times in different passes. For example, In Insertion sorts, while
loop runs maximum times in worst case. If array is already sorted in the decreasing order, then in
worst case while loop runs 2+3+4+------n times in each iteration. Hence, we can say that the
running time complexity of Insertion Sort in worst case is O (n2).
Using Divide and Conquer approach, we are dividing the array into two sub-arrays. Due to the
division focus shift on the half of the elements. Now, number of comparison reduced in context to
comparison based sorting algorithm. Hence, we can say that the complexity of the merge sort will
be less than the complexity of comparison based sorting algorithms. In Merge sort, we divide the
elements into two sub-arrays on the basis of the size of the array. In Quick Sort, division is done
into two sub-arrays on the basis of the Pivot Element.
How Merge Sort Works: The Merge Sort algorithm has two procedures. These are calling of
Merge function and merging procedure. Let’s us understand the merging procedure before going
towards calling procedure of Merge Sort.
The first step is to divide the array into two halves- left and right sub arrays.
Repeative recursive calls are made in second steps.
This division will be done till the size of array is 1.
As soon as each sub array contain 1 element, merge procedure is called.
The merge procedure combines these subs sorted arrays to have a final sorted array.
Pseudo code for performing the merge operation on two sorted arrays is as
follows:
ALGORITHM Merge(A[ ], p, q, r)
BEGIN:
1. n1 = q-p+1
2. n2 = r-p
3. Create Arrays L[n1+1], R[n2+1]
4. FOR i = 1 TO n1 DO
5. L[i] = A[p+i-1]
6. FOR j = 1 TO n2 DO
7. R[j] = A[q+j]
8. L*n1+1+ = ∞
9. R*n2+1+ = ∞
10. i = 1
11. j = 1
12. FOR k=p TO r DO
13. IF L[i] <= R[j] THEN
14. A[k]=L[i]
15. i=i+1
16. ELSE A[k] = R[j]
17. j=j+1
END;
Step 1 and Step 2 is used to divide the array into sub-arrays. In Step 3, we will create left and right
sub-arrays along with the memory allocation. From First three steps, running time complexity is
constant θ(1). In Step 4 and 5, Independent for loop is used for assigning Array elements to the
left sub-array.In Step 6 and 7, Independent for loop is used for assigning Array elements to the
right sub-array. Running Time Complexity of step 4 to step 7 is θ(n 1+ n 2). Step 8 and Step 9 is used
for storing maximum element in left and right sub-array. Step 10 and Step 11 is used to initialize
the variables i and j to 1. Step 12 to Step 17 are used to compare the elements of the left and right
sub-array, keeping the track of i and j variables. After comparison, left and right sub-array
elements are further stored in original array. After this, running time complexity of Merge
Procedure is θ(n).
Let’s take an example where we are sorting the array using this algorithm. Here there would be
different steps which we have shown later. Here we are using one step in order to show how
merging procedure take place:
Step-01: Two sub array Left and Right is created, number of elements contained in Left sub array
is given by index i and number of elements contained in Right sub array is given by index j .
Number of elements contained in Left array will be L[i] = q – p + 1 and number of elements in
right most array will be R[j] = r – q.
Step-02: Initiallyi , j and k will be 0.As element in right array R[0] is 2 which is than L[0] that is 5. So,
A[k] will hold 2.
Step-03: Now j = 1 , i = 0 and k = 1. Now R[j] will point to 3 and L[i] will point to 5, since 3 is less
than 5 A[k] will hold 3.
Step 4:Nowj = 2, k = 2, i = 0. Since R[j] will point to 4 and L[i] is still at 5 so B[2] will now contains 4.
Step-05:Now i = 1, j points to senitel which stores infinity , k = 3. Now A[k] will be 5.Thus, we have-
Step 6:Now i = 2, j points to senitel which stores infinity , k is at 4 th index. Now A[k] will be holding
7.Thus, we have-
Step-07:Now i = 3, j points to senitel which stores infinity , k is at 5th index. Now A[k] will be holding 8.
Thus, we have--
The procedure MERGE-SORT (A, p, r) recursively calls itself until each element reaches to one that
is it cannot be further subdivided. The recursive calling of this procedure is shown by the algo
given below:-
ALGORITHM MergeSort(A[ ],p,r)
BEGIN:
IF p<r THEN
q=⌊ ⌋
MergeSort(A, p, r)
MergeSort(A,q+1,r)
Merge(A,p,q,r)
END;
To sort the entire sequence A = _A[1], A[2], . . . ,A[n], we make the initial call MERGESORT(A, 1,
length[A]), where once again length[A] = n.
In this, p is the first element of the array and r is the last element of the array. In Step 1, we will
check the number of the elements in an array. If number of element is 1 then, it can’t be further
divided.. If number of element is greater than 1 then, it can be further divided. q is the variable
which specifies from where the array is to be divided. Merge sort function (A, p, r) is divided into
two sub function Merge Sort Function(A, p, q) and Merge Sort Function(A,q+1,r). In this, n size
problem is divided into two sub problems of n/2 size. This recursive calling will be done and then
perform the merge operation in order to unite two sorted arrays into a single array.
2.6.6. Complexity Analysis of Merge Sort along with Diagrammatic Flow:
The complexity of merge sort can be analyzed using the piece of information given below:-
• Divide: When we find the middle index of the given array and it is a recursive call. So, finding the
middle index each time will take D(n) = Θ(1) operations.
• Conquer: Whenever we solve two sub-problems, of size n/2 Here n varies from 1 to n/2 as it is a
recursive function.
• Combine: Here we are sorting n elements present in array , so C(n) = Θ(n).
Hence the recurrence for the worst-case running time T (n) of merge sort will be:
T (n) = c If n=1
= 2T (n/2) + c n If n is greater than 1
Where, the constant c represents the time required to solve problems of size 1 as well as the time
per array element of the divide and combine steps.
By applying Case 2 of the master method, we simply say that the running time complexity of
Merge Sort is O (n log n).
Here, recursion tree method is used to calculate the complexity of Merge Sort. In the above
diagrammatic representation, we can see how the array is divided in to two sub-arrays. Cost at
each level is calculated, which is same as cn. Hence, we can say that there are (log n+1) level in
binary tree. At each and every level the cost is same as cn.
Therefore, Total Cost= Cost at each level * number of levels in the binary tree
= c n* (log n+1)
= c n log n+ c n
=c n log n
= θ (n log n)
Hence, running time complexity is θ (n log n).
The time complexity of Merge Sort is order of (n*Log n) in all the 3 cases (worst, average and
best) as the merge sort always divides the array into two halves and takes linear time to merge
two halves.
2.6.7. Comparison of Merge Sort Running Time Complexity with other sorting
algorithm:
1. Comparison of various sorting algorithms on the basis of Time and Space
Complexity:
Similarly, we can define the K way Merge sort in which we can divide the array in to K sub-arrays and then
merge them together to form a sorted array.
Input:24,-18
Output:-18, 24
Time Complexity: In 2 way merge sort, the recurrence obtained are: T(n) = 2T(n/2) + θ (n). By
applying master method on above mentioned recurrence we can compute the running time
complexity of 2 way merge sort which is θ (n log n). This is obtained with the help of Case 2 of the
Master Method. Similarly, in case of 3-way Merge sort the recurrence obtained is: T(n) = 3T(n/3) +
O(n) by solving it using Master method Case 2, we get its complexity as O(n log 3n). Similarly, in
case of 4-way Merge sort the recurrence obtained is: T (n) = 4 T (n/4) + θ(n) by solving it using
Master method Case 2, we get its complexity as θ(n log 4n). Similarly, in case of K-way Merge sort
the recurrence obtained is: T (n) = K T (n/K) + θ(n) by solving it using Master method Case 2, we
get its complexity as θ(n log k n)
2.Tim Sort:
Introduction to Tim Sort: Tim Peter in 2002 derived a sorting algorithm with his name. In this
sorting algorithm, Tim proposed the blended model of two sorting algorithm working together i.e.
Insertion and Merge Sort in an optimal way on real world data. It is an adaptive sorting algorithm
which requires O (n log n) comparisons to sort an array of n elements in average case and worst
case. But in the best case the blended mode of Merge and Insertion sort will take O (n) running
time to sort an array of n elements.
Example 1:
Input Elements is:-2, 7, 15, -14, 0, 15, 0, 7, -7, -4, -13, 5, 8, -14, 12
Output Elements is: -14, -14,-13,-7 ,-4 ,-2 ,0 ,0 ,5 ,7 ,7 ,8 ,12 ,15 ,15
Technique used in Tim Sort: Let us assume that we are given an array of n elements and we have to sort
the given array using Tim Sort. In Tim Sort, division of array is done into several parts on the basis of the
run. These runs will further be sort one by one using insertion Sort and then, in turn will be combined
together in order to form a sorted array using Merge function. Basic idea behind this sorting algorithm is
that Insertion sort works more optimally on the short arrays in comparative to large arrays.
3 10 15 20 21 3 5 10 2 4 5 10 14 16 20
Run 1 Run 2 Run 3
1. Divide the given array into the number of sub-arrays on the basis of the runs.
2. Next Step is to consider the size of the run which can either be 32 or 64.
3. Next Step is to sort each run one by one using Insertion Sort.
4. Then, Merge the sorted runs one by one in such a way using the merge function of the
Merge Sort.
5. After performing Merge function of Merge Sort, we will then combine the sorted sub-
arrays in to the sorted array.
Time Complexity: Time complexity of Tim Sort in best case is better than the running time complexity of
Merge Sort. This is only because of the blended mode used by the Tim sort to sort elements. Blended way
means, Tim Sort is basically a combination of Insertion sort and Merge Sort.
In Bottom-up Merge Sort, we will consider each element of an array as a separate sub-array. Then,
we will merge the pairs of adjacent arrays of 1 element each. Then, merges pairs of adjacent
arrays of 2 elements, next merges pairs of adjacent arrays of 4 elements and so on, until the whole
array is merged.
Input array: 6.4 3.5 7.5 2.5 8.9 4.2 9.2 1.1
There are various real life scenarios where merge sort can be used. In this, we will discuss about
the two scenarios in order to have a depth understanding of the concept.
1. E-commerce website: Most of us might see a section in various e-commerce website” You
might like” in which we found some stuff of our choice .Do you know how it actually work?
E-commerce website maintains an array of its all users with their activities on the website
and when they have some latest products in their products list, which also store them in
another array, will in turn divide those latest products in array according to user’s array of
choice. Another mega example of using merge sorting is two of India’s biggest e-commerce
website policybazar.com and trivago.com who use merge sorting for creating best price
and stuff for its users.
2. Division of Long Loaf of Breads: If you want to divide a long loaf of bread in 8 or 16 equal
pieces, generally people cut it into two equal halves first and then cut each half into two
equal halves again, repeating the process until you get as many pieces as you want – 8, 16,
32, or whatever. Almost nobody tries to divide the loaf into 8 pieces all at once – people
can guess halves much better than eighths.
Following are the two links for better understanding of Merge Sort Concept with the Example:
3 You have to sort 1 GB of data with only 100 MB of available main memory.
Which sorting technique will be most appropriate?
A Heap
B Merge
C Quick
D Insertion
ANS B
4 In a modified merge sort, the input array is splitted at a position one-third
of the length (N) of the array. Which of the following is the tightest upper
bound on time complexity of this modified Merge Sort?
A N( log N base 3)
B N( log N base 2/3)
C N( log N base 1/3)
D N( log N base 3/2)
ANS D
B Insertion
C Selection
D Quick
ANS A
5 For merging two sorted lists of size m and n into sorted list of size
m + n, we require comparisons of
GATE 1995
A O (n )
B O (m )
C O (n +m )
D O (log n + log m)
ANS C
6 Given Log n sorted list each of size n/ Log n. What is the total time
required to merge them into a single list of size n
GATE 2016
A O (n )
B O (log n )
C O (n log n )
D O (n log log n )
ANS D
2.6.11. Competitive Coding Problems on Merge Sort:Solved
Step 1: Problem Statement: Given an array arr[] consisting of N integers, the task is to count
the number of greater elements on the right side of each array element.
Step3: Naive Approach: The simplest approach is to iterate all the array elements using two loops
and for each array element, count the number of elements greater than it on its right side and
then print it.
Time Complexity: O (N2) and Auxiliary Space: O(1)
Optimized Approach: The problem can be solved using the concept of Merge sort in descending
order. Follow the step by step algorithm to solve the problem:
1.Initialize an array count[] where count[i] store the respective count of greater elements
on the right for every arr[i]
3:If higher index element is greater than the lower index element then, the entire higher
index element will be greater than all the elements after that lower index.
4:Since the left part is already sorted, add the count of elements after the lower index
element to the count[] array for the lower index.
Coding Problem 2:Count pairs (i, j) from given array such that i < j and arr[i] > K *
arr[j]
Step 1: Problem Statement:Given an array arr[] of length N and an integer K, the task is to count
the number of pairs (i, j) such that i < j and arr[i] > K * arr[j].
Step3: Naive Approach: The simplest approach to solve the problem is to traverse traverse an
array and for every index, find numbers having indices greater than it, such that the element in it
when multiplied by K is less than the element at the current index.
1. Initialize a variable, say count, with 0 to count the total number of required pairs.
2. Traverse the array from left to right.
3. For each possible index, say i, traverse the indices i + 1 to N – 1 and increase the value of
count by 1 if any element, say arr[j], is found such that arr[j] * K is less than arr[i].
4. After complete traversal of the array, print countas the required count of pairs.
1. Initialize a variable, say answer, to count the number of pairs satisfying the given
condition.
2. Repeatedly partition the array into two equal halves or almost equal halves until one
element is left in each partition.
3. Call a recursive function that counts the number of times the condition arr[i] > K *
arr[j] and i < j is satisfied after merging the two partitions.
4. Perform it by initializing two variables, say i and j, for the indices of the first and second
half respectively.
5. Increment j till arr[i] > K * arr[j] and j < size of the second half. Add (j – (mid + 1)) to the
answer and increment i.
6. After completing the above steps, print the value of answer as the required number of
pairs.
Coding Problem 3:Count sub-sequences for every array element in which they are
the maximum
Step 1: Problem Statement:Given an array arr[] consisting of N unique elements, the task is to
generate an array B[] of length N such that B[i] is the number of subsequencesin which arr[i] is the
maximum element.
Input:arr[] = {2, 3, 1}
Output: {2, 4, 1}
Explanation: Subsequences in which arr[0] ( = 2) is maximum are {2}, {2, 1}.
Subsequences in which arr[1] ( = 3) is maximum are {3}, {1, 3, 2}, {2, 3}, {1, 3}.
Subsequence in which arr[2] ( = 1) is maximum is {1}.
Step 3: Optimized Approach: The problem can be solved by observing that all the subsequences
where an element, arr[i], will appear as the maximum will contain all the elements less than arr[i].
Therefore, the total number of distinct subsequences will be 2(Number of elements less than arr[i]).
Steps to solve the problem Optimized Approach
1. Sort the array arr[] indices with respect to their corresponding values present in the given
array and store that indices in array indices[], wherearr[indices[i]] <arr[indices[i+1]].
2. Initialize an integer, subsequence with 1 to store the number of possible subsequences.
3. Iterate N times with pointer over the range [0, N-1] using a variable, i.
a. B[indices[i]] is the number of subsequences in which arr[indices[i]] is maximum
i.e., 2i, as there will be i elements less than arr[indices[i]].
b. Store the answer for B[indices[i]] as B[indices[i]] = subsequence.
c. Update subsequenceby multiplying it by 2.
4. Print the elements of the arrayB[] as the answer.
Coding Problem 1:Count sub-sequences which contains both the maximum and
minimum array element.
Difficulty Level: Moderate
Problem Statement: Given an array arr[] consisting of N integers, the task is to find the number of
subsequences which contain the maximum as well as the minimum element present in the given
array.
Coding Problem 2: Partition array into two sub-arrays with every element in the
right sub-array strictly greater than every element in left sub-array
Difficulty Level: Moderate
Problem Statement: Given an array arr[] consisting of N integers, the task is to partition the array
into two non-empty sub-arrays such that every element present in the right sub-array is strictly
greater than every element present in the left sub-array. If it is possible to do so, then print the
two resultant sub-arrays. Otherwise, print “Impossible”.
Coding Problem 3: Count Ways to divide C in two parts and add to A and B to
make A strictly greater than B
Difficulty Level: Moderate
Problem Statement: Given three integers A, B and C, the task is to count the number of ways to
divide C into two parts and add to A and B such that A is strictly greater than B.
2.7. Quick Sort:
2.7.1. Analogy: Arranging students with different heights: This is an analogy of quick sort
as any one student here looking for its appropriate place can be a pivot. Student’s smaller than
him will stand ahead of him and taller behind him. In the same way when a single person has
found it’s exact place the other in the same manner can be the pivot and find it’s place. Leading to
a very quick way of line formation according to heights.
Quick Sort is another sorting algorithm following the approach of Divide and Conquer. Another name
of quick sort is Partition exchange sort because of the reason, it selects a pivot element and does the
array elements partitioning as per that pivot. Placing element smaller than pivot to the left and
greater than pivot to the right.
Quick Sort is also known as Selection exchange sort because in selection sort we select the position
and find an element for that position whereas in Quick Sort we select the element and finding the
position. As compared to Merge Sort if the size of input is small, quick sort runs faster.
1. Commercial applications generally prefer quick sort, since it runs fast and no additional
requirement of memory.
2. Medical monitoring.
3. Monitoring & control in industrial & Research plants handling dangerous material.
4. Search for information
5. Operations research
6. Event-driven simulation
7. Numerical computations
8. Combinatorial search
9. When there is a limitation on memory then randomised quicksort can be used. Merge sort is not
an in-place sorting algorithm and requires some extra space.
10. Quicksort is part of the standard C library and is the most frequently used tool - especially for
arrays. Example: Spread sheets and database program.
Generally, in merge sort we use to divide the array into two-halves but in quick sort division is done
on the basis of pivot element which could be either first, last element or any random element.
Steps to divide an array into sub-arrays on the basis of the Pivot Element:
Divide: Initially, we divide Array A into two sub-arrays A*p……q-1+ and A *q+1……..r+. A*q+ returns the
sorted array element. In line 2 we get q. Every element in A*p…………q-1] are less than or equal to
A*q+. Every element in A*q+1……………r+ is greater than A*q+.
Partitioning Algorithm:
ALGORITHM Partition(A[ ], p, r)
BEGIN:
x = A[r]
i = p-1
FOR j = p TO r-1 DO
i = i+1
RETURN i+1
END;
2.7.4.Example to demonstrate working of Quick Sort:
After first partition call the index of i+1 will be returned which is 4 for the above example.
In same manner, QuickSort partition function will be called once for Array 1 and once for Array 2. For
Array 1, QuickSort (A, 1, 3) and for Array 2, QuickSort(A, 5, 8) will be passed for partition function.
The algorithm is: Considering p as the index of first element and r as the index of last element:
ALGORITHM QuickSort(A[ ], p, r)
BEGIN:
IF p<r
q = Partition (A, p, r)
QuickSort(A, p, q-1)
QuickSort(A, q+1, r)
END;
Step by step Complexity analysis of Partition Algorithm:
ALGORITHM Partition(A, p, r)
x = A[r]................................................................................... C1 1
i = p-1...................................................................................... C2 1
i = i+1............................................................................. C5 n
RETURN i+1................................................................................C8 1
END;
F (n) = a(n) +b
So, we can say that time complexity: θ(n) for partition algorithm. The space complexity of partition
algorithm will be θ(1), as 5 extra variables are required.
In the performance analysis of QuickSort selection of pivot element plays an important role. This can be
classified as 3 cases in the form of input given.
Case 1: a) When the input array is sorted in ascending order. Such a case experiences
an unbalanced array split, with (n-1) elements on one side of an array and one as a
sorted element (pivot element).
Case 1: b) When the input array is sorted in descending order. Given example below:
Here, worst case complexity of QuickSort can be explained either by the Recurrence relation or by
iterative approach.
Step 1: Recurrence Method: Following Recurrence will be obtained for the unbalanced partitioning
which occurs when the array is either in ascending order or descending order, that is, T(n) = T(n-1) +
O(n). With the help of this recurrence we can simply say that running time complexity will be O(n2).
Step 2: Iteration Method: In this method, we will compute the cost at each level of the tree. For
example, at 0th level the cost is n, at 1st level the cost is (n-1), and at 2nd level the cost is (n-2) and so
on. On the basis of this we derive the cost at all the levels of the tree, which is equal to [n + (n-1) +
(n-2) + (n-3) + …………… +2+ = (n(n+1)/2 -1] = O(n2) Worst case complexity.
Case 2: When the input array splits from the middle and partition occurs evenly from
the middle at each level.
Suppose we have an array of 15 numbers A[1:15]. The best case is possible if:
At each and every level when input array is divided into two equal halves, so, we can simply say that cost at
each level of the binary tree is n and the total number of levels is log n+1. Now, we can say that the running
time complexity in the best case of quick sort will be equal to cost at each level multiplied by number of levels
== n * (log n+1). Hence, we can conclude the complexity will be Ω(nlog n). Recurrence derived from the
above mentioned approach is as follows: T(n)= 2T(n/2) + Θ(n) where 2T(n/2) is the cost for dividing the array
into sub-arrays and n is the cost of merging the two sub-arrays. We can solve this Recurrence either by the
Master Method or by Recursion Tree Method. After applying any of the method we can obtain best case
complexity as O(nlog n).
Let us suppose the problem size n=36. By applying QuickSort algorithm we can partition these 36 elements
into 3 ways on the basis of best case, worst case and average case. In best case, the equal elements are
available on both the sides. In worst case, maximum elements appear from one side, whereas in average case,
the number of elements that appear on the left side may range from 8-16 and number of element that appear
on right may range from 20-28 and vice-versa. On the basis of this division of elements on left and right hand
side we can derive various types of Recurrences which reflects the average case complexity of the Quick Sort.
The auxiliary space required by the Quick Sort Algorithm is O (n) for the call stack in the worst case.
1. Three way Quick Sort: In Three Way Quick Sort, Array A= [1….n] is divided in 3 parts: a)
Sub-array *1…..i] elements less than pivot. b) Sub-array [i+1…..j] elements equal to pivot. c) Sub-
array [j+1…..r] elements greater than pivot. Running Time Complexity is θ(n) and Space
Complexity is θ(1).
Partitioning Algorithm of Three Way Quick Sort:
ALGORITHM Partition (arr[ ], left, right, i, j)
BEGIN:
IF right – left <= 1THEN
If arr[right] <arr[left] THEN
Swap arr[right] and arr[left]
i = left
j = right
RETURN
Mid = left
pivot = arr[right]
WHILE mid <= right DO
IF arr[mid] < pivot THEN
Swap arr[left], arr[mid]
Left=left+1
Mid=Mid+1
ELSE
IF arr[mid] = pivot THEN
mid=mid+1
ELSE
Swap arr[mid] and arr[right]
right =right - 1
i = left – 1
j = mid
END;
Three Way Quick Sort Algorithms:
ALGORITHM QuickSort (arr[ ], left, right)
BEGIN:
IF left >= right THEN
RETURN
Define i and j
Partition(arr, left, right, i, j)
Quicksort(arr, left, i)
Quicksort(arr, j, right)
END;
Examples:
1.Input: Array [] = {12, 3, 5, 7, 4, 19, and 26}
Output: 7
The size of an array is 7. Hence the median element will be one that is available at 4 th Position i.e. 7.
Naive Approach:
1. Sort the array in increasing order.
3.If the number of elements in array is even, median is average of array [n/2] and array [n/2+1].
2. If after the previous step, the position of the chosen pivot is the middle of the array then it is the
required median of the given array.
3. If the position is before the middle element then repeat the step for the sub-array starting from
previous starting index and the chosen pivot as the ending index.
4. If the position is after the middle element then repeat the step for the sub-array starting from the
chosen pivot and ending at the previous ending index.
5. In case of even number of elements, the middle two elements have to be found and their average
will be the median of the array.
The new partitioning procedure simply implemented the swap before actually partitioning.
i = Random(p, r)
Exchange A[p] with A[i]
RETURN PARTITION(A, p, r)
END;
Now Randomized Quick Sort will call the above procedure in place of PARTITION
IF p < r THEN
q = RandomizedPartition (A, p, r)
END;
This algorithm is used for selecting any random element as the pivot element.
Randomized Quick Sort has the expected running time complexity as θ (n log n) but in the worst case
the time complexity will remain as same.
1. Eating apples:
Problem: You are staying at point (1, 1) in a matrix 109×109. You can travel by following these
steps:
1. If the row where you are staying has 1 or more apples, then you can go to another end of
the row and can go up.
2. Otherwise, you can go up.
The matrix contains N apples. The ith apple is at point (xi, yi). You can eat these apples while
traveling. For each of the apples, your task is to determine the number of apples that have been
eaten before.Practice Problem (hackerearth.com)
2. Specialty of a sequence:
Problem: You are given a sequence A of length n and a number k. A number A[l] is special if there
exists a contiguous sub-array that contains exactly k numbers that are strictly greater than A[l].
The specialty of a sequence is the sum of special numbers that are available in the sequence. Your
task is to determine the specialty of the provided sequence.
Practice Problem (hackerearth.com)
3. Noor and his pond:
Problem: Noor is going fish farming. There are N types of fish. Each type of fish has size(S) and
eating Factor(E). A fish with eating factor of E, will eat all the fish of size ≤E.Help Noor to select a
set of fish such that the size of the set is maximized as well as they do not eat each other.
Practice Problem (hackerearth.com)
4. Card game:
Problem: Two friends decided to play a very exciting online card game. At the beginning of this
game, each player gets a deck of cards, in which each card has some strength assigned. After that,
each player picks random card from his deck and them compare strengths of picked cards. The
player who picked card with larger strength wins. There is no winner in case both players picked
cards with equal strength.First friend got a deck with n cards. The i-th his card has strength ai.
Second friend got a deck with m cards. The i-th his card has strength bi.
First friend wants to win very much. So he decided to improve his cards. He can increase by 1 the
strength of any card for 1 dollar. Any card can be improved as many times as he wants. The second
friend can't improve his cards because he doesn't know about this possibility.
What is the minimum amount of money which the first player needs to guarantee a victory for
himself?
5. Missing Number Problem: You are given an array A. You can decrement any element of the
array by 1. This operation can be repeated any number of times. A number is said to be missing if
it is the smallest positive number which is a multiple of 2 that is not present in the array A. You
have to find the maximum missing number after all possible decrements of the elements.
1. In Quicksort while sorting integers in increasing order, if a1 and a2 are the comparison
count for the provided inputs {6, 7, 8, 9, 10} and {9, 6, 10, 8, 7}. What situation will hold
from the following, when pivot is the first element of the array.GATE 2014
A a1 = 5
B a1 < a2
C a1 > a2
D a1 = a2
ANS C
2. What is recurrence for worst case of Quick Sort and state it’s time complexity in worst
case.
A Recurrence is T(n) = T(n-2) + O(n) and time complexity is O(n2)
B Recurrence is T(n) = T(n-1) + O(n) and time complexity is O(n2)
C Recurrence is T(n) = 2T(n/2) + O(n) and time complexity is O(nlog n)
D Recurrence is T(n) = T(n/10) + T(9n/10) + O(n) and time complexity is O(nlog n)
ANS B
3. Suppose we have a O(n) time algorithm that finds median of an unsorted array. Now
consider a QuickSort implementation where first median is found using above algorithm,
then use median as pivot. What will be the worst case time complexity of this modified
Quick Sort
A O(n2log n)
B O(n2)
C O(n log nlog n)
D O(nlog n)
ANS D
4. From the given sorting which is not a stable sort in its typical implementation?
A Insertion Sort
B Merge Sort
C Quick Sort
D Bubble Sort
ANS C
5. Suppose we are sorting an array of eight integers using QuickSort, after finishing just the
first partitioning with array elements be like: { 3, 6, 2, 8, 10, 13, 12, 11}
A The pivot could be either 8 or 10
B The pivot could be 8, but not 10
C The pivot is not 8, but could be 10
D Neither 8 nor 10 is the pivot
ANS A
6. In QuickSort, for sorting n elements, the (n/4)th smallest element is selected as pivot
using an O(n) time algorithm. What is the worst case time complexity of the QuickSort?
(A)O(n) (B) O(nlog n) (C) O(n2) (D) O(n2log n) GATE 2009
A A
B B
C C
D D
ANS B
7. Consider QuickSort algorithm. Suppose there is a procedure for finding a pivot element
which splits the list into two sub-lists each of which contains at least one-fifth of the
elements. Let T(n) be number of comparisons required to sort n elements. GATE 2008
A T(n) < = 2T(n/5) + n
B T(n) < = T(n/5) + T(4n/5) + n
C T(n) < = 2T(4n/5) + n
D T(n) < = 2T(n/2) +n
ANS B
8. You have an array of n elements. Suppose you implement QuickSort by always choosing
the central element of the array as the pivot. Then the tightest upper bound for the
worst case performance is: GATE 2014
A O(n2)
B O(nlog n)
C Θ(nlog n)
D O(n3)
ANS A
10. Which of the following changes to typical QuickSort improves its performances on
average and are generally done in practice?
1) Randomly picking up to make worst case less likely to occur.
2) Calling insertion sort for small sized arrays to reduce recursive calls.
3) QuickSort is tail recursive, so tail call optimizations can be done.
4) A linear time median searching algorithm is used to pick the median, so that the
worst case time reduces to O(nlog n) GATE 2015
A 1 and 2
B 2, 3 and 4
C 1, 2 and 3
D 2, 3 and 4
ANS C
11. Which one of the following is the recurrence equation for the worst case time complexity
of the QuickSort algorithm for sorting n(>=2) numbers? In the recurrence equations given
in the options below, c is a constant GATE 2015
A T(n)= 2T(n/2) +cn
B T(n) = T(n-1) + T(0) + cn
C T(n) = 2T(n-2) + cn
D T(n) = T(n/2) + cn
ANS B
12. What is the best sorting algorithm to use for the elements in array are more than 1
million in general?
A Merge Sort
B Bubble Sort
C Quick Sort
D Insertion Sort
ANS C
13. QuickSort is run on 2 inputs shown below to sort in ascending order taking first element
as pivot, 1) 1, 2, 3, …………., n
2) n, n-1, n-2, ……………., 2, 1
Let C1 and C2 be the number of comparisons made for the inputs 1) and 2) respectively.
Then, GATE 1996
A C1<C2
B C1>C2
C C1=C2
D We cannot say anything for arbitrary n
ANS C
14. Consider the following array 35 50 15 25 80 20 90 45.How the array looks like after the
first pass if it applies a quick sort on the array.
A 25 20 15 35 80 50 90 45
B 25 15 20 35 45 50 80 90
C 15 20 45 35 25 50 80 90
D 25 20 15 35 80 50 90 45
ANS A
15. The recurrence relation to solve the Quick sort algorithm in the average case will be?
A T(n) = 2T(n/2) + n
B T(n) = T(n-1) + log n
C T(n) = T(n/4) + n
D T(n) = T(n-1) + 1
ANS A
16. The recurrence relation to solve the Quick sort algorithm in the worst case will be?
A T(n) = T(n-1) + n
B T(n) = T(n-1) + log n
C T(n) = T(n/4) + n
D T(n) = T(n-1) + 1
ANS A
17. How much time complexity will be required by the partition algorithm in one iteration to
sort the element in the case of quick sort ?
A O(n)
B O(1)
C O(log n)
D O(n^2)
ANS A
18. When does the worst case occur in the quick sort algorithm?
19. When does the best case occur in the quick sort algorithm?
A Pivot is at ⅓ rd position
B Pivot is randomly selected
C Pivot is always the middle element
D Pivot element is the smallest or the largest element
ANS C
20. When pivot is the middle element, we are able to reduce a problem of size N to two
problems of size (N-1)/2 and (N-1)/2 results in ? (Considering quick sort)
A Best case
B Worst case
C Average Case
D None of the above
ANS A
21. When pivot is the smallest or largest element, we are able to reduce a problem of size N
to two problems of size 1 and (N-1) results in? (Considering quick sort)
A Best case
B Worst case
C Average Case
D None of the above
ANS B
22. To guarantee achieving the complexity of quick sort as O(n log n). Which method must be
selected for picking the pivot element?
A Choose the middle element
B Choose the left most or the rightmost element
C Median of three
D Median of the medians
ANS D
2.8 Counting Sort
Counting Sort, as the name suggests, must be counting something to sort the elements. The
situation wherein we are working with the data set having a very short range, counting sort may
be handy. Usually we deal with Direct address table while doing the sorting with the Counting
Sort. Let us first see what the Direct Address Table is.
2.8.1.Direct Address Table
DAT is A data structure for mapping records to their corresponding keys using arrays.
Records are placed using their key values directly as indexes.
Problem Statement: Given An array of integers (Range 1:10) , Find which of the elements are
repeated and which are not.{4,3,1,2,5,7,1,6,3,2,4,1,8,10}
To solve this problem, let us take the Direct address table of size 10. Each of the elements in this
table are initialized to zero.
1 2 3 4 5 6 7 8 9 10
0 0 0 0 0 0 0 0 0 0
For Input Key: 4 we will increase a count at that index.
1 2 3 4 5 6 7 8 9 10
1
For Input Key: 3 we will increase a count at that index.
1 2 3 4 5 6 7 8 9 10
1 1
For Input Key: 1 we will increase a count at that index.
1 2 3 4 5 6 7 8 9 10
1 1 1
For Input Key: 2 we will increase a count at that index.
1 2 3 4 5 6 7 8 9 10
1 1 1 1
For Input Key: Similarly we will get the Final Array as:
1 2 3 4 5 6 7 8 9 10
3 2 2 2 1 1 1 1 0 1
The values in the output array having values greater than 1 are the one having frequency more
than one.
So far all the sorting methods we have talked about works on the principle of comparing two
numbers whether they are equal, smaller or larger. Counting sort algorithms rely solely on non-
comparison approach. In counting sort basically works on the principle of counting the occurrence
of the elements to be sorted. This sorting algorithm works faster having a complexity of linear
time without making any comparison between two values. It is assumed the numbers we want to
sort are in range from 1 to k where the value of k small. The main idea is to find the rank of each
value in the final sorted array. Counting sort is not used frequently because there are some
limitations that make this algorithm impractical in many applications. However if the input data is
in small range then this algorithm has a distinct advantage. As it is the only algorithm that sorts
the elements in order of (n) Complexity. This is also a stable sort algorithm. Many of the
comparison sort algorithms sorts in quadratic time (θ(n²)), The exceptions – Merge, Heap and
Quick Sort algorithms sorts elements in (θ(n log n)) time . We rarely use Counting Sort but if the
requirements are fulfilled then it proves to be the best algorithm in choice.
2.8.3.Limitations:-
ALGORITHM CountingSort(A[ ], B[ ], k)
BEGIN:
FOR i = 0 TO k DO
c[i] = 0
FOR j = 1 to length[A] DO
C[A[j]] = C[A[j]] + 1
//C [i] now contains the number of elements equal to i
FOR i = 1 TO k DO
C[i] = C[i] + C[i-1]
//C [i] now contains the number of elements less than or equal to i
FOR j = length[A] to 1 STEP -1 DO
B[C[A[j]]] = A[j]
C[A[j]] = C[A[J]] – 1
END;
Running time of Counting Sort
The first for loop takes time Θ(k), the second for loop takes time Θ(n), third for loop takes time
Θ(k), and the fourth for loop takes time Θ(n). Thus, the overall time is Θ(k+n). In practice, we
usually use counting sort when we have k = O(n), in which case the running time is Θ(n).
Some of the Applications of counting sort algorithm are:
1. We can apply the counting sort to sort the data lexicographically whether the data may be
in form of punch cards, words, integers, or mails.
2. We can apply counting sort in the area of parallel computing.
a) We can also apply counting sort in the field of molecular biology, data compression
and finding plagiarism it has the capabilities of finding redundant data
3. Radix sort is quite useful for very large in-memory sorts in a multiprocessor or cluster.
Imagine that the entire dataset is in ram, distributed across a cluster. Each node sorts
locally, based on the first digit of the key, The nodes then exchange data (what in MPI is
called an ALL TO ALL collective) so that the data is distributed across the machine but with
the first digit in overall order. Then the nodes move to the next key digit. Repeat until
done..
4. Suppose you want to implement medals tally counter country wise. Gold has more priority
than silver and silver has more priority than bronze irrespective of the count of each of the
individual type.
2.8.4.Example on Counting Sort:
2.8.5..Objective Questions:
1 Q1. How many comparisons will be made to sort the array arr={1, 5, 3, 8, 2} using
counting sort?
A 5
B 7
C 9
D 0
ANS D
5 It is not possible to implement counting sort when any of the input element has
negative value.
A TRUE
B FALSE
ANS B
6 If we use Radix sort to sort n integers in the range (n k/2 , nk ) for some value k>0 which is
independent of n , the time taken would be :
A O(n)
B O(kn)
C O(nlog n)
D O(n2)
ANS B
7 Following algorithm can be used to sort n integers in the range *1…..n 3] in O(n) time :
A Heap Sort
B Quick Sort
C Merge Sort
D Radix Sort
ANS D
2.8.6.Competitive Coding– Problem Statement-
Imagine a situation where heights of students are given. Your task is to find out any positive
number n if possible, by using this number n all the given heights becomes equal by using any of
these given operations
1) Adding number n in given heights, not necessary to add in all heights
2) Subtracting number n in given heights, not necessary to subtract in all heights
3) No operation perform on given heights
Example-
12 5 12 19 19 5 12
Let us suppose that value of n=7
If addition operation perform on 5 then = 5+7=12
If subtraction operation perform on 19 then = 19-7=12
Perform no operation on 12
Now heights becomes
12 12 12 12 12 12 12
Input format
Print single line height becomes equal or height not becomes equal
1-Identify problem statement
Read the story and try to convert it into technical form. For this problem reframes as-
Store the heights in an array and read a number n and try to perform given operations.
1<N<100
Sample input sample output heights becomes equal
7
12 5 12 19 19 5 12
7
Design Logic
1 – Take an auxiliary array and store all the unique heights in this array.
2 – Count unique heights and store it in a variable c
IF c==1 THEN
WRITE(“Height becomes equal”)
RETURN 0
IF c==2 THEN
WRITE(“Height becomes equal”)
RETURN h1-h2
//two unique heights let h1 and h2 and also h1>h2
IF c==3 THEN
IF h3-h2==h2-h1 THEN //h1<h2<h3
WRITE(“Height becomes equal”)
RETURN h3-h2
ELSE
WRITE(“Height did not become equal”)
RETURN
Time Complexity-θ(n)
2.8.7.Un solved Coding problem:
1. https://www.hackerearth.com/practice/algorithms/sorting/counting-sort/practice-
problems/algorithm/shil-and-birthday-present/
2. https://www.hackerearth.com/practice/algorithms/sorting/counting-sort/practice-
problems/algorithm/finding-pairs-4/
2.9.RADIX SORT
The major problem with counting sort is that when the range of key elements is very high it does
not work efficiently as we have to increase the size of auxiliary array and sorting time is high. In
such input, Radix sort proves to be the better choice to sort elements in linear time. In Radix Sort
we used to sort every digit hence the complexity is O(nd). This algorithm is fastest and most
efficient when we talk about linear time sorting Algorithms. It was basically developed to sort
large range integers.
2.9.1Analogy:- If you want to sort the 32 bit numbers, then the most efficient
algorithm will be Radix Sort.
2.9.2.Radix Sort Algorithm
Observe the image given below carefully and try to visualize the concept of this algorithm.
1. Problem Statement:
We have already studied about many sorting techniques such as Insertion sort, Bubble sort,
Selection sort, Quick sort, Merge sort, Heap sort etc. Here I will talk about a different type of
sorting technique which is called as “Radix Sort” and is probably the best sorting technique as far
as time complexity is concerned.
Operations which are performed in Radix Sort is as follows:-
1) Do following for each digit i where i varies from least significant digit to the most significant
digit.
a) Sort input array using counting sort (or any stable sort) according to the ith digit.
Example
Original Unsorted List 170, 45, 75, 90, 802, 24, 2, 66
Sorting by least significant digit (1s place) gives:
[Notice that we keep 802 before 2, because 802 occurred before 2 in the original list, and similarly for
pairs 170 & 90 and 45 & 75.]
170, 90, 802, 2, 24, 45, 75, 66
Sorting by next digit (10s place) gives: [*Notice that 802 again comes before 2 as 802 comes
before 2 in the previous list.]
802, 2, 24, 45, 66, 170, 75, 90
Sorting by most significant digit (100s place) gives:
2, 24, 45, 66, 75, 90, 170, 802
Hence we get a sorted sequence for the corresponding random sequence.
For a given set of N random numbers, generate a sorted (non-decreasing order) sequence using
above discussed technique
2. Radix Sort | CodeChef: Given n strings, how to output their order after k phases of the radix sort
Problem
You are given n strings. Output their order after k phases of the radix sort.
Input
The first line of the input file contains three integer numbers: n, the number of strings, m, the
length of each string, and k, the number of phases of the radix sort.
(1<=n<=106,1<=k<=m<=106,n∗m<=5∗107)
Then the description of the strings follows. The format is not trivial. The i-string (1<=i<=n) is
written as the ith symbols of the second, …, (m+1)th lines of the input file. In simple words, the
strings are written vertically. This is made intentionally to reduce the running time of your
programs. If you construct the strings from the input lines in your program, you are doing the
wrong thing.
The strings consist of small Latin letters, from "a" to "z" inclusively. In the ASCII table, all these
letters go in a row in the alphabetic order. The ASCII code of "a": 97, of "z" : 122.
Output
Print the indices of the strings in the order these strings appear after kk phases of the radix sort.
Example:
Input
331
bab
bba
baa
Output
231
In all examples the following strings are given:
"bbb", with index 1;
"aba", with index 2;
"baa", with index 3.
Consider the first example. The first phase of the radix sort will sort the strings using their last
symbol. As a result, the first string will be "aba" (index 2), then "baa" (index 3), then "bbb" (index
1). The answer is thus "2 3 1".
algorithms - Given n strings, how to output their order after k phases of the radix sort (huge
constraints)? - Computer Science Stack Exchange
3. Descending Weights
Problem
You have been given an array A of size N and an integer K. This array consists of N integers ranging
from 1 to 107. Each element in this array is said to have a Special Weight. The special weight of an
element a[i] is a[i]%K.
You now need to sort this array in Non-Increasing order of the weight of each element, i.e the
element with the highest weight should appear first, then the element with the second highest
weight and so on. In case two elements have the same weight, the one with the lower value
should appear in the output first.
Input Format:
The first line consists of two space separated integers N and K. The next line consists
of N space separated integers denoting the elements of array A.
Output Format:
Print N space separated integers denoting the elements of the array in the order in which
they are required.
Constraints:
1≤N≤105
1≤A*i+≤107
1≤K≤107
Note: You need to print the value of each element and not their weight.
Sample Input
52
12345
Sample Output
13524
2.10. Bucket Sort or Bin Sort
2.10.1 Analogy:
Bucket sort or Bin Sort is a sorting algorithm that works by distributing the elements of an array
into different buckets uniformly. After distribution each bucket is sorted independently using any
sorting algorithms or recursively or by recursively applying bucket sort on each bucket.
Why Bucket Sort?
1. After distribution of elements into bucket array size become smaller and can be solve each
bucket independently.
2. Each bucket can be solved in parallel.
3. Bucket sort solve fractional numbers efficiently.
4. Bucket Sort is not in place sorting algorithm.
ALGORITHM Bucket Sort (arr [ ], n) // n is length of array
BEGIN:
Create n bucket with NULL value. // initialize empty bucket
FOR i=0 TO n-1 DO // start loop from first element to last element
bucket[ n*array[i]] = arr[i] // enter element into respective Bucket
Sort each bucket using insertion sort or any other sort.
Concatenate all sorted buckets.
END;
Concatenated Bucket is the sorted buckets
Complexity:
1. Time Complexity:
a. Average Case complexity is θ(n)
b. Best case complexity is Ω(n)
c. Worst case time complexity is O(n 2)
2. Space Complexity of bucket sort is θ (n + k). n is number of elements and k is number of
buckets.
Cases:
1. Average case & Best Case: when all element distribution is uniform in buckets.
2. Worst Case: when n or approximate n elements lie in one Bucket. Then it will work as
insertion sort.
Example 1.
0.57 0.23 0.54 0.12 0.45 0.62 0.28 0.16 0.49 0.35 0.01 0.9
0.57 0.23 0.54 0.12 0.45 0.62 0.28 0.16 0.49 0.35 0.01 0.9
0.57 0.23 0.54 0.12 0.45 0.62 0.28 0.16 0.49 0.35 0.01 0.9
0.57 0.23 0.54 0.12 0.45 0.62 0.28 0.16 0.49 0.35 0.01 0.9
4th Element B[int 12*0.12] <- 0.12
B[1]=0.12
0.57 0.23 0.54 0.12 0.45 0.62 0.28 0.16 0.49 0.35 0.01 0.9
0.57 0.23 0.54 0.12 0.45 0.62 0.28 0.16 0.49 0.35 0.01 0.9
0.57 0.23 0.54 0.12 0.45 0.62 0.28 0.16 0.49 0.35 0.01 0.9
0.57 0.23 0.54 0.12 0.45 0.62 0.28 0.16 0.49 0.35 0.01 0.9
0.57 0.23 0.54 0.12 0.45 0.62 0.28 0.16 0.49 0.35 0.01 0.9
0.57 0.23 0.54 0.12 0.45 0.62 0.28 0.16 0.49 0.35 0.01 0.9
0.01
0.01 0.12
0.01 0.12 0.16 0.23 0.28 0.35 0.45 0.49 0.54 0.57
0.01 0.12 0.16 0.23 0.28 0.35 0.45 0.49 0.54 0.57 0.62
0.01 0.12 0.16 0.23 0.28 0.35 0.45 0.49 0.54 0.57 0.62 0.9
Algorithm:
1. Find Max and min element of array.
2. Calculate the range of each bucket
Range = (max+ min) / n // n is the number of bucket
3. Create n Buckets of Calculated Range
4. Distribute the elements in the buckets.
5. Bucket index=( arr[i] – min) / range
6. Now Sort each bucket individually.
7. Concatenate the sorted elements from buckets to original array.
Example:
Input array 9.6, 0.5, 10.5, 3.04 , 1.2 , 5.4 , 8.6 , 2.47, 3.24 , 1.28 and number of bucket = 5
Max=10.5
Min=0.5
Range=(10.5-0.5)/5 = 2
9.6 0.5 10.5 3.04 1.2 5.4 6.6 2.47 3.24 2.28
1 If we use Radix Sort to sort n integers in the range (nk/2 , nk), for some k>0 which is
independent of n, the time taken would be. (GATE 2008)
A O(n)
B O(kn)
C O(nlogn)
D O(n2)
ANS B
2 How many comparisons will be made to sort the array arr={1, 5, 3, 8, 2} using bucket
sort?
A 5
B 7
C 9
D 0
ANS D
6 Which of the following don’t affect the time complexity of bucket sort?
A Algorithm implemented for sorting individual buckets
B number of buckets used
C distribution of input
D input values
ANS D
7 Bucket sort is most efficient in the case when __________
A the input is non-uniformly distributed
B the input is uniformly distributed
C the input is randomly distributed
D the input range is large
ANS B
9 What is the worst case time complexity of bucket sort (k = number of buckets)?
A O(n + k)
B O(n.k)
C O(n2)
D O(n log n)
ANS C
10 What is the best case time complexity of bucket sort (k = number of buckets)?
A O(n + k)
B O(n.k)
C O(n2)
D O(n log n)
ANS A
A bucket sort
B counting sort
C merge sort
D pigeonhole sort
ANS A
12 Bucket sort is an in place sorting algorithm.
A True
B False
ANS B
2.10.6..Animated Link
:https://www.cs.usfca.edu/~galles/visualization/BucketSort.html