Quick Sort

C Programming

C Programming Examples

#include<stdio.h>

void quick_sort(int start, int end, int *array) {
	int mid, i = start, j = end, temp;
	mid = array[(start + end) / 2];
	if(i <= j) {
		while(array[i] < mid)
			i++;
		while(mid < array[j])
			j--;
		temp = array[i];
		array[i] = array[j];
		array[j] = temp;
		i++;
		j--;
	}
	if(start < j)
		quick_sort(start, j, array);
	if(i < end)
		quick_sort(i, end, array);
}

int main() {
	int data[10], i;
	printf("Enter 10 random number to sort in ascending order:\n");
	for(i = 0; i < 10; i++)
		scanf("%d", &data[i]);

	/* Sorting array using call by reference */
	quick_sort(0, 9, data);
	printf("After sort\n");
	for(i = 0; i < 10; i++)
		printf("%d\n",data[i]);
	return 0;
}


          /****** Output ******/
          

Enter 10 random number to sort in ascending order:


4


6


1


8


3


7


2


5


9


0



After sort


0


1


2


3


4


5


6


7


8


9

Learn more about Quick Sort