Strand sort

Strand sort

Strand sort is a sorting algorithm. It works by repeatedly pulling sorted sublists out of the list to be sorted and merging them with a result array. Each iteration through the unsorted list pulls out a series of elements which were already sorted, and merges those series together.

The name of the algorithm comes from the "strands" of sorted data within the unsorted list which are removed one at a time. It is a comparison sort due to its use of comparisons when removing strands and when merging them into the sorted array.

The strand sort algorithm is O("n" log "n") in the average case. In the best case (a list which is already sorted) the algorithm is linear, or O("n").In the worst case (a list which is sorted in reverse order) the algorithm is O("n"2).

Strand sort is most useful for data which is stored in a linked list, due to the frequent insertions and removals of data. Using another data structure, such as an array, would greatly increase the running time and complexity of the algorithm due to lengthy insertions and deletions. Strand sort is also useful for data which already has large amounts of sorted data, because such data can be removed in a single strand.

Example

#Parse the Unsorted List once, taking out any ascending (sorted) numbers.
#The (sorted) Sublist is, for the first iteration, pushed onto the empty sorted list.
#Parse the Unsorted List again, again taking out relatively sorted numbers.
#Since the Sorted List is now populated, merge the Sublist into the Sorted List.
#Repeat steps 3-4 until both the Unsorted List and Sublist are empty.

Algorithm

A simple way to express strand sort in pseudocode is as follows: procedure strandSort( A : list of sortable items ) defined as: while length( A ) > 0 clear sublist sublist [ 0 ] := A [ 0 ] remove A [ 0 ] for each i in 0 to length( A ) do: if A [ i ] > sublist [ last ] then append A [ i ] to sublist remove A [ i ] end if end for merge sublist into results end while return results end procedure

Java 1.5 Implementation

public static > List sort(Collection coll) { List results = new ArrayList(); while (!coll.isEmpty()) { ArrayList sublist = new ArrayList(); Iterator i = coll.iterator(); sublist.add(i.next()); i.remove(); while (i.hasNext()) { E val = i.next(); if (val.compareTo(sublist.get(sublist.size()-1)) >= 0) { sublist.add(val); i.remove(); } } if (!results.isEmpty()) { ListIterator li = results.listIterator(); E current = li.next(); while (!sublist.isEmpty()) { if (sublist.get(0).compareTo(current) < 0) { li.previous(); li.add(sublist.remove(0)); } else if (li.hasNext()) { current = li.next(); } else break; } } else results.addAll(sublist); sublist.clear(); } return results;}

PHP Implementation

function strandsort(array $arr) { $result = array(); while (count($arr)) { $sublist = array(); $sublist [] = array_shift($arr); $last = count($sublist)-1; foreach ($arr as $i => $val) { if ($val > $sublist [$last] ) { $sublist [] = $val; unset($arr [$i] ); $last++; } }

if (!empty($result)) { foreach ($sublist as $val) { $spliced = false; foreach ($result as $i => $rval) { if ($val < $rval) { array_splice($result, $i, 0, $val); $spliced = true; break; } } if (!$spliced) { $result [] = $val; } } } else { $result = $sublist; } } return $result;}

References

* Paul E. Black [http://www.nist.gov/dads/HTML/strandSort.html "Strand Sort"] from Dictionary of Algorithms and Data Structures at NIST.


Wikimedia Foundation. 2010.

Игры ⚽ Поможем решить контрольную работу

Look at other dictionaries:

  • Merge sort — Example of merge sort sorting a list of random dots. Class Sorting algorithm Data structure Array Worst case performance O(n log n) …   Wikipedia

  • Counting sort — In computer science, counting sort is an algorithm for sorting a collection of objects according to keys that are small integers; that is, it is an integer sorting algorithm. It operates by counting the number of objects that have each distinct… …   Wikipedia

  • Comb sort — Class Sorting algorithm Data structure Array Worst case performance O(n log n)[1] …   Wikipedia

  • Cocktail sort — Class Sorting algorithm Data structure Array Worst case performance О(n²) Best case performance O(n) …   Wikipedia

  • Pigeonhole sort — Class Sorting algorithm Data structure Array Worst case performance O(N + n), where N is the range of key values and n is the input size Worst case space complexity O(N * n) …   Wikipedia

  • Cycle sort — Example of cycle sort sorting a list of random numbers. Class Sorting algorithm Data structure Array Worst case performance Θ(n2) …   Wikipedia

  • Odd–even sort — Example of odd even transposition sort sorting a list of random numbers. Class Sorting algorithm Data structure Array Worst case performance …   Wikipedia

  • J sort — is an in place sort algorithm that uses strand sort to sort fewer than about 40 items and shuffle sort to sort more. John Cohen claimed to have invented this algorithm. [ [http://groups.google.com/group/fido7.ru.algorithms/msg/26084cdb04008ab3… …   Wikipedia

  • Immortal DNA strand hypothesis — The immortal DNA strand hypothesis was proposed in 1975 by John Cairns as a mechanism for adult stem cells to minimize mutations in their genomes.Cairns, J. 1975. Mutation selection and the natural history of cancer. Nature (London) 255: 197… …   Wikipedia

  • Sorting algorithm — In computer science, a sorting algorithm is an algorithm that puts elements of a list in a certain order. The most used orders are numerical order and lexicographical order. Efficient sorting is important for optimizing the use of other… …   Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”