-
Notifications
You must be signed in to change notification settings - Fork 0
/
QuickSortDiffPivotInC.c
84 lines (66 loc) · 1.79 KB
/
QuickSortDiffPivotInC.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <stdio.h>
void swap(int* a, int* b) {
int temp = *a;
*a = *b;
*b = temp;
}
int choosePivot(int arr[], int low, int high, int pivotChoice) {
if (pivotChoice == 1)
return (low + high) / 2;
else if (pivotChoice == 2)
return low;
else
return high;
}
int partition(int arr[], int low, int high, int pivotChoice) {
int pivotIndex = choosePivot(arr, low, high, pivotChoice);
int pivotValue = arr[pivotIndex];
swap(&arr[pivotIndex], &arr[high]);
int i = low - 1;
int j;
for (j = low; j < high; j++) {
if (arr[j] < pivotValue) {
i++;
swap(&arr[i], &arr[j]);
}
}
swap(&arr[i + 1], &arr[high]);
return i + 1;
}
void quickSort(int arr[], int low, int high, int pivotChoice) {
if (low < high) {
int pivot = partition(arr, low, high, pivotChoice);
quickSort(arr, low, pivot - 1, pivotChoice);
quickSort(arr, pivot + 1, high, pivotChoice);
}
}
int i;
void displayArray(int arr[], int n) {
for (i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}
int main() {
int n, choice;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
int k;
printf("Enter %d elements:\n", n);
for (k = 0; k < n; k++) {
scanf("%d", &arr[k]);
}
printf("Original array: ");
displayArray(arr, n);
printf("\nChoose the pivot:\n");
printf("1. Middle Element\n");
printf("2. First Element\n");
printf("3. Last Element\n");
printf("Enter your choice: ");
scanf("%d", &choice);
quickSort(arr, 0, n - 1, choice);
printf("Sorted array: ");
displayArray(arr, n);
return 0;
}