QUICKSORT OPINION

来源:互联网 发布:软件商城下载 编辑:程序博客网 时间:2024/05/18 00:34

QuickSort

QuickSorting is a typically divide and conquer algorithm.

It continously divides an array into two subarrays until its of each array come into a right order.

The algorithm needs a pivot ,a left point and a right point to be the standard and mark the comparison location.

The pivot is always the middle element of each array . The value of pivot is the reference which is never changed in this comparison . After reordering the array, the number less than pivot is on the left of pivot and the number greater than pivot is on the right of pivot.

To finish reordering , we should use the left and right point .
When we meet the number greater than the pivot on the left , we should copy the number to the pivot location and replace the greater one position with pivot location , then start the point moving on the right side and go on until it completes it.

The weakness is that once you come back to the left or right point , you need to compare the last exchanged left or right one again. You can imagine the picture with 3 arrow of left right and pivot . And before the arrow of left and right exchanging , the pivot arrow alternatively jump between the left and right after one of those moving(– or ++)

The method of dividing and conquering is evidenced by recursively using sorting algorithm to process the divider array.

int Partitioning(int array[],int division,int left,int right){    int temp = array[division];    while(1){        while(array[left] < temp){            left++;        }        if(left >= right) break;        array[division] = array[left];        division = left;        while(array[right] >= temp){            right--;        }        if(left >= right) break;        array[division] = array[right];        division = right;    }    array[division] = temp;    return division;}void Sort(int array[],int left,int right){    if(left >= right)        return;    int division = (left + right)/2;    int part = Partitioning(array,division,left,right);    Sort(array,left,part-1);    Sort(array,part+1,right);}
原创粉丝点击