-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquickSort.c
More file actions
74 lines (63 loc) · 1.38 KB
/
quickSort.c
File metadata and controls
74 lines (63 loc) · 1.38 KB
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
68
69
70
71
72
73
74
#include <stdio.h>
//For the array size input
int arraySize(){
int s;
printf("Input the array size: ");
scanf("%d", &s);
return s;
}
//For the array input
void arrayInput(int s, int x[]){
printf("Input the array: ");
for(int j = 0; j < s; j++){
scanf("%d", &x[j]);
}
}
//The swapper function
void swap(int *a, int *b){
int temp = *a;
*a = *b;
*b = temp;
}
//The function to partion the array and find the new pivot
int partition(int a[], int l, int r){
//The pivot element
int p = a[r];
int k = l-1;
for(int j = l; j < r; j++){
if(a[j] < p){
k++;
swap(&a[k], &a[j]);
}
}
swap(&a[k+1], &a[r]);
return k+1;
}
//The recursive Quick Sorting algorithm
void quickSort(int a[], int l, int r){
if(l < r){
int p = partition(a, l, r);
quickSort(a, l, p-1);
quickSort(a, p+1, r);
}
return;
}
//For printing out the array's elements on the screen
void arrayOutput(int s, int a[]){
printf("The new array is: ");
for(int k = 0; k < s; k++){
printf("%d ", a[k]);
}
}
int main()
{
//Taking input
int s = arraySize();
int *a = (int *)calloc(s, sizeof(int));
arrayInput(s, a);
//Sorting using quickSort
quickSort(a, 0, s-1);
//Printing the output
arrayOutput(s, a);
return 0;
}