Category: 

What Is Array Sorting?

Article Details
  • Written By: Eugene P.
  • Edited By: Angela B.
  • Last Modified Date: 21 September 2016
  • Copyright Protected:
    2003-2016
    Conjecture Corporation
  • Print this Article
Free Widgets for your Site/Blog
In late 19th-century London, mail was delivered to residential addresses up to twelve times each day.   more...

September 28 ,  1924 :  Two US military planes complete the first flights around the world.  more...

Array sorting is the process of taking the individual elements of an array and arranging them in some type of logical order according a series of rules defined by the user. The process involves stepping through the array, one element at a time, and testing that element against the surrounding elements to determine whether it needs to be moved to another index within the array. When performing array sorting, there are several algorithms that can be used, especially when the sorting conditions are numerical as opposed to something more arbitrary. Most array-sorting algorithms are measured by their speed and efficiency, with the slowest algorithms being the easiest to program and the fastest being much more complex.

The simplest array-sorting algorithm is called a bubble sort, and it also is the slowest. The process begins with a loop that will step through each element in the array. The current element is compared to the next element in the array and, if the next element is lower in value than the current element, the data at the indices are switched. The drawback to a bubble sort is that it needs to loop through the array several times to make all of the necessary swaps to sort the array. In the most basic implementations, the sort will loop through the entire array one complete time for each element it contains.

Ad

A selection sort uses an algorithm that performs array sorting in a slightly more efficient manner than a bubble sort but still requires multiple iterations through the array. This sort starts by looping through the array to find the lowest valued element. This element is then placed in the first index of the array and some tracking variables are incremented. The cycle then repeats, now looking for the next lowest value that will then be placed in the second index of the array. The process continues until the highest-value element is placed in the last index of the array.

A method of array sorting that can be efficient but sometimes complex to implement is known as a quicksort. Quicksorting involves taking a value that is in the middle of all the possible values held in the array. The algorithm walks through all the elements of the array and puts all values greater than the median number at the end of the array, and lower values at the beginning. This process is performed recursively on blocks of the array until, at the end, the entire array is sorted. Assuming the middle value used for the array is fairly accurate, this can be a very fast way to sort.

One factor that can affect an array-sorting algorithm is the means by which the data is tested for equivalency. Simple numbers are easy to compare for which value is greater, but this might not be the case for complex data classes in which multiple conditions need to be compared. The longer it takes to compare whether one element is greater than or less than another, the longer it will take for the algorithm to sort the array.

Ad

You might also Like

Recommended

Discuss this Article

Post your comments

exception 'Exception' with message 'error writing captcha: Duplicate entry '2147483647' for key 'PRIMARY'' in /ssd/www/wisegeek/public_html/_core/classes/public/Captcha.php:44
Stack trace:
#0 /ssd/www/wisegeek/public_html/_core/controls/public/ControlDiscussionPostBox.php(324): Captcha->createCaptcha()
#1 /ssd/www/wisegeek/public_html/framework/classes/Control.php(104): ControlDiscussionPostBox->preRender(false)
#2 /ssd/www/wisegeek/public_html/framework/classes/Control.php(149): Control->render()
#3 /ssd/www/wisegeek/public_html/tpl/default-nocustom-lu/pages/public/article/article.htm(526): Control->__toString()
#4 /ssd/www/wisegeek/public_html/framework/classes/Control.php(300): require('/ssd/www/wisege...')
#5 /ssd/www/wisegeek/public_html/framework/classes/Control.php(309): Control->requireTpl('pages/public/ar...', Object(PageArticleCom), true)
#6 /ssd/www/wisegeek/public_html/framework/classes/Control.php(131): Control->renderTpl('pages/public/ar...', Object(PageArticleCom))
#7 /ssd/www/wisegeek/public_html/framework/classes/FormDataControl.php(87): Control->renderTemplate()
#8 /ssd/www/wisegeek/public_html/framework/classes/Control.php(109): FormDataControl->renderTemplate()
#9 /ssd/www/wisegeek/public_html/framework/classes/ScriptPage.php(50): Control->render(false)
#10 /ssd/www/wisegeek/public_html/framework/classes/Control.php(149): ScriptPage->render()
#11 /ssd/www/wisegeek/public_html/framework/classes/Page.php(97): Control->__toString()
#12 /ssd/www/wisegeek/public_html/_core/classes/public/PublicFrontController.php(443): Page->processRequest()
#13 /ssd/www/wisegeek/public_html/_core/classes/public/PublicFrontController.php(7): PublicFrontController->renderPage()
#14 /ssd/www/wisegeek/public_html/index.php(11): PublicFrontController::run()
#15 {main}