Write A Program To Implement Quick Sort Algorithm.: (For Programming Based Labs)

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Student Name: Ashrith Ravikanti UID:20BCS4378

Branch: CSE Section/Group:20BDA3-B


Semester: 4 Date of Performance:17-02-2022
Subject Name: Design and Analysis of Algorithm lab
Subject Code:

1. Aim/Overview of the practical:


WS
Write a program to implement Quick sort algorithm.
2

2. Algorithm/Flowchart (For programming based labs):

 Step 1: If left <right (Else Step 8)


 Step 2: Set pivot = a[left]
Set i=left
Set j =right
 Step 3: Repeat step 4-6 while i<j
 Step 4: Repeat while a[i] <=pivot and i<right
Set i++
 Step 5: Repeat while a[j]>pivot
Set j—
 Step 6: If i<j
Swap a[i] and a[j]
 Step 7: If i>=j
Swap a[j] and a[left]
Call quicksort (a, left, j-1)
Call quicksort (a, j+1, right)
 Step 8: Exit.
3. Code:
#include<iostream>
using namespace std;
void quicksort(int arr[],int first,int last)
{
    int i,j,pivot,temp;
    if(first<last)
    {
        pivot=first;
        i=first;
        j=last;

        while(i<j)
        {
            while(arr[i]<=arr[pivot]&&i<last)
            {
               i++;
            }
               while (arr[j]>arr[pivot])
               {
                   j--;
               }
               if(i<j)
               {
                   temp=arr[i];
                   arr[i]=arr[j];
                   arr[j]=temp;
               }
   
           
        }
        temp=arr[pivot];
        arr[pivot]=arr[j];
        arr[j]=temp;
        quicksort(arr,first,j-1);
        quicksort(arr,j+1,last);
    }
}
int main()
{
    int i,n;
    cout<<"Enter (n) size of array::";
    cin>>n;
    int arr[n];
    cout<<"Enter elements of array::\n";
    for(i=0;i<n;i++)
    {
        cin>>arr[i];

    }
   
    quicksort(arr,0,n-1);
    cout<<"After Sorting::\n";
    for(i=0;i<n;i++)
    {
        cout<<arr[i]<<"\n";
    }

    return 0;
}

4. Result/Output/Writing Summary:
Evaluation Grid (To be created as per the SOP and Assessment guidelines by the faculty):

Sr. No. Parameters Marks Obtained Maximum Marks


1.
2.
3.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy