-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathQuick Sort.c
61 lines (53 loc) · 1.01 KB
/
Quick Sort.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
#include<stdio.h>
void swap(int* a, int* b)
{
int t = *a;
*a = *b;
*b = t;
}
int partition (int arr[], int l, int h)
{
int pivot = arr[h];
int i = (l - 1);
//int swap;
for (int j = l; j <= h- 1; j++)
{
if (arr[j] <= pivot)
{
i++;
//swap = arr[i];
//arr[i] = arr[j];
//arr[j] = swap;
swap(&arr[i], &arr[j]);
}
}
swap(&arr[i + 1], &arr[h]);
return (i + 1);
}
void quickSort(int arr[], int l, int h)
{
if (l < h)
{
int pi = partition(arr, l, h);
quickSort(arr, l, pi - 1);
quickSort(arr, pi + 1, h);
}
}
int main()
{
int n,i,arr[100];
printf("Enter the size of the array: ");
scanf("%d",&n);
printf("Enter the elements of the array:");
for(i=0; i<n; i++)
{
scanf("%d",&arr[i]);
}
quickSort(arr, 0, n-1);
printf("After sorting:");
for(i=0; i<n; i++)
{
printf("%d ",arr[i]);
}
return 0;
}