0% found this document useful (0 votes)
48 views

DSA Lab 05

The document describes a lab assignment to implement three sorting algorithms: bubble sort, selection sort, and insertion sort. The student is asked to create a program that takes an array of 10 user inputs and sorts the array using each of the three algorithms. Code snippets are provided for each sorting algorithm, showing how it works on an array of integers. The output of running each algorithm is requested.

Uploaded by

Atif Jalal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views

DSA Lab 05

The document describes a lab assignment to implement three sorting algorithms: bubble sort, selection sort, and insertion sort. The student is asked to create a program that takes an array of 10 user inputs and sorts the array using each of the three algorithms. Code snippets are provided for each sorting algorithm, showing how it works on an array of integers. The output of running each algorithm is requested.

Uploaded by

Atif Jalal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Atif Jalal

02-235191-027
BS (IT)-3A Lab 05 Date: 10 July, 2020

Lab 05: Implementation for elementary sorting algorithms

 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>

using namespace std;

int main()

int a[10],n=10,i,j,temp;

cout<<"Enter the array elements: \n\n";

for(i=0;i<n;++i)

cout<<"Enter Element "<<i+1<<" : ";

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;

cout<<"\n\nArray after bubble sort:";

for(i=0;i<n;++i)

cout<<" "<<a[i];

return 0;

Output:

Selection Sort:
Source Code:
#include<iostream>

using namespace std;

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++)

cout<<"Enter Element "<<i+1<<" : ";

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;

cout<<"\n\nArray after Selection sort : ";

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>

using namespace std;

int main()

int i,j,n=10,temp,a[10];

cout<<"Enter the array elements: \n\n";

for(i=0;i<n;i++)

cout<<"Enter Element "<<i+1<<" : ";

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;

cout<<"\n\nArray after Insertion sort : ";

for(i=0;i<n;i++)

cout<<a[i]<<" ";

return 0;

Output:

You might also like