1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
|
#include<stdio.h>
void quicksort(int array[], int firstIndex, int lastIndex)
{
int pivotIndex, temp, index1, index2;
if(firstIndex < lastIndex)
{
pivotIndex = firstIndex;
index1 = firstIndex;
index2 = lastIndex;
while(index1 < index2)
{
while(array[index1] <= array[pivotIndex] && index1 < lastIndex)
{
index1++;
}
while(array[index2]>array[pivotIndex])
{
index2--;
}
if(index1<index2)
{
temp = array[index1];
array[index1] = array[index2];
array[index2] = temp;
}
}
temp = array[pivotIndex];
array[pivotIndex] = array[index2];
array[index2] = temp;
quicksort(array, firstIndex, index2-1);
quicksort(array, index2+1, lastIndex);
}
}
int main()
{
//Declaring variables
int array[100],n,i;
printf("Enter the number of element you want to Sort : ");
scanf("%d",&n);
printf("Enter Elements in the list : ");
for(i = 0; i < n; i++)
{
scanf("%d",&array[i]);
}
quicksort(array,0,n-1);
printf("Sorted elements: ");
for(i=0;i<n;i++)
printf(" %d",array[i]);
return 0;
}
|