Raw 7

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

#include<stdio.

h>
void swap (int a[], int left, int right)
{
int temp;
temp=a[left];
a[left]=a[right];
a[right]=temp;
}

void printarr(int a[], int n);

void quicksort( int a[], int low, int high,int n )


{
int pivot;
if ( high > low )
{
pivot = part( a, low, high );
printf("\n%d is pivot element\n",a[pivot]);
printarr(a,n);
quicksort( a, low, pivot-1,n );

quicksort( a, pivot+1, high,n );


}
}
int part( int a[], int low, int high )
{
int left, right;
int pivot_item;
int pivot = left = low;
pivot_item = a[low];
right = high;
while ( left < right )
{

while( a[left] <= pivot_item )


{
left++;
}

while( a[right] >pivot_item )


{
right--;
}
if ( left < right )
{
swap(a,left,right);
}
}

a[low] = a[right];
a[right] = pivot_item;
return right;
}
int main()
{
int a[50], i, n;
printf("\nEnter no. of elements: ");
scanf("%d", &n);
printf("\nEnter the elements: \n");
for (i=0; i<n; i++)
scanf ("%d", &a[i]);
printf("\nUnsorted elements: \n");
printarr(a,n);
quicksort(a,0,n-1,n);
printf("\nSorted elements: \n");
printarr(a,n);
}

void printarr(int a[], int n)


{
int i;
printf("\n");
for (i=0; i<n; i++)
printf(" %d ", a[i]);
printf("\n");
}

//////////////////////////////////////////////////////////////////////
OUTPUT:
Enter no. of elements: 6

Enter the elements:


10
21
32
45
66
78

Unsorted elements:

10 21 32 45 66 78

10 is pivot element

10 21 32 45 66 78

21 is pivot element

10 21 32 45 66 78

32 is pivot element

10 21 32 45 66 78

45 is pivot element

10 21 32 45 66 78

66 is pivot element

10 21 32 45 66 78

Sorted elements:

10 21 32 45 66 78
Process returned 0 (0x0) execution time : 16.524 s
Press any key to continue.

You might also like