Home arrow JavaScript arrow Page 3 - QuickSort in Action
JAVASCRIPT

QuickSort in Action


This is the second part of a two-part article that explains how to use Quicksort. It is a method of sorting a list of elements and putting them in the correct order. Please make sure you have read and understood the first article before you read this one.

Author Info:
By: Chrysanthus Forcha
Rating: 4 stars4 stars4 stars4 stars4 stars / 4
October 27, 2008
TABLE OF CONTENTS:
  1. · QuickSort in Action
  2. · Swapping Elements
  3. · Tackling a Sub-list
  4. · And Then There was One
  5. · Code for Quicksort
  6. · Sorting Strings

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

advertisement
QuickSort in Action - Tackling a Sub-list
(Page 3 of 6 )

We compare the left and right elements of the sub-list and then swap if necessary. The situation is as follows:


I

O

E

i


j


E

O

I

i


j


The left element, E, will be the pivot for the sub-list. We now increment i and decrement j under the same conditions as we did above. We end up with the following situation:


E

O

I

j

i



O is the first element for the variable index i that is higher than E. There is no element for the index variable j that is lower than E, so j ends at the position of E, the first position. We now exchange the left element, E, with the element of the position for j, which is still E. So E is exchanged with itself. The new value for the index variable k is now 0, which is the final position for E in the main list. The sub-list can now be written as:


(E0) [O I]


There is only one sub-list now, the right sub-list; there is no left sub-list. Whenever the computer (sorting program) arrives at the situation where there is no left sub-list, it starts sorting the immediate right sub-list, which in our case is [O I].

There are only two elements in the sub-list. We compare the left and right elements of the sub-list and then swap if necessary. The situation is as follows:


O

I

i

j


I

O

i

j


The left element, I, is the pivot for the sub-list. We now increment i and decrement j under the same conditions as we did above. We end up with the following:


I

O

j

i


O is the first element for the variable index i that is higher than I. There is no element for the index variable j that is lower than I, so j ends at the position of I. We now exchange the left element, I, with the element of the position for j, which is still I. So I is exchanged with itself. The new value for the index variable k is now 1, the position for I in the main list. The sub-list can now be written as:

(I1) [O]

Again, there is only one sub-list, the right sub-list; there is no left sub-list. Remember, whenever the computer (sorting program) arrives at the situation where there is no left sub-list, it starts sorting the immediate right sub-list, which in our case is [O]. There is only one element in this sub-list.


blog comments powered by Disqus
JAVASCRIPT ARTICLES

- More Top jQuery Plugins for Menus
- Top jQuery Tutorials for Beginners
- New UI Framework and SDK for JavaScript Rele...
- JavaScript OpenPGP Tool, Node.js 0.6.3 Avail...
- Yahoo Releases Cocktails Language and Develo...
- Customizing jQuery Slideshows: Dynamic Contr...
- Customizing jQuery Slideshows: the animate()...
- Customizing jQuery Slideshows: slideUp() and...
- Customizing jQuery Slideshows: hide() and sh...
- Web Workers: Performing Calculations in Para...
- More Top JavaScript Frameworks and Libraries
- More Dynamic jQuery Styling Techniques
- The Top JavaScript Libraries
- The Top JavaScript Frameworks
- Dynamic jQuery Styling

Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 



© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 8 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials