Implement the quick sort algorithum using recuesive function
Feb 13, 2013 at 1:05pm UTC
Hello, guys I have written this code to arrange user input array in an order. However, I am not able to figure the error on this code that i have witten pls help me out
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
//Recursive Quick Sort
#include<stdio.h>
#define ARRAY_SIZE 10
void quick_sort(int array[], int len)
{
int a[ARRAY_SIZE], c, d, pos;
for (c = 0; c<len; c++)
{
pos = c;
for (d = c+1; c <len; d++)
{
if (a[pos] > a[d]) // error is in this line
pos = d;
}
if (pos !=c)
{
int swap;
swap = a[c];
a[c] = a[pos];
a[pos] = swap;
}
}
printf("Sorted list\n" );
for ( c=0; c<len; c++)
{
printf("%d\n" , a[c]);
}
}
int main(void )
{
int array1[ARRAY_SIZE], index,a[ARRAY_SIZE], c, d, pos;
printf("Enter 10 integer: " );
for (index = 0; index<ARRAY_SIZE; index++)
{
scanf("%d" , &array1[index]);
}
quick_sort(array1, ARRAY_SIZE);
getch();
return 0;
}
Feb 13, 2013 at 1:22pm UTC
What makes you thik that there is an error there?
First I noticed that you passing array bur sorting uninitializated local array a
Feb 13, 2013 at 1:24pm UTC
You are using elements in the a array without initializing them.
Feb 13, 2013 at 1:31pm UTC
it's neither quick sort (instead it's bubblesort) nor recursive.
use array
in place of a
in your quick_sort()
function
Feb 14, 2013 at 8:47am UTC
@ Miinippa I debugged my code form visual studio and every time i run it value at this line are always negative number....
Feb 14, 2013 at 8:48am UTC
@coder777 how can i make it recursive. I cant use for loop...
Feb 14, 2013 at 9:38am UTC
I debugged my code form visual studio and every time i run it value at this line are always negative number....
Of course it will be random number! You are sorting uninitializated local array a
As coder777 says use array instead of a
Topic archived. No new replies allowed.