Hall Sort
Directly on the code
int Partition1 (seqlist r,int i,int j)
{//Call partition (R,low,high) for R[low:
while (I<j&&r[j].key>=pivot.key)//pivot is equivalent to j--on position I
,//right-to-left scan, find 1th keyword less than pivot.key record R[j]
if (i<j)//Indicates the keyword found for r[j] <pivot.key
R[I++]=R[J];///equivalent to Exchange R[i] and R[j], after exchange I pointer plus 1
while (I<j&&r[i].key<=pivot.key)//pivot equivalent to on position J
i+ +//left-to-right scan, find 1th keyword greater than Pivot.key record R[i]
if (i<j)//indicate found R[i], make R[i].key>pivot.key
r[j--]=r[i]; Equivalent to Exchange R[i] and R[j], after Exchange J pointer minus 1
}//endwhile
R[i]=pivot;//Datum record has been last positioned
return i;
}//partition
Detailed explanation
First step: (initialize) set two pointers I and J, their initial values are the lower bound and upper bounds of the interval, namely I=low,i=high; Select the first record of the unordered area R[i] (i.e. R[low]) as the Datum record, and save it in the variable pivot;
The second step: to scan the J from high to the left until the 1th keyword is found to be less than Pivot.key's record r[j], the r[j]) moved to the position I referred to, which is equivalent to R[j] and Datum r[i] (that is, pivot) is exchanged, Moves the record with the keyword less than the base keyword Pivot.key to the left of the datum, the equivalent of pivot in the interchange R[j], and then the I pointer scans to the right from the i+1 position until it finds a record with a 1th keyword greater than Pivot.key r[i], R[i] Move to the position I referred to, which is equivalent to exchanging r[i] and datum r[j], so that the key is greater than the base keyword record to the right of the datum, after the Exchange R[i] is the equivalent of holding pivot, and then the pointer J from the position j-1 start to the left to scan, so alternately change the scanning direction, From each side to the middle of each other, until i=j, I is the final position of the Datum pivot, the pivot is placed in this position to complete a division.
Click the open link to animate the display
quick sorting on the introduction of algorithms
int Partition2 (int *arr, int low, int. high)
{
int sentinel = Arr[high];
int i = low-1;
for (int j=low; j<=high-1; ++j)
{
if (Arr[j] <= Sentinel)
{
i++;
Swap (Arr[i], arr[j]);
}
}
Swap (arr[i+1], Arr[high]);
PrintArray (arr);
return i+1;
}