Saturday, October 20, 2018

c program for quicksort

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.

float arr[5]={5.1,4.2,3.3,2.4,1.5};

void quicksort(int start,int end)
{
    if(start<end)
    {
        int i=start+1;
        float pivote=arr[start];
        int j=end;
        float temp;
        while(i<j)
        {
            while(arr[i]<pivote&&i<5)
            i++;
            while(arr[j]>pivote)
            j--;
           
            if(i<j)
            {
                temp=arr[i];
                arr[i]=arr[j];
                arr[j]=temp;
            }
        }
       
        temp=arr[j];
        arr[j]=arr[start];
        arr[start]=temp;
        quicksort(start,j-1);
        quicksort(j+1,end);
    }
}

int main()
{
    int i=0;
    int start=0;
    int end=4;
   
    quicksort(start,end);
   
    printf("\nThe top 5 scores are:");
    for(i=0;i<5;i++)
    {
        printf("\n%f",arr[i]);
    }
   
    return 0;
}

No comments:

Post a Comment