DSA Lab 05
DSA Lab 05
02-235191-027
BS (IT)-3A Lab 05 Date: 10 July, 2020
Bubble Sort
Selection Sort
Insertion Sort
Task 1: Create a program that take an array of 10 inputs from the user and generate the sorted out
put using the following Algorithms;
Bubble Sort
Selection Sort
Insertion Sort
Bubble Sort:
Source Code:
#include<iostream>
int main()
int a[10],n=10,i,j,temp;
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])
{
Atif Jalal
02-235191-027
BS (IT)-3A Lab 05 Date: 10 July, 2020
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
for(i=0;i<n;++i)
cout<<" "<<a[i];
return 0;
Output:
Selection Sort:
Source Code:
#include<iostream>
int main()
int i,j,n=10,loc,temp,min,a[10];
Atif Jalal
02-235191-027
BS (IT)-3A Lab 05 Date: 10 July, 2020
cout<<"Enter the array elements: \n\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]<<" ";
Atif Jalal
02-235191-027
BS (IT)-3A Lab 05 Date: 10 July, 2020
}
return 0;
Output:
Insertion Sort:
Source Code:
#include<iostream>
int main()
int i,j,n=10,temp,a[10];
for(i=0;i<n;i++)
cin>>a[i];
}
Atif Jalal
02-235191-027
BS (IT)-3A Lab 05 Date: 10 July, 2020
for(i=1;i<=n-1;i++)
temp=a[i];
j=i-1;
while((temp<a[j])&&(j>=0))
a[j+1]=a[j];
j=j-1;
a[j+1]=temp;
for(i=0;i<n;i++)
cout<<a[i]<<" ";
return 0;
Output: