quick sort c++
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 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
|
#include <iostream>
using namespace std;
void quickSort( int * , int , int );
int main(){
int size = 8;
int *numbers = new int[size];
numbers[0] = 14;
numbers[1] = 8;
numbers[2] = 2;
numbers[3] = 67;
numbers[4] = 78;
numbers[5] = 32;
numbers[6] = 4;
numbers[7] = 19;
int left = numbers[0] ;
int right = numbers[7];
quickSort( numbers , left , right );
cout << "array after quick sort : " << endl;
for ( int i = 0 ; i < size ; i++ ){
cout << numbers[0] << " ";
}
system( "pause" );
return 0;
}
void quickSort( int *numbers , int left , int right ){
int i = left , j = right;
int tmp;
int pivot = numbers[( left + right ) / 2];
/*partition*/
while( i <= j ){
while( numbers[i] < pivot ){
i++;
}
while( numbers[j] > pivot ){
j--;
}
if( i <= j ){
tmp = numbers[i];
numbers[i] = numbers[j];
numbers[j] = tmp;
i++;
j--;
}
}
if( left < j ){
quickSort( numbers , left , j );
}
if( i < right ){
quickSort( numbers, i , right );
}
}
|
my program cannot run, when cout it got problem and pop out a error window
1 2 3 4 5 6 7 8 9
|
numbers[0] = 14;
//...
int left = numbers[0] ;//left = 14
//...
quickSort( numbers , left , right );
//...
int i = left , j = right;//i = 14
//...
while( numbers[i] < pivot ){//number[14]? WTF?
|
haha. i change to
1 2
|
left = 0;
right = size -1;
|
worked . what a stupid mistake i have..
and mind have a look for my singly list ?
headache with it..
http://www.cplusplus.com/forum/beginner/97073/
thanks you
Topic archived. No new replies allowed.