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

Quick Sort

The document contains a C program that implements the Quick Sort algorithm. It includes functions for swapping elements, partitioning the array, and displaying the sorted array. The main function prompts the user to input the size and elements of the array, sorts it using Quick Sort, and displays the results before and after sorting.
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Quick Sort

The document contains a C program that implements the Quick Sort algorithm. It includes functions for swapping elements, partitioning the array, and displaying the sorted array. The main function prompts the user to input the size and elements of the array, sorts it using Quick Sort, and displays the results before and after sorting.
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

#include<stdio.

h>
void swap(int *a,int *b){
int temp=*a;
*a=*b;
*b =temp;
}
int partition(int a[],int low,int high){
int pivot=a[high];
int i=low-1;
int j;
for(j=low;j<high;j++){
if(a[j]<pivot){
i++;
swap(&a[i],&a[j]);
}
}
swap(&a[i+1],&a[high]);
return i+1;
}
void quicksort(int a[],int low,int high){
if(low<high){
int pi=partition(a,low,high);
quicksort(a,low,pi-1);
quicksort(a,pi+1,high);
}
}

void display(int a[],int size){


for(int i=0;i<size;i++){
printf("%d\t",a[i]);
}
}
void main(){
int i,size,a[20];
printf("Enter the Size of the array");
scanf("%d",&size);
printf("Enter the elements of the array");
for(i=0;i<size;i++){
scanf("%d",&a[i]);
}
printf("Before sorting\n");
display(a,size);
quicksort(a,0,size-1);
printf("\nAfter sorting\n");
display(a,size);
}

You might also like