-
Notifications
You must be signed in to change notification settings - Fork 0
/
QuickSort.c
53 lines (47 loc) · 1.13 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
#include<stdio.h>
void swap(int* a, int* b) { //Swapping array elements
int t = *a;
*a = *b;
*b = t;
}
void printArray(int arr[], int n){ // Print array
for(int i = 0; i<n; i++){
printf("%d\t",arr[i]);
}
}
int part(int arr[], int low, int high){
int pivot = arr[high];
int i = low-1;
int j;
for(j=low; j <= high-1; j++){
if(arr[j] < pivot){
i++;
swap(&arr[i], &arr[j]);
}
}
swap(&arr[i+1], &arr[high]);
printf("SubArray:\n");
printArray(arr, high-low+1);
printf("\n\n");
return i+1;
}
void quickSort(int arr[], int low, int high){ //Quick Sort function
if(low < high){
int x = part(arr, low, high);
quickSort(arr, low, x-1);
quickSort(arr, x+1, high);
}
}
void main(){
int n;
printf("Enter size of array:\n"); //Input of size of array
scanf("%d", &n);
int arr[n];
printf("Enter elements of the array:\n"); //Input of elements of array
for(int i=0; i<n; i++){
scanf("%d", &arr[i]);
}
quickSort(arr, 0, n-1);
printf("Sorted Array:\n");
printArray(arr, n);
}