O*******d 发帖数: 20343 | 1 贴在这里,面试时也许用得上
void quickSort(int * a, int len)
{
if(len > 1)
{
int pivot;
int *up, *down;
int c;
/* choose the first element as pivot */
pivot = *a;
/* set up and down */
up = a + 1;
down = a + len - 1;
while(up < down)
{
/* partition */
while(up < down && *up <= pivot)
++up;
while(up < down && *down > pivot)
--down;
if(up < down)
{
c = *up;
*up = *down;
| p*u 发帖数: 2454 | 2 #include
#include
#include
// Change this to change the number of elements to be stored and sorted
#define ARRAY_LENGTH 128
template
void quickSort(Type* array, int low, int high)
{
int pivotPosition = -1;
if ( low < high )
{
pivotPosition = partition(array, low, high);
quickSort(array, low, pivotPosition-1);
quickSort(array, pivotPosition+1, high); | b********n 发帖数: 609 | 3 这个好,more generic
【在 p*u 的大作中提到】 : #include : #include : #include : // Change this to change the number of elements to be stored and sorted : #define ARRAY_LENGTH 128 : template : void quickSort(Type* array, int low, int high) : { : int pivotPosition = -1; : if ( low < high )
| O*******d 发帖数: 20343 | 4 赞。 你的思路是用两个index来作partition起点和终点, 而array的起始点不变。 我
的思路是把每个partition的起点和长度传给下一个recursive call。 |
|