0% found this document useful (0 votes)
2 views3 pages

Quick Sort

This document contains a C program that implements the Quick Sort algorithm to sort an array of integers. It prompts the user to enter the size and elements of the array, performs the sorting using a recursive function, and then displays the sorted elements. The program includes detailed comments explaining the sorting process and the swapping of elements.

Uploaded by

hinisiva
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views3 pages

Quick Sort

This document contains a C program that implements the Quick Sort algorithm to sort an array of integers. It prompts the user to enter the size and elements of the array, performs the sorting using a recursive function, and then displays the sorted elements. The program includes detailed comments explaining the sorting process and the swapping of elements.

Uploaded by

hinisiva
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

//Quick sort

#include<stdio.h>

int a[20],n;

void qsort(int,int);

int main(void)

int i;

printf("enter the array size:");

scanf("%d",&n);

printf("enter the elements to be sorted\n");

for(i=0;i<n;i++)

scanf("%d",&a[i]);

qsort(0,n-1);

printf("\nThe sorted elements are\n");

for(i=0;i<n;i++)

printf("%d\t",a[i]);

system("pause");

//Routine for quick sort

void qsort(int left, int right)

int i,j,pivot,temp,k;

if(left<right)

{// initial values

i=left+1;

j=right;

pivot=left;

for(;;)

{
while(a[pivot]>=a[i])//scanning to right for larger element than pivot

i=i+1;

while(a[pivot]<a[j])//scanning to left for smaller element than pivot

j=j-1;

if(i<j)//swapping elements of i & j

temp=a[i];

a[i]=a[j];

a[j]=temp;

for(k=0;k<n;k++)

printf("%d\t",a[k]);

printf("\n");

else

break;//if i>=j for loop breaks

temp=a[pivot];//swapping pivot and element at j

a[pivot]=a[j];

a[j]=temp;

for(k=0;k<n;k++)

printf("%d\t",a[k]);

printf("\n");

qsort(left,j-1);//repeating quick sort for left half of array

qsort(j+1,right);//repeating quick sort for right part

return;

You might also like