0% found this document useful (0 votes)
4 views

Quick Sort

Uploaded by

Yashi Gupta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Quick Sort

Uploaded by

Yashi Gupta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

#include <stdio.

h>

// Function to swap two elements


void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}

// Partition function
int partition(int A[], int p, int r) {
int x = A[r]; // Pivot element
int i = p - 1;

for (int j = p; j <= r - 1; j++) {


if (A[j] <= x) {
i++;
swap(&A[i], &A[j]);
}
}
swap(&A[i + 1], &A[r]);
return (i + 1);
}

// Quick Sort function


void quickSort(int A[], int p, int r) {
if (p < r) {
int q = partition(A, p, r);
quickSort(A, p, q - 1);
quickSort(A, q + 1, r);
}
}

int main() {
int n;

// Take the size of the array as input


printf("Enter the number of elements: ");
scanf("%d", &n);

int A[n];

// Take array elements as input


printf("Enter %d elements:\n", n);
for (int i = 0; i < n; i++) {
scanf("%d", &A[i]);
}

// Call the quickSort function


quickSort(A, 0, n - 1);

// Print the sorted array


printf("Sorted array: ");
for (int i = 0; i < n; i++) {
printf("%d ", A[i]);
}
printf("\n");

return 0;
}

You might also like