Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions C/QuickSort.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#include <stdio.h>

int partition(int arr[], int low, int high) {
int pivot = arr[high];
int i = (low - 1);

for (int j = low; j <= high - 1; j++) {
if (arr[j] < pivot) {
i++;
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
int temp = arr[i + 1];
arr[i + 1] = arr[high];
arr[high] = temp;
return (i + 1);
}

void quickSort(int arr[], int low, int high) {
if (low < high) {
int pivotIndex = partition(arr, low, high);
quickSort(arr, low, pivotIndex - 1);
quickSort(arr, pivotIndex + 1, high);
}
}

void printArray(int arr[], int size) {
for (int i = 0; i < size; i++)
printf("%d ", arr[i]);
}

int main() {
printf("Enter the Size of the array\n");
int size;
scanf("%d",&size);
int arr[size];
printf("Enter the values of the array\n");
for(int i=0;i<size;i++)
{
scanf("%d",&arr[i]);
}

printf("Before sorting, array elements are:\n");
printArray(arr, size);

quickSort(arr, 0, size - 1);

printf("\nAfter sorting, array elements are:\n");
printArray(arr, size);

return 0;
}