0% found this document useful (0 votes)
11 views2 pages

Untitled Document

Uploaded by

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

Untitled Document

Uploaded by

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

//quick sort

#include <stdio.h>

void swap(int *a, int *b) {


int temp = *a;
*a = *b;
*b = temp;
}

int partition(int A[], int low, int high) {


int pivot;
pivot = A[low];
int i = low + 1;
int j = high;
while (i <= j) {
while (i <= high && A[i] <= pivot) { // Added boundary check for i
i++;
}
while (A[j] > pivot) {
j--;
}
if (i <= j) {
swap(&A[i], &A[j]);
}
}
swap(&A[low], &A[j]);
return j;
}

void quick(int A[], int low, int high) {


if (low < high) {
int index = partition(A, low, high);
quick(A, low, index - 1);
quick(A, index + 1, high);
}
}

void print(int n, int a[]) {


for (int i = 0; i < n; i++) {
printf("%d ", a[i]);
}
printf("\n"); // Add a newline for better output formatting
}

int main() {
int n, A[100];
printf("Enter the number of elements: ");
scanf("%d", &n);
printf("Enter the elements: ");
for (int i = 0; i < n; i++) {
scanf("%d", &A[i]);
}
printf("Array before sorting:\n");
print(n, A);
quick(A, 0, n - 1);
printf("Array after sorting is:\n");
print(n, A);
}

You might also like