Sorting Techniques: Stable and Not Stable Sorting
Sorting Techniques: Stable and Not Stable Sorting
Sorting Techniques: Stable and Not Stable Sorting
The importance of sorting lies in the fact that data searching can be
optimized to a very high level, if data is stored in a sorted manner.
Sorting is also used to represent data in more readable formats.
Following are some of the examples of sorting in real-life scenarios −
Bubble sort starts with very first two elements, comparing them to check
which one is greater.
We know then that 10 is smaller 35. Hence they are not sorted.
We swap these values. We find that we have reached the end of the
array. After one iteration, the array should look like this −
To be precise, we are now showing how an array should look like after
each iteration. After the second iteration, it should look like this −
Notice that after each iteration, at least one value moves at the end.
And when there's no swap required, bubble sorts learns that an array is
completely sorted.
begin BubbleSort(list)
swap(list[i], list[i+1])
end if
end for
return list
end BubbleSort
#include<iostream>
int main()
int a[50],n,i,j,temp;
cin>>n;
for(i=0;i<n;++i)
cin>>a[i];
for(i=1;i<n;++i)
for(j=0;j<(n-i);++j)
if(a[j]>a[j+1])
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
for(i=0;i<n;++i)
cout<<" "<<a[i];
return 0;
Output
Insertion Sort
How Insertion Sort Works?
We take an unsorted array for our example.
It finds that both 14 and 33 are already in ascending order. For now, 14
is in sorted sub-list.
It swaps 33 with 27. It also checks with all the elements of sorted sub-
list. Here we see that the sorted sub-list has only one element 14, and
27 is greater than 14. Hence, the sorted sub-list remains sorted after
swapping.
So we swap them.
This process goes on until all the unsorted values are covered in a sorted
sub-list. Now we shall see some programming aspects of insertion sort.
Algorithm
Now we have a bigger picture of how this sorting technique works, so we
can derive simple steps by which we can achieve insertion sort.
Step 1 −
If it is the first element, it is already sorted. return 1;
Step 2 −
Pick next element
Step 3 −
Compare with all elements in the sorted sub-list
Step 4 −
Shift all the elements in the sorted sub-list that is greater than the
value to be sorted
Step 5 − Insert the value
Step 6 − Repeat until list is sorted
Program for Insertion Sort in C++
#include<iostream>
int main()
int i,j,n,temp,a[30];
cin>>n;
for(i=0;i<n;i++)
cin>>a[i];
for(i=1;i<=n-1;i++)
temp=a[i];
j=i-1;
while((temp<a[j])&&(j>=0))
j=j-1;
cout<<a[i]<<" ";
return 0;
}
Selection Sort
Selection sort is a simple sorting algorithm. This sorting algorithm is an
in-place comparison-based algorithm in which the list is divided into two
parts, the sorted part at the left end and the unsorted part at the right
end. Initially, the sorted part is empty and the unsorted part is the entire
list.
The smallest element is selected from the unsorted array and swapped
with the leftmost element, and that element becomes a part of the
sorted array. This process continues moving unsorted array boundary by
one element to the right.
For the first position in the sorted list, the whole list is scanned
sequentially. The first position where 14 is stored presently, we search
the whole list and find that 10 is the lowest value.
For the second position, where 33 is residing, we start scanning the rest
of the list in a linear manner.
We find that 14 is the second lowest value in the list and it should appear
at the second place. We swap these values.
After two iterations, two least values are positioned at the beginning in a
sorted manner.
The same process is applied to the rest of the items in the array.
Algorithm
Step 1 − Set MIN to location 0
Step 2 − Search the minimum element in the list
Step 3 − Swap with value at location MIN
Step 4 − Increment MIN to point to next element
Step 5 − Repeat until list is sorted
#include<iostream>
int main()
int i,j,n,loc,temp,min,a[30];
cin>>n;
for(i=0;i<n;i++)
cin>>a[i];
for(i=0;i<n-1;i++)
min=a[i];
loc=i;
for(j=i+1;j<n;j++)
if(min>a[j])
{
min=a[j];
loc=j;
temp=a[i];
a[i]=a[loc];
a[loc]=temp;
for(i=0;i<n;i++)
cout<<a[i]<<" ";
return 0;
}
Merge Sort Algorithm
Merge sort first divides the array into equal halves and then combines
them in a sorted manner.
We know that merge sort first divides the whole array iteratively into
equal halves unless the atomic values are achieved. We see here that an
array of 8 items is divided into two arrays of size 4.
We further divide these arrays and we achieve atomic value which can no
more be divided.
Now, we combine them in exactly the same manner as they were broken
down. Please note the color codes given to these lists.
We first compare the element for each list and then combine them into
another list in a sorted manner. We see that 14 and 33 are in sorted
positions. We compare 27 and 10 and in the target list of 2 values we put
10 first, followed by 27. We change the order of 19 and 35 whereas 42
and 44 are placed sequentially.
In the next iteration of the combining phase, we compare lists of two
data values, and merge them into a list of found data values placing all in
a sorted order.
After the final merging, the list should look like this −
int main()
{
int n, i;
cout<<"\nEnter the number of data element to be sorted: ";
cin>>n;
int arr[n];
for(i = 0; i < n; i++)
{
cout<<"Enter element "<<i+1<<": ";
cin>>arr[i];
}
MergeSort(arr, 0, n-1);
return 0;
}
Output
Enter the number of data element to be sorted: 10
Enter element 1: 23
Enter element 2: 987
Enter element 3: 45
Enter element 4: 65
Enter element 5: 32
Enter element 6: 9
Enter element 7: 475
Enter element 8: 1
Enter element 9: 17
Enter element 10: 3