我是否正确理解了以下快速排序算法?

时间:2016-06-11 10:25:39

标签: algorithm sorting

我正在尝试理解给定的算法,这是我的想法:

A是给定的数组... x代表枢轴元素左侧的数字,y代表右侧的数字枢轴元素。 (假设枢轴元素是数组右侧的元素。)A [y]代表枢轴元素?

如果我理解正确,算法首先从xy搜索,直到第一个数字大于或等于A[y],然后从yx搜索直到第一个数字小于或等于A[y]。 之后,如果i未达到j,则交换这两个数字并重复。最后,i左侧的数字小于A[y]j左侧的数字大于A[y] ...同时将A[y]移至Algorithm Quicksort 1 func int div (A array; x, y integer) { 2 num = A[y]; 3 i = x; 4 j = y-1; 5 repeat 6 while (A[i] <= num and i < y) 7 i = i+1; 8 end while; 9 while (A[j] >= num and j > x) 10 j = j-1; 11 end while; 12 if i < j then 13 swap (A[i], A[j]); 14 end if; 15 until i >= j; 16 swap (A[i], A[y]); 17 return i; 18 } 中间。 你怎么看待这件事?我对吗? 也许你可以给一个随机数组的例子?我相信,我不能这样做。

<div layout="row">
  <div layout="column">
    <md-form ng-model="student" name="studentForm" flex="90%">
      <div layout="column" layout-padding>
        <md-input-container class="md-block" flex>
          <label for="firstName">firstName</label>
          <input type="text" ng-model="student.firstName" name="fName" placeholder="firstName" required></input>
        </md-input-container>
        <div ng-messages="studentForm.fName.$error" role="alert">
          <div ng-message="required">Required!!</div>
        </div>
        <md-datepicker name="DOB" ng-model="student.DOB" md-placeholder="Enter DOB" required valid>Enter DOB</md-datepicker>
        <div ng-messages="studentForm.DOB.$error" role="alert">
          <div ng-message="valid">The entered value is not a date!</div>
          <div ng-message="required">This date is required!</div>
        </div>
      </div>
      <md-button ng-click="submit()">Send your message</md-button>
    </md-form>
  </div>
</div>

1 个答案:

答案 0 :(得分:1)

从算法看,x和y看起来像是在数组中标记排序算法的左右边界(为了对整个数组进行排序,你要使用x = 0和y = A.length)。 pivot元素是最右边的元素(在索引y处)。

然后,我从x(左边界)开始,并将每个元素与轴A [y]进行比较。它增加到一个索引,在该索引处元素大于A [y]。 j从y开始(右边界)并且互补:它减少直到它到达一个索引,在该索引处找到一个小于A [y]的元素。

如果i仍然小于j,则交换这两个元素(在索引i和j处),意味着从x到i的所有元素都小于A [y],从j到y的所有元素都大于A [Y]。在这种情况下,索引i和j之间仍然存在元素,这些元素尚未被看到&#39;通过算法。 重复此过程直到最终,整个阵列被分成较低的半部分。 (均小于A [y])和上半部分&#39; (均大于A [y])。然后将一个大于A [y]的元素与A [y]交换,然后返回两个分区之间的索引。

总之,您的算法只将数组划分为小于A [y]的元素和大于A [y]的元素。在两个分区上以递归方式重复此操作最终将对它们进行完全排序。

示例:

A = [ 4 7 9 2 5 1 3 8 6 ]
--> called with x = 0, y = 8 (thus, partitioning the complete array)
e.g. div([ 4 7 9 2 5 1 3 8 6 ], 0, 8)

 xi             j   y
[ 4 7 9 2 5 1 3 8 | 6 ] x=0, y=8, A[y]=6, i=0, j=7

  x i           j   y
[ 4 7 9 2 5 1 3 8 | 6 ] x=0, y=8, A[y]=6, i=1, j=7
A[i]=7 >= A[y]=6 -> first while loop ends

  x i         j     y
[ 4 7 9 2 5 1 3 8 | 6 ] x=0, y=8, A[y]=6, i=1, j=6
A[j]=3 <= A[y]=6 -> second while loop ends

  x i         j     y
[ 4 3 9 2 5 1 7 8 | 6 ] x=0, y=8, A[y]=6, i=1, j=6
swapped A[i] and A[j] (i and j stay the same!) -> repeat until i<j

  x   i       j     y
[ 4 3 9 2 5 1 7 8 | 6 ] x=0, y=8, A[y]=6, i=2, j=6

  x   i     j       y
[ 4 3 9 2 5 1 7 8 | 6 ] x=0, y=8, A[y]=6, i=2, j=5

  x   i     j       y
[ 4 3 1 2 5 9 7 8 | 6 ] x=0, y=8, A[y]=6, i=2, j=5
swapped -> repeat (i<j)

  x     i   j       y
[ 4 3 1 2 5 9 7 8 | 6 ] x=0, y=8, A[y]=6, i=3, j=5

  x       i j       y
[ 4 3 1 2 5 9 7 8 | 6 ] x=0, y=8, A[y]=6, i=4, j=5

  x        ij       y
[ 4 3 1 2 5 9 7 8 | 6 ] x=0, y=8, A[y]=6, i=5, j=5
first while ends again

  x       j i       y
[ 4 3 1 2 5 9 7 8 | 6 ] x=0, y=8, A[y]=6, i=5, j=4
second while ends again

j>i -> don't swap

j>i -> don't repeat

swap A[i] and A[y]
  x       j i       y
[ 4 3 1 2 5 6 7 8 | 9 ] x=0, y=8, A[y]=6, i=5, j=4

return i=5

Now, all elements between x and i are smaller than your ORIGINAL A[y] (which is now different!) and all elements between i+1 and y are smaller than the ORIGINAL A[y].
Next, you'd call div([ 4 3 1 2 5 6 7 8 9 ], 0, 5) and div([ 4 3 1 2 5 6 7 8 9 ], 6, 8) or more abstract: div(A, x, i) and div(A, i+1, y).