Life Hacks


      

Write a program of the Quick Sort.



#include<iostream.h>

#include<conio.h>

int a[10], l, u, i, j; /* local variable */

void quick(int *, int, int);

void main( ) /* main start */

{

clrscr( );

cout << “ Enter 10 elements : ”;

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

{

cin >> a[i];

} /* End of for loop */

l = 0;

u = 9;

quick(a, l, u);

cout << “ Sorted elements :”;

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

{

cout << a[i] << “ “;

} /* End of for loop */

getch( );

}

void quick(int a[ ], int l, int u)

{

int p, temp; /* local variable */

if(l < u)

{

p = a[l];

i = l;

j = u;

while(i < j)

{

while(a[i] <= p && i < j)

i++;

while(a[j] > p && i <= j)

j–;

if(i <= j)

{

temp = a[i];

a[i] = a[j];

a[j] = temp;

}

} /* End of while loop */

temp = a[j];

a[j] = a[l];

a[l] = temp;

cout << “\n”;

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

{

cout << a[i] << “ “;

} /* End of for loop */

quick(a, l, j – 1);

quick(a, j + 1, u);

}

}

Output:-

clip_image001

Leave a Reply