Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

Program :-

#include <stdio.h>
#include <conio.h>
int partition(int a[], int left, int right) {
int pivot = a[left];
int i = left + 1;
int j = right;
int temp;
do {
while (a[i] <= pivot) i++;
while (a[j] > pivot) j--;
if (i < j) {
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
} while (i < j);
temp = a[left];
a[left] = a[j];
a[j] = temp;
return j;
}
void quickSort(int a[], int left, int right) {
int partIndex;
if (left < right) {
partIndex = partition(a, left, right);
quickSort(a, left, partIndex - 1);
quickSort(a, partIndex + 1, right);
}
}
int main() {
int arr[20], i, n;
clrscr();
printf("Name:Aditi Gharat");
printf("\nDiv:A Roll no:319");
printf("\nEnter no. of elements in array: ");
scanf("%d", &n);
printf("Enter elements in array:\n");
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
quickSort(arr, 0, n - 1);
printf("The sorted array is:\n");
for (i = 0; i < n; i++) {
printf("%d\t", arr[i]);
}
getch();
return 0;
}

Output :-

You might also like