Fast Sorting with Quicksort
       by kirupa | 11 July 2006

We started with the ActionScript explanation on the previous page, and let's continue from where we left off.
while (i<=j) {
while (arrayInput[i]<pivotPoint) {
i++;
}
while (arrayInput[j]>pivotPoint) {
j--;
}
if (i<=j) {
var tempStore:Number = arrayInput[i];
arrayInput[i] = arrayInput[j];
arrayInput[j] = tempStore;
i++;
j--;
}
}

This is our first while loop, and it loops any code contained within it if the value of i is less than or equal to the value of j.


while (arrayInput[i]<pivotPoint) {
i++;
}

This is the loop for checking on the left side of our pivot value. You check to make sure that the arrayInput value at i is less than the pivot value. If it is, you keep increasing i - the equivalent of moving to the right on your array.


while (arrayInput[j]>pivotPoint) {
j--;
}

To complement the left loop you saw earlier, this section of code corresponds to ensuring that the values on the right side of the pivot value actually belong there. You check to make sure that the array value referenced by j is greater than the pivot value.


if (i<=j) {
var tempStore:Number = arrayInput[i];
arrayInput[i] = arrayInput[j];
i++;
arrayInput[j] = tempStore;
j--;
}

This section of code is responsible for swapping the values between the left and right sides of the pivot value. You first check to make sure that i is less than or equal to the value of j. If i is <= to j, you swap the values.

You first store the value in arrayInput[i] in a new variable called tempStore. Next, you set the value of arrayInput[i] equal to the right-side value referenced by j in arrayInput[j]. Finally, you set the right-sided value arrayInput[j] to tempStore. During this time, you also increment i by one and decrement j by one also.

There is more ActionScript that needs to be explained on the next page!


1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13




SUPPORTERS:

kirupa.com's fast and reliable hosting provided by Media Temple.