两种应该掌握的排序方法--------2.quick Sort

介绍

http://zh.wikipedia.org/wiki/%E5%BF%AB%E9%80%9F%E6%8E%92%E5%BA%8F

 

 

用些里面的c++ 实现,感觉这个空间复杂度比较小。还挺好

 
int partition(int* array, int left, int right)
{
        int index = left;

        int pivot = array[index];


        swap(array[index], array[right]);
        for (int i=left; i<right; i++)
        {
                if (array[i] > pivot)  // 降序
                        swap(array[index++], array[i]);
        }
        swap(array[right], array[index]);
        return index;

}
 
void qsort(int* array, int left, int right)
{
        if (left >= right)
                return;
        int index = partition(array, left, right);
        qsort(array, left, index - 1);
        qsort(array, index + 1, right);
}

 

//======================

调用端

    int i_List_[] ={3, 7 ,8, 5, 2, 1, 9, 5, 4};
    int count_ = sizeof(i_List_)/4;
    qsort(i_List_, 0, count_-1);

 

 

 

 

===========


void shellsort1_1(int *data, size_t size)
{
int index = 0;
for (int gap = size / 2; gap > 0; gap /= 2)
for (size_t i = gap; i < size; ++i)
{
int Temp = data[i];
int j = 0;
for (j = i - gap; j >= 0 && data[j] > Temp; j -= gap)
{
data[j + gap] = data[j];
}
data[j + gap] = Temp;

printf("index=%d, \n", index++);
}


}

void bubble_sort(int* unsorted, unsigned int Length)
{
int index = 0;


for (int i = 0; i < Length; i++)
{
for (int j = i; j < Length; j++)
{
if (unsorted[i] > unsorted[j])
{
int temp = unsorted[i];
unsorted[i] = unsorted[j];
unsorted[j] = temp;
}
printf("index=%d, \n", index++);
}
}
}

int main()
{

int a[20];
memset(a, 0, sizeof(int)*20);


//shellsort1_1(a,20);//60
bubble_sort(a,20);//400

return 0;
}

 

posted @ 2014-04-10 20:47  scott_h  阅读(219)  评论(0编辑  收藏  举报