You are on page 1of 8

Give the algorithm and explain the concept of selection sort with an example program.

Selection Sort
Selection sort is one of the simplest internal sorting technique. It involves selection of the
smallest element from the given list and replacing it with the first element. This process is
done on the unsorted portion of the list and will be repeated until all the elements in the list
are sorted. Initially the complete list will be unsorted. The selected smallest element is
swapped with the first element. Now, the first element is said to be sorted and the
remaining elements are unsorted. This process will be repeated on the unsorted portion
until all the elements in the list are sorted.
Algorithm for selection sort
Step 1: Read all the elements from the list.
Step 2: Store each element in an array
Step 3 : set the index value i=0
Step 4: Repeat step 5 to step 10 until all the elements in the list are sorted.
Step 5: Set first to i
Step 6: Set smallest to i+1
Step 7: Repeat step 8 and step 9 while smallest < size_of_the_list.
Step 8 : If smallest < min, swap min and smallest.
Step 9 : Increment smallest
Step 10: Increment i
Step 11: End

Program

#include <iostream>
#include<conio.h>
using namespace std;
int main()
{
int i,min,n,next,temp,sortList[20];

1
cout<<"Enter the size of the list:";
cin>>n;5
cout<<"Enter the elements to be sorted:";
for(i=0;i<n;i++)
cin>>sortList[i];
for(i=0;i<n;i++)
{
for(next=i+1;next<n;next++)1<7
{
min=i;0
if(sortList[next]<sortList[min])1<0
{
temp=sortList[min];9
sortList[min]=sortList[next];2
sortList[next]=temp;
}
}
}
cout<<"List after selection sort:";
for(i=0;i<n;i++)
cout<<"\t"<<sortList[i];
getch();
return 0;
}
Output:

Example

2
3
Give the algorithm and explain the concept of bubble sort with an example program.
Bubble sort
Bubble sort is another simple sorting technique in which after each pass the largest
elements is bubbled up to its correct position.
The principle of bubble sort is to organize the list of elements by uniformly checking the
two adjacent elements and accordingly adjusting their positions. The sorting starts by
initially comparing the first two adjacent elements with each other. If the first element is
greater than the second element, then these positions are exchanged. Otherwise, the
elements are at their own positions. Next, comparison is done between the second element
and third element. Their positions are exchanged if second element is larger than the third
element. Otherwise elements remain in their existing positions. In similar fashion, all the
subsequent elements are compared with each other and exchanged if required. This

4
comparison is repeated till the last element is reached. This last element will be the largest
element and is placed at its correct position. This process is repeated until all the elements
are sorted in ascending order and till all the elements are bubbled up from the last element
to first element.

Program
#include<iostream>
#include<conio.h>

5
int main()
{
int sortList[100];
int i,j,temp,n,x;
cout<<"Enter the size of the list:";
cin>>n;
cout<<"\n Enter elements to be sorted:\n";
for(i=0;i<n;i++)
cin>>sortList[i];
for(i=1;i<n;i++)
{
for(j=0;j<(n-i);j++)
{
if(sortList[j]>sortList[j+1])
{
temp=sortList[j];
sortList[j]=sortList[j+1];
sortList[j+1]=temp;
}
}
}
cout<<"\n\n List after bubble sort:\n";
for(i=0;i<n;i++)
{
cout<<sortList[i];
cout<<" ";
getch();
}

6
Output:

Example:

7
8

You might also like