0% found this document useful (0 votes)
71 views

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

The document summarizes a student's lab experiment on implementing the quicksort algorithm. It includes the student's name and details, an overview of the aim of implementing quicksort, the steps of the quicksort algorithm, code to implement quicksort in C++, and results/output. The code takes an array as input, implements quicksort to sort the array, and outputs the sorted array.

Uploaded by

405 found
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
71 views

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

The document summarizes a student's lab experiment on implementing the quicksort algorithm. It includes the student's name and details, an overview of the aim of implementing quicksort, the steps of the quicksort algorithm, code to implement quicksort in C++, and results/output. The code takes an array as input, implements quicksort to sort the array, and outputs the sorted array.

Uploaded by

405 found
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 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