hoare-logic

Explanation of Hoare Partitioning algorithm

我只是一个虾纸丫 提交于 2019-12-07 16:38:46
问题 As per the pseudo-code given in many websites, I have written this Hoare partitioning algorithm, which takes an array, the start and end indexes of the sub-array to be partitioned based on the pivot given. It works fine, but can somebody explain the logic, how it does what it does? Here' the code: def hoare(arr,start,end): pivot = 4 i,j = start,end while i < j: while i < j and arr[i] <= pivot: i += 1 while j >= i and arr[j] > pivot: j -= 1 if i < j: arr[i],arr[j] = arr[j],arr[i] return j

Explanation of Hoare Partitioning algorithm

对着背影说爱祢 提交于 2019-12-05 18:35:53
As per the pseudo-code given in many websites, I have written this Hoare partitioning algorithm, which takes an array, the start and end indexes of the sub-array to be partitioned based on the pivot given. It works fine, but can somebody explain the logic, how it does what it does? Here' the code: def hoare(arr,start,end): pivot = 4 i,j = start,end while i < j: while i < j and arr[i] <= pivot: i += 1 while j >= i and arr[j] > pivot: j -= 1 if i < j: arr[i],arr[j] = arr[j],arr[i] return j There's another variant of the partitioning, the Lomuto algorithm. It does something similar, although