Home arrow JavaScript arrow Page 2 - 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 - Swapping Elements
(Page 2 of 6 )

When the index variables cross, they will be identifying elements, that is, the ith variable will be identifying the next element that is greater that the left element, or it will be identifying the last element if no element was found greater than the left element. Meanwhile, the jth variable will be identifying the next element that is less than the left element or it will be identifying the first (left) element if no element was less than the left element. The situation when our variables cross, is as follows:


P

W

E

I

T

Y

U

R

O

Q




j

i







I

O

E

P

T

Y

U

R

W

Q




j

i







Look at the first of the two tables above carefully. Note that if we swap the present pair and continue to increment i, decrement j, and swap, we shall be reversing our process. We have to stop here.

Actually we can use either the left or the right element as the pivot. Well, we shall use the left element as I said above. It can be shown that the position for the index variable, j, at the moment, is the final (sorted) position for the left element, P. So we swap the left element and the element at position j. Doing this with our array gives us the pivot, P, at its final position. The situation is as follows:


[I O E] (P3) [T Y U R W Q] --------result 1


We have the pivot at position 3, the left sub-list and the right sub-list. The index variable k that we mention above takes its first value of 3. We have to repeat the same process for the left sub-list and right sub-list. Let us start with the left 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