Rotate your phone or change to desktop for better experience

Rotate your phone or change to desktop for better experience

quick sort || DS

 #include <stdio.h>

void quicksort (int list[], int low, int high)

{

int key, i, j, temp;

if (low < high)

{

key = low;

i = low;

j = high;

while (i < j)

{

while (list[i] <= list[key] && i <= high)

i++;


while (list[j] > list[key] && j >= low)

j--;

if (i < j)

{

temp = list[i];

list[i] = list[j];

list[j] = temp;

}

}

temp = list[j];

list[j] = list[key];

list[key] = temp;

quicksort (list, low, j - 1);

quicksort (list, j + 1, high);

}

}

void main ()

{

int a[10],n, i;

printf ("Enter the number of elements: ");

scanf ("%d", &n);

printf ("Enter the elements to be sorted:\n");


for (i = 0; i < n; i++)

scanf ("%d", &a[i]);

quicksort (a, 0, n - 1);

printf ("After applying quick sort\n");

for (i = 0; i < n; i++)

printf ("%d\n ", a[i]);

printf ("\n");

}

Post a Comment

0 Comments