Archive

This is an archived tutorial about Flash. It's kept for history and may reference outdated tech.

 

 

Fast Sorting with Quicksort
       by kirupa | 11 July 2006

In the previous page, we just got started with the walkthrough. Let's continue with Step 3 on this page.

Step 3
With both the left loop and right loop terminated at the unordered values, we need to perform the swap. The swap code is the pink section of code:

The swap code in the pseudocode.

Before we swap, we need to make sure that the left value is truly to the left of our right value. In other words, i <= j. This makes sure that we don't perform a reverse swap. Without this check, our code would simply run forever also.

The value of i (0) is still less than the value of j (2). And so the swap begins. The array value referenced by i, 4, is stored in a temporary value:

temp = array[i];

You then set the array value at i to equal the array value at j:

The first part of the swap.

After the value at i is set, you set the temporary value, the value at i you determined a few steps ago, to the value at j:

The second part of the swap.

As a quick observation, notice that your pivot point is still referencing the array value 1 and not the index position of the array value. Finally, you increase the value of i and decrease the value of j. This is done to make sure you are not analyzing the same values again:

The i and j pointers after the swap.

We are about half-way through this article, so feel free stretch for a bit or take a break. There is more on the next page!


Quick Sort6

Quick Sort8

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. www.thehungersite.com