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

Program 11-1

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

Program 11-1

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

PROGRAM-11

AIM – To sort the elements using quicksort.


ALGORITHM:
Step-1 – Choose a pivot element from the array.
Step-2 – Partition the array around the pivot, so that:
- All elements less than the pivot are on the left of the pivot.
- All elements greater than the pivot are on right of the pivot.
Step-3 – Recursively apply the above steps to the sub-array of the
elements greater than the pivot.
Step-4 – Recursively apply the above steps to the sub-array of the
elements less than the pivot.
Step-5 – Combine the results of the two recursive calls to produce the
final sorted array.

PROGRAM CODE:
#include<stdio.h>
#include<conio.h>

void quickSort(int arr[], int low, int high){


int i=low, j=high;
int pivot=arr[(low+high)/2];

while(i<=j){
while(arr[i]<pivot) i++;
while(arr[j]>pivot) j--;
if(i<=j){
int temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
i++;
j--;
}
}

if(low<j)quickSort(arr,low,j);
if(i<high)quickSort(arr,i,high);
}

int main(){
int arr[10];
int n;
clrscr();

prin ("Enter the size of the array:\n");


scanf("%d,&n");
prin ("Enter the elements into the array:\n");
for(int k=0;k<n;k++){
scanf("%d",&arr[k]);
}

quickSort(arr,0,n-1);
prin ("Sorted array:\n");
for(int i=0;i<n;i++){
prin ("%d ",arr[i]);
}

getch();
return 0;
}

OUTPUT:

You might also like