Home > Back-end >  Questions about quick sort
Questions about quick sort

Time:09-27

Code is as follows, the problem is when creating an array is too large, such as 100000, the first order is normal, the second behind an orderly array will go wrong when going on? Array hours a few times also won't go wrong, ask the great spirit show
#include
#include
Void the Quick (long num [], int low, int high) {//Quick sort
Long pivotkey;
Int l, h;
L=low;
H=high;
Pivotkey num=[low];//use the first element as the hub
While (lWhile (num [h] & gt;=pivotkey& & LNum=[l] num [h];
While (num [l] <=pivotkey& & H> L) l++;//from right to left to find a bigger than pivotkey element
Num=[h] num [l];
}
Num=[l] pivotkey;
If (low!=l)
The Quick (num, low, l - 1);//recursive sequence of the remaining quick sort
If (high!=l)
The Quick (num, m + 1, high);

}
Int main () {
Long * num;
int n;
Printf (" input array length: \ n ");
The scanf (" % d ", & amp; N);
Num=(long *) malloc sizeof (n * (long));
int i=0;
For (I; iNum [I]=rand () % 1000 + 1;//randomly generated number 1-1000
Printf (" at the end of array is generated, \ n \ n \ n ");
Int flag=1;
While (flag) {
The Quick (num. Zero, n - 1);
flag=0;
Sequence printf (" \ n ");
Printf (" continue to sort please enter 1 \ n ");
The scanf (" % d ", & amp; The flag);
If (flag!
=1)flag=0;
}
return 0;
}
  • Related