C++ DSA Sorting
C++ DSA Sorting
Home My courses CONT_23CSH-103 :: ELEMENTARY DATA STRUCTURES USING C++ Chapter 2.2
CO3 - Analyse and explain the behaviour of linear data structure operations using the programming addressed in
the course.
Sorting:
Sorting is the method of arranging the elements in particular order mostly either ascending or descending order.
Types of sorting
Bubble Sort
Selection Sort
Insertion Sort
1. Bubble Sort: It is the simplest sorting algorithm that works by swapping adjacent elements if they are in the wrong
order.
Example:
Take an array of numbers ” 5 1 4 2 8″, and sort the array from lowest number to greatest number using bubble sort. In
each step, elements written in bold are being compared. Three passes will be required;
First Pass
( 5 1 4 2 8 ) → ( 1 5 4 2 8 ), Here, algorithm compares the first two elements, and swaps since 5 > 1.
( 1 4 2 5 8 ) → ( 1 4 2 5 8 ), Now, since these elements are already in order (8 > 5), algorithm does not swap them.
Second Pass
(14258)→(14258)
(12458)→(12458)
(12458)→(12458)
Now, the array is already sorted, but the algorithm does not know if it is completed. The algorithm needs
one whole pass without any swap to know it is sorted.
Third Pass
(12458)→(12458)
(12458)→(12458)
(12458)→(12458)
(12458)→(12458)
#include<iostream>
using namespace std;
int main()
{
int n, i, arr[50], j, temp;
cout<<"Enter the Size (max. 50): ";
cin>>n;
cout<<"Enter "<<n<<" Numbers: ";
for(i=0; i<n; i++)
cin>>arr[i];
cout<<"\nSorting the Array using Bubble Sort Technique..\n";
for(i=0; i<(n-1); i++)
{
for(j=0; j<(n-i-1); j++)
{
if(arr[j]>arr[j+1])
{
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
cout<<"\nArray Sorted Successfully!\n";
cout<<"\nThe New Array is: \n";
for(i=0; i<n; i++)
cout<<arr[i]<<" ";
cout<<endl;
return 0;
}
2. Selection Sort: The selection sort improves on the bubble sort by making only one exchange for every pass
through the list. In order to do this, a selection sort looks for the largest value as it makes a pass and, after completing
the pass, places it in the proper location. As with a bubble sort, after the first pass, the largest item is in the correct
place. After the second pass, the next largest is in place. This process continues and requires n−1 passes to
sort n items, since the final item must be in place after the (n−1)(n−1) st pass.
Following figure shows the entire sorting process. On each pass, the largest remaining
item is selected and then placed in its proper location. The first pass places 93, the
second pass places 77, the third places 55, and so on.
Figure3 Selection Sort
#include<iostream>
using namespace std;
int main()
{
int tot, arr[50], i, j, temp, small, chk, index;
cout<<"Enter the Size of Array: ";
cin>>tot;
cout<<"Enter "<<tot<<" Array Elements: ";
for(i=0; i<tot; i++)
cin>>arr[i];
for(i=0; i<(tot-1); i++)
{
chk=0;
small = arr[i];
for(j=(i+1); j<tot; j++)
{
if(small>arr[j])
{
small = arr[j];
chk++;
index = j;
}
}
if(chk!=0)
{
temp = arr[i];
arr[i] = small;
arr[index] = temp;
}
}
cout<<"\nSorted Array is:\n";
for(i=0; i<tot; i++)
cout<<arr[i]<<" ";
cout<<endl;
return 0;
}
Insertion Sort
To sort an array using the insertion sort technique in C++ programming, you have to ask the user to enter the size of
the array and then enter elements of that size in random order. After receiving the inputs, sort the given array in
ascending order using insertion sort as shown in the program given below:
#include<iostream>
using namespace std;
int main()
{
int arr[50], tot, i, j, k, elem, index;
cout<<"Enter the Size for Array: ";
cin>>tot;
cout<<"Enter "<<tot<<" Array Elements: ";
for(i=0; i<tot; i++)
cin>>arr[i];
for(i=1; i<tot; i++)
{
elem = arr[i];
if(elem<arr[i-1])
{
for(j=0; j<=i; j++)
{
if(elem<arr[j])
{
index = j;
for(k=i; k>j; k--)
arr[k] = arr[k-1];
break;
}
}
}
else
continue;
arr[index] = elem;
}
cout<<"\nThe New Array (Sorted Array):\n";
for(i=0; i<tot; i++)
cout<<arr[i]<<" ";
cout<<endl;
return 0;
}
Previous activity
◄ PPT Lecture 2.2.1
Jump to...
Next activity
PPT Lecture 2.2.2 ►