Category:

# What Is Array Sorting?

Article Details
• Written By: Eugene P.
• Edited By: Angela B.
2003-2018
Conjecture Corporation
 Abraham Lincoln's son was rescued from falling off a train platform by Edwin Booth, brother of John Wilkes Booth.  more...

 November 16 ,  1933 :  Diplomatic relations between the US and the Soviet Union were established.  more...
wiseGEEK Slideshows

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.

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.