### Write C++ program to store first year percentage of students in array. Write function for sorting array of floating point numbers in ascending order using a) Selection Sort b) Bubble sort c) Insertion Sort and display top five scores.

Write C++ program to store first year percentage of students in array. Write function for

sorting array of floating point numbers in ascending order using

a) Selection Sort b) Bubble sort c) Insertion Sort and display top five scores.

#include<iostream>

using namespace std;

int n;

float A[50];

int i,j;

void input()

{

cout<<"Enter the No. of students: ";

cin>>n;

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

{

cout<<"\nEnter Data : ";

cin>>A[i];

}

}

void display()

{

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

cout<<"\n Data Is : "<<A[i];

}

void bubble_sort()

{

int i,j,temp;

input();

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

{

cout<<"Pass "<<i+1;

for(j=0;j<n-1;j++)

{

if(A[j]>A[j+1])

{

temp = A[j];

A[j] = A[j+1];

A[j+1] = temp;

}

}

display();

cout<<"\n\n";

}

display();

}

void selection_sort()

{

int i,j,temp;

input();

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

{

cout<<"Pass "<<i+1;

for(j=i+1;j<n;j++)

{

if(A[i]>A[j])

{

temp = A[i];

A[i] = A[j];

A[j] = temp;

}

}

cout<<"\n";

display();

cout<<"\n";

}

}

void insertion_sort()

{

int i,j,k,temp;

input();

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

{

cout<<"Pass "<<i+1;

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

{

if(A[j]>A[i])

{

temp = A[i];

for(k=i-1;k>=j;k--)

A[k+1] = A[k];

A[j] = temp;

}

}

cout<<"\n";

display();

cout<<"\n";

}

}

int main()

{

int choice;

do

{

cout<<"\n******* MENU ******\n";

cout<<"\n1.Bubble Sort";

cout<<"\n2.Selection Sort";

cout<<"\n3.Insertion Sort";

cout<<"\n0.Exit";

cout<<"\nEnter Choice: ";

cin>>choice;

switch(choice)

{

case 1:

bubble_sort();

break;

case 2:

selection_sort();

break;

case 3:

insertion_sort();

break;

}

}while(choice!=0);

return 0;

}

sorting array of floating point numbers in ascending order using

a) Selection Sort b) Bubble sort c) Insertion Sort and display top five scores.

#include<iostream>

using namespace std;

OUTPUT FOR THE PROGRAM |

float A[50];

int i,j;

void input()

{

cout<<"Enter the No. of students: ";

cin>>n;

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

{

cout<<"\nEnter Data : ";

cin>>A[i];

}

}

void display()

{

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

cout<<"\n Data Is : "<<A[i];

}

void bubble_sort()

{

int i,j,temp;

input();

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

{

cout<<"Pass "<<i+1;

for(j=0;j<n-1;j++)

{

if(A[j]>A[j+1])

{

temp = A[j];

A[j] = A[j+1];

A[j+1] = temp;

}

}

display();

cout<<"\n\n";

}

display();

}

void selection_sort()

{

int i,j,temp;

input();

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

{

cout<<"Pass "<<i+1;

for(j=i+1;j<n;j++)

{

if(A[i]>A[j])

{

temp = A[i];

A[i] = A[j];

A[j] = temp;

}

}

cout<<"\n";

display();

cout<<"\n";

}

}

void insertion_sort()

{

int i,j,k,temp;

input();

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

{

cout<<"Pass "<<i+1;

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

{

if(A[j]>A[i])

{

temp = A[i];

for(k=i-1;k>=j;k--)

A[k+1] = A[k];

A[j] = temp;

}

}

cout<<"\n";

display();

cout<<"\n";

}

}

int main()

{

int choice;

do

{

cout<<"\n******* MENU ******\n";

cout<<"\n1.Bubble Sort";

cout<<"\n2.Selection Sort";

cout<<"\n3.Insertion Sort";

cout<<"\n0.Exit";

cout<<"\nEnter Choice: ";

cin>>choice;

switch(choice)

{

case 1:

bubble_sort();

break;

case 2:

selection_sort();

break;

case 3:

insertion_sort();

break;

}

}while(choice!=0);

return 0;

}

## Comments

## Post a Comment