-
Notifications
You must be signed in to change notification settings - Fork 0
/
46john24.c
68 lines (56 loc) · 1.69 KB
/
46john24.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
/*
Roll no:46
Batch:B
Author Name:John Joy
Date:2/10/23
Description:Program to implement Quick Sort
*/
#include <stdio.h>
// Function to swap two elements in an array
void swap(int* a, int* b) {
int t = *a;
*a = *b;
*b = t;
}
// Function to partition the array into two subarrays, elements less than the pivot
// and elements greater than or equal to the pivot
int partition(int arr[], int low, int high) {
int pivot = arr[high]; // Choose the rightmost element as the pivot
int i = (low - 1); // Index of the smaller element
for (int j = low; j <= high - 1; j++) {
// If the current element is smaller than or equal to the pivot
if (arr[j] <= pivot) {
i++; // Increment index of the smaller element
swap(&arr[i], &arr[j]);
}
}
swap(&arr[i + 1], &arr[high]); // Place the pivot element in its correct position
return (i + 1);
}
// Function to perform Quick Sort
void quickSort(int arr[], int low, int high) {
if (low < high) {
// Partition the array into two subarrays
int pi = partition(arr, low, high);
// Recursively sort the subarrays
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}
int main() {
int n;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d elements:\n", n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
quickSort(arr, 0, n - 1); // Call quickSort to sort the array
printf("Sorted array: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}