Stability of quicksort partitioning approach

后端 未结 8 2063
隐瞒了意图╮
隐瞒了意图╮ 2020-12-05 01:12

Does the following Quicksort partitioning algorithm result in a stable sort (i.e. does it maintain the relative position of elements with equal values):

  pa         


        
8条回答
  •  一向
    一向 (楼主)
    2020-12-05 01:23

    Your code looks suspiciously similar to the sample partition function given on wikipedia which isn't stable, so your function probably isn't stable. At the very least you should make sure your pivot point r points to the last position in the array of values equal to A[r].

    You can make quicksort stable (I disagree with Matthew Jones there) but not in it's default and quickest (heh) form.

    Martin (see the comments) is correct that a quicksort on a linked list where you start with the first element as pivot and append values at the end of the lower and upper sublists as you go through the array. However, quicksort is supposed to work on a simple array rather than a linked list. One of the advantages of quicksort is it's low memory footprint (because everything happens in place). If you're using a linked list you're already incurring a memory overhead for all the pointers to next values etc, and you're swapping those rather than the values.

提交回复
热议问题