Content deleted Content added
Added the implementation of the algorithm, along with the animation |
LucasBrown (talk | contribs) m ce |
||
(23 intermediate revisions by 17 users not shown) | |||
Line 1:
{{Short description|Sorting algorithm}}
[[File:StrandSort.gif|thumb|220x220px|Strand Sort Animation]]
'''Strand sort''' is a
The algorithm first moves the first element of a list into a sub-list.<ref name=":0" /> It then compares the last element in the sub-list to each subsequent element in the original list.<ref name=":0" /> Once there is an element in the original list that is greater than the last element in the sub-list, the element is removed from the original list and added to the sub-list.<ref name=":0" /> This process continues until the last element in the sub-list is compared to the remaining elements in the original list.<ref name=":0" /> The sub-list is then merged into a new list.<ref name=":0" /> Repeat this process and merge all sub-lists until all elements are sorted.<ref name=":0" /> This algorithm is called strand sort because there are strands of sorted elements within the unsorted elements that are removed one at a time.<ref name=":0" /> This algorithm is also used in [[J Sort]] for fewer than 40 elements.<ref>{{Cite book|title=Data structures using C : 1000 problems and solutions|last=Sudipta.|first=Mukherjee|date=2008|publisher=Tata McGraw-Hill|isbn=9780070667655|___location=New Delhi|oclc=311311576}}</ref>
== Example ==
'''Step 1:''' Start with a list of numbers: {
'''Step 2:''' Next, move the first element of the list into a new sub-list:
'''Step 3:''' Then, iterate through the original list and compare each number to
* '''Step 4:''' Now compare
* 6 <
* * 8 < 9, so 8 is not added to the sub-list.
* 7 < 9, so 7 is not added to the sub-list.
'''Step 5:''' Now
▲29 > 25 so 29 is added to the sub-list.
'''Step 6:''' Now compare 29 to the remaining elements in the original list until there is a number greater than 29. ▼
'''Step 7:'''
* 4 > 1, so 4 is added to the sub-list and 4 is removed from the original list.
▲After step 7, the original list contains {7, 6, 1, 4}
▲'''Step
▲The sub-list is empty, and the solution list contains {13, 19, 25, 19}
* 0 < 4, so 0 is not added to the sub-list.
* 6 > 4, so 6 is added to the sub-list and is removed from the original list.
'''Step 9:'''
* '''Step 10:'''
'''Step
'''Step 12:''' Move the first element of the original list into sub-list. Sub-list contains {2}.
▲1 < 6 so 1 is not added to the sub-list. 4 < 6 so 4 is not added to the sub-list.
'''Step 13:'''
* 0 < 2, so 0 is not added to the sub-list.
* 3 > 2, so 3 is added to the sub-list and is removed from the original list.
'''Step 14:'''
* 7 > 3, so 7 is added to the sub-list and is removed from the original list.
'''Step 15:''' Since there are no more elements in the original list to compare {7} to, the sub-list is merged with the solution list. The original list now contains {0}, the sub-list is empty, and solution list contains {1, 2, 3, 4, 5, 6, 7, 8, 9}.
▲4 > 1 so 4 is added to the sub-list.
'''Step 16:'''
'''Step 17:''' Since the original list is now empty, the sub-list is merged with the solution list. The solution list now contains
== Implementation ==
Since Strand Sort requires many insertions and deletions, it is best to use a linked list when implementing
package strandSort;
Line 74 ⟶ 85:
/**
* This is a recursive Strand Sort method. It takes in a linked list of
* integers as
* linked list is empty. Then proceeds to the Strand sort algorithm until
* the linked list is empty.
*
* @param origList:
Line 98 ⟶ 109:
// Iterate through the original list, checking if any elements are
// Greater than the element in the sub list.
int index = 0;
for (int j = 0; j < origList.size(); j++) {
Line 108 ⟶ 118:
}
}
// Merge sub-list into solution list.
// There are // Case 1: The first recursive call, add all of the elements to the
// solution list in sequential order
if (k == 0) {
for (int i = 0; i < subList.size(); i++) {
Line 121 ⟶ 130:
}
// Case 2: After the first recursive call if there subList contains▼
else if (subList.size() > 1) {▼
solList.addFirst(subList.get(w));▼
// w = w+1;▼
// This works by comparing the greatest element in the sublist (which is always the last element)
// with the first element in the solution list.
int solStart = 0;
while (!subList.isEmpty()) {
if (subList.get(subEnd) > solList.get(solStart)) {
solStart++;
▲ } else {
▲ // It just gets added to the front of the solution list.
} else {
subList.remove(subEnd);
subEnd--;
▲ }
}
}
strandSortIterative(origList);▼
▲ strandSortIterative(origList);
}
Line 152 ⟶ 163:
LinkedList<Integer> origList = new LinkedList<Integer>();
// Add the following integers to the linked list: {
origList.add(
origList.add(7);▼
origList.add(19);▼
origList.add(6);▼
origList.add(25);▼
origList.add(29);▼
origList.add(1);
origList.add(4);
▲ origList.add(6);
origList.add(3);
origList.add(8);
▲ origList.add(7);
strandSortIterative(origList);
Line 175 ⟶ 187:
</syntaxhighlight>
== References ==
<!-- Inline citations added to your article will automatically display here. See https://en.wikipedia.org/wiki/WP:REFB for instructions on how to add citations. -->
{{reflist}}
[[Category:Sorting algorithms]]
|