Write C++ program to store first year percentage of students in array. Sort array of floating
point numbers in ascending order using quick sort and display top five scores.
#include<iostream>
using namespace std;
void quick_sort(float a[],int low,int high)
{
int i,j;
float pivot,temp;
pivot=a[low];
i=low;
j=high;
while(i<j)
{
while(a[i]<=pivot)
i++;
while(a[j]>pivot && i<=j)
j--;
if(i<=j)
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
temp=a[j];
a[j]=a[low];
a[low]=temp;
cout <<"\n\n Pass"<<" : ";
for(i=0; i<5; i++)
cout <<a[i]<<" ";
quick_sort(a, low , j-1); // Recursive Calls....for partition before pivot
quick_sort(a, j+1 , high);
}
int main()
{
int i, size = 5;
float a[10];
cout<<"\n\n Enter the Percentages of 5 students : ";
for(i=0; i<size; i++)
{
cout<<"\n\t For Student "<<i+1<<" :";
cin>>a[i];
}
cout<<"\n\n Sorted List using Quick Sort Algorithm: ";
quick_sort(a , 0 , size-1);
cout<<"\n\n";
return 0;
}
#include<iostream>
using namespace std;
void quick_sort(float a[],int low,int high)
OUTPUT FOR THE PROGRAM |
int i,j;
float pivot,temp;
pivot=a[low];
i=low;
j=high;
while(i<j)
{
while(a[i]<=pivot)
i++;
while(a[j]>pivot && i<=j)
j--;
if(i<=j)
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
temp=a[j];
a[j]=a[low];
a[low]=temp;
cout <<"\n\n Pass"<<" : ";
for(i=0; i<5; i++)
cout <<a[i]<<" ";
quick_sort(a, low , j-1); // Recursive Calls....for partition before pivot
quick_sort(a, j+1 , high);
}
int main()
{
int i, size = 5;
float a[10];
cout<<"\n\n Enter the Percentages of 5 students : ";
for(i=0; i<size; i++)
{
cout<<"\n\t For Student "<<i+1<<" :";
cin>>a[i];
}
cout<<"\n\n Sorted List using Quick Sort Algorithm: ";
quick_sort(a , 0 , size-1);
cout<<"\n\n";
return 0;
}
For more such posts click the link:-http://svencrai.com/G8W
Tags
CODING