-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImplement Quick Sort using Divide and Conquer approach .c
More file actions
60 lines (49 loc) · 1.54 KB
/
Implement Quick Sort using Divide and Conquer approach .c
File metadata and controls
60 lines (49 loc) · 1.54 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
#include <stdio.h>
// Function to perform Quick Sort
void quickSort(int arr[], int low, int high) {
if (low < high) {
int pi = partition(arr, low, high);
quickSort(arr, low, pi - 1); // Recursively sort the left subarray
quickSort(arr, pi + 1, high); // Recursively sort the right subarray
}
}
// Partition function to rearrange elements
int partition(int arr[], int low, int high) {
int pivot = arr[high]; // Choose the last element as pivot
int i = (low - 1); // Index of the smaller element
for (int j = low; j < high; j++) {
// If current element is smaller than or equal to pivot
if (arr[j] <= pivot) {
i++; // Increment index of smaller element
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
// Swap the pivot element with the element at index i + 1
int temp = arr[i + 1];
arr[i + 1] = arr[high];
arr[high] = temp;
return (i + 1);
}
int main() {
int n;
// Take user input for array size
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
// Take user input for array elements
printf("Enter the elements: \n");
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
// Perform Quick Sort
quickSort(arr, 0, n - 1);
// Print the sorted array
printf("Sorted array: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}