-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquickSort.c
67 lines (53 loc) · 1.03 KB
/
quickSort.c
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[10], int start, int end);
int partition(int array[10], int left, int right);
void swap(int *x, int *y);
int main(void)
{
int array[10], i;
printf("Enter an array of size 10\n");
for (i = 0; i < 10; i++)
{
scanf("%d", &array[i]);
}
quicksort(array, 0, 9);
printf("Printing Sorted array...\n");
for (i = 0; i < 10; i++)
{
printf("%d ", array[i]);
}
return 0;
}
void quicksort(int array[10], int start, int end)
{
int p;
if (start < end)
{
p = partition(array, start, end);
quicksort(array, start, p-1);
quicksort(array, p+1, end);
}
}
int partition(int array[10], int left, int right)
{
int i = left;
int j = left;
int pivot = array[right];
//printf("In partition\n");
for (j = left; j <= right-1; j++)
{
if (array[j] <= pivot)
{
swap(&array[i], &array[j]);
i++;
}
}
swap(&array[i], &array[right]);
return (i);
}
void swap(int *x, int *y)
{
int temp = *x;
*x = *y;
*y = temp;
}