Sorting Algorithm
Sorting Algorithm
Topperworld.in
Sorting Algorithm
There are various sorting algorithms are used in data structures. The
following two types of sorting algorithms can be broadly classified:
1. Comparison-based: We compare the elements in a
comparison-based sorting algorithm)
2. Non-comparison-based: We do not compare the elements in
a non-comparison-based sorting algorithm)
Sorting algorithm
Data Structure and Algorithms
1. Selection Sort
Lets consider the following array as an example: arr[] = {64, 25, 12, 22,
11}
First pass:
For the first position in the sorted array, the whole array is traversed
from index 0 to 4 sequentially. The first position where 64 is stored
presently, after traversing whole array it is clear that 11 is the lowest
value.
64 25 12 22 11
Thus, replace 64 with 11. After one iteration 11, which happens to be
the least value in the array, tends to appear in the first position of the
sorted list.
11 25 12 22 64
Second Pass:
For the second position, where 25 is present, again traverse the rest of
the array in a sequential manner.
11 25 12 22 64
11 12 25 22 64
Third Pass:
Now, for third place, where 25 is present again traverse the rest of the
array and find the third least value present in the array.
11 12 25 22 64
While traversing, 22 came out to be the third least value and it should
appear at the third place in the array, thus swap 22 with element
present at third position.
11 12 22 25 64
Fourth pass:
Data Structure and Algorithms
Similarly, for fourth position traverse the rest of the array and find the
fourth least element in the array
As 25 is the 4th lowest value hence, it will place at the fourth position.
11 12 22 25 64
Fifth Pass:
At last the largest value present in the array automatically get placed at
the last position in the array
The resulted array is the sorted array.
11 12 22 25 64
2. Bubble Sort
Bubble sort starts with very first two elements, comparing them to check
which one is greater.
( 5 1 4 2 8 ) –> ( 1 5 4 2 8 ), Here, algorithm compares the first two
Data Structure and Algorithms
Second Pass:
Third Pass:
Now, the array is already sorted, but our algorithm does not know if it is
completed.
The algorithm needs one whole pass without any swap to know it is
sorted.
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
Illustration:
3. Insertion Sort
12 11 13 5 6
First Pass:
12 11 13 5 6
11 12 13 5 6
Data Structure and Algorithms
Second Pass:
11 12 13 5 6
Third Pass:
11 12 13 5 6
• Both 5 and 13 are not present at their correct place so swap them
11 12 5 13 6
11 5 12 13 6
5 11 12 13 6
Fourth Pass:
5 11 12 13 6
• Clearly, they are not sorted, thus perform swap between both
5 11 12 6 13
5 11 6 12 13
5 6 11 12 13
Illustrations:
4. Merge Sort
Advantages Disadvantages
It can be applied to files of any Requires extra space »N
size.
Reading of the input during the Merge Sort requires more space
run-creation step is sequential than other sort.
==> Not much seeking.
If heap sort is used for the in- Merge sort is less efficient than
memory part of the merge, its other sort
operation can be overlapped with
I/O
To know the functioning of merge sort, lets consider an array arr[] = {38,
27, 43, 3, 9, 82, 10}
At first, check if the left index of array is less than the right index, if yes
then calculate its mid point
Data Structure and Algorithms
Now, as we already know that merge sort first divides the whole array
iteratively into equal halves, unless the atomic values are achieved.
Here, we see that an array of 7 items is divided into two arrays of
size 4 and 3 respectively.
Now, again find that is left index is less than the right index for both
arrays, if found yes, then again calculate mid points for both the arrays.
Now, further divide these two arrays into further halves, until the atomic
units of the array is reached and further division is not possible.
Data Structure and Algorithms
After dividing the array into smallest units, start merging the elements
again based on comparison of size of elements Firstly, compare the
element for each list and then combine them into another list in a sorted
manner.
5 Quick Sort
To know the functioning of Quick sort, let’s consider an array arr[] = {10,
80, 30, 90, 40, 50, 70}
• Indexes: 0 1 2 3 4 5 6
• low = 0, high = 6, pivot = arr[h] = 70
• Initialize index of smaller element, i = -1
Step1
Step2
Step3
Step 4
• j = 5 : Since arr[j] <= pivot, do i++ and swap arr[i] with arr[j]
• i=3
Data Structure and Algorithms
• arr[] = {10, 30, 40, 50, 80, 90, 70} // 90 and 50 Swapped
Step 5
Step 6
Step 7
Step 8
Data Structure and Algorithms
6. Heap Sort
To understand heap sort more clearly, let’s take an unsorted array and try
to sort it using heap sort. Consider the array: arr[] = {4, 10, 3, 5, 1}.
Build Complete Binary Tree: Build a complete binary tree from the array.
Transform into max heap: After that, the task is to construct a tree from
that unsorted array and try to convert it into max heap.
Data Structure and Algorithms
Perform heap sort: Remove the maximum element in each step (i.e.,
move it to the end position and remove that) and then consider the
remaining elements and transform it into a max heap.
• Delete the root element (10) from the max heap. In order to
delete this node, try to swap it with the last node, i.e. (1). After
removing the root element, again heapify it to convert it into max
heap.
• Resulted heap and array should look like this:
• Repeat the above steps and it will look like the following:
Data Structure and Algorithms
• Now when the root is removed once again it is sorted. and the
sorted array will be like arr[] = {1, 3, 4, 5, 10}.
7. Counting Sort
• Now, store the count of each unique element in the count array
• If any element repeats itself, simply increase its count.
• Modify the count array such that each element at each index
stores the sum of previous counts.
• Index: 0 1 2 3 4 5 6 7 8 9
• Count: 0 2 4 4 5 6 6 7 7 7
• The modified count array indicates the position of each object in
the output sequence.
Data Structure and Algorithms
• Find the index of each element of the original array in the count
array. This gives the cumulative count.