Thursday, 12 October 2017

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>
//Author : Vivek S. Sharma

//Copyright : Your copyright notice
using namespace std;
void quick_sort(int[],int,int);
int partition(int[],int,int);
void top_five(int[],int);

int main()
{
    int a[50],n,i;
    cout<<"Enter number of students : ";
    cin>>n;
    cout<<"\nEnter first year percentage :";

    for(i=0;i<n;i++)
        cin>>a[i];

    int ch;
    do{
      cout<<"\n\nEnter choice : \n1.Quick Sort \n2.Top five scores \n3.Exit";
      cin>>ch;
      switch(ch){
            case 1:quick_sort(a,0,n-1);
          cout<<"\nPercentage after sorting:";

          for(i=0;i<n;i++)
              cout<<a[i]<<" ";
          break;

            case 2: top_five(a,n);break;
      };
    }while(ch!=3);


    return 0;
}

void quick_sort(int a[],int l,int u)
{
    int j;
    if(l<u)
    {
        j=partition(a,l,u);
        quick_sort(a,l,j-1);
        quick_sort(a,j+1,u);
    }
}

int partition(int a[],int l,int u)
{
    int v,i,j,temp;
    v=a[l];
    i=l;
    j=u+1;

    do
    {
        do
            i++;

        while(a[i]<v&&i<=u);

        do
            j--;
        while(v<a[j]);

        if(i<j)
        {
            temp=a[i];
            a[i]=a[j];
            a[j]=temp;
        }
    }while(i<j);

    a[l]=a[j];
    a[j]=v;

    return(j);
}
void top_five(int a[],int n){
for(int i=n-1 ; i>=n-5 ; i--){
      cout<<"\n "<<a[i];
}
}
--------------------------------------------------------------------------------------------------------------------------



**If some mistakes, plz let me know!-----Vivek S. Sharma

No comments:

Post a Comment