English 中文(简体)
快速分治
原标题:QuickSort Partition

我试图从上理解快速算法,而pa执行的速度是:sort(在较大范围内采取静坐制,在较小范围进行插入)。

我将执行pa与地雷相比较,地雷比执行缓慢3倍。 通过描述我们的守则,我发现主要的分裂是Partition

The following is excerpt form paul s Code:

void Partition(int data[], int low , int high){
 int pivot = data[low];
 int fromLow = low;
 int fromHigh = high;
 int ptr = low;

 goto QStart;
 while(true){
    do {
        fromHigh--;
        if(fromLow >= fromHigh)
          goto QEnd;
QStart:;        
    }while(data[fromHigh] > pivot)

    data[ptr] = data[fromHigh];
    ptr = fromHigh;

    do{
        fromLow++;
        if(fromLow >= fromHigh){
          ptr = fromHigh;
          goto QEnd;
        }
    }while(data[fromLow] < pivot)
    data[ptr] = data[fromLow];
    ptr = fromLow;
 }
QEnd:;
   data[ptr] = pivot;
}

如下:

void MyPartition(int data[], int low , int high){

 int pivot = data[low]; 
 int fromLow = low;
 int fromHigh = high;
 int ptr = low;
 while(fromLow != fromHigh){
    if(data[fromHigh] >= pivot)
        fromHigh--;
    else{
        data[ptr] = data[fromHigh];
        ptr = fromHigh;

        while(data[fromLow] <= pivot && fromLow != fromHigh)
            fromLow ++;
        data[ptr] = data[fromLow];
        ptr = fromLow;
    }
 }
 data[ptr] = pivot;
}

These two functions implement the same algorithm, and I believe they have the same BigO:

  1. first, scanning array from high end to low end (right => left) for finding the first value is less than pivot.
  2. then, scanning array from low end to high end (left => right) for finding the first value is greater than pivot.
  3. In either scan, if anything is found, then we "swap it with pivot value", this is logical swap,because pivot value is cached with variable pivot, we can regard variable ptr as the current pivot value position.

是否有人知道,执行pa的速度快于地雷?

www.un.org/Depts/DGACM/index_spanish.htm

int PartitionData2(int * data, int low, int high){
  int pivot = data[low]; 
  int fromLow = low;
  int fromHigh = high;
  int ptr = low;

  while(fromLow < fromHigh){      
    if(data[fromHigh] > pivot)           //  >=  ==>  > 
      fromHigh--;   
    else{
      data[ptr] =data[fromHigh] ;
      ptr = fromHigh;      

      while(data[++fromLow] < pivot &&   //  <=  ==>  < 
            fromLow != fromHigh);

      data[ptr] = data[fromLow];
      ptr = fromLow;
      fromHigh--;                        // antti.huima s idea
    }
  }
  data[ptr] = pivot;
  return ptr;
}

我只是根据阿蒂特·胡马的想法更新法典,这使我的法典成为 codes。

它使我感到困惑,因为它看起来像是贬低价值,等于ot。

UPDATE2: I understand the reason why we need move element which equals to pivot, because if we don t, the two new partitions will be uneven, e.g. there should be one is much larger than another. it eventually go to O(n^2) case.

http://www.cs.princeton.edu/~rs/talks/QuicksortIsOptimal.pdf”rel=“nofollow”>this PDF

问题回答

你们的法典中有一些多余的检查,就是说,pa的法典没有。

例如,行文

   while(data[fromLow] <= pivot && fromLow != fromHigh)

第一次检查是多余的,因为它始终认为,在你开始这种重复处理时,第一种重复数据[来自Low]并不高于原始数据。 原因是,每当你开始这种争执时,你就只能从高纬度的纸面上挥发。 由于随机订购的阵列,这种隔.只能用于一对一对一对 lo(可终止50%的随机粉碎机),实际上,你进行了25%的额外比较,而只有限制检查之前就没有做粉碎比较的pa法,但首先从Low进行加固。

You have the same performance bug in the first loop where you decrease fromHigh, even though it s syntactically different. paul s code doesn t have it... and that s why he needs to goto to QStart :)





相关问题
How to add/merge several Big O s into one

If I have an algorithm which is comprised of (let s say) three sub-algorithms, all with different O() characteristics, e.g.: algorithm A: O(n) algorithm B: O(log(n)) algorithm C: O(n log(n)) How do ...

Grokking Timsort

There s a (relatively) new sort on the block called Timsort. It s been used as Python s list.sort, and is now going to be the new Array.sort in Java 7. There s some documentation and a tiny Wikipedia ...

Manually implementing high performance algorithms in .NET

As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Enumerating All Minimal Directed Cycles Of A Directed Graph

I have a directed graph and my problem is to enumerate all the minimal (cycles that cannot be constructed as the union of other cycles) directed cycles of this graph. This is different from what the ...

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...

热门标签