Strand sort is a recursive sorting algorithm that sorts items of a list into increasing order. It has O(n²) worst time complexity which occurs when the input list is reverse sorted. It has a best case time complexity of O(n) which occurs when the input is a list that is already sorted.
Given a list of items, sort them in increasing order.
Examples:
Input: ip[] = {10, 5, 30, 40, 2, 4, 9}
Output: op[] = {2, 4, 5, 9, 10, 30, 40}
Input: ip[] = {1, 10, 7}
Output: op[] = {1, 7, 10}
Illustrations:
Let, input[] = {10, 5, 30, 40, 2, 4, 9}
Initialize: output[] = {}, sublist[] = {}
Move first item of input to sublist.
sublist[] = {10}
Traverse remaining items of input and if current element is greater than last item of sublist, move this item from input to sublist.
Now, sublist[] = {10, 30, 40}, input[] = {5, 2, 4, 9}
Merge sublist into output.
op = {10, 30, 40}
Next recursive call: Move first item of input to sublist. sublist[] = {5}
Traverse remaining items of input and move elements greater than last inserted.
input[] = {2, 4}
sublist[] = {5, 9}
Merge sublist into op.
output = {5, 9, 10, 30, 40}
Last Recursive Call:
{2, 4} are first moved to sublist and then merged into output.
output = {2, 4, 5, 9, 10, 30, 40}
Below are simple steps used in the algorithm:
- Let ip[] be input list and op[] be output list.
- Create an empty sublist and move first item of ip[] to it.
- Traverse remaining items of ip. For every item x, check if x is greater than last inserted item to sublist. If yes, remove x from ip and add at the end of sublist. If no, ignore x (Keep it in ip)
- Merge sublist into op (output list)
- Recur for remaining items in ip and current items in op.
Below is the implementation of above algorithm in C++ and Javascript. The C++ implementation uses list in C++ STL.
CPP
#include <bits/stdc++.h>
using namespace std;
void strandSort(list< int > &ip, list< int > &op)
{
if (ip.empty())
return ;
list< int > sublist;
sublist.push_back(ip.front());
ip.pop_front();
for ( auto it = ip.begin(); it != ip.end(); ) {
if (*it > sublist.back()) {
sublist.push_back(*it);
it = ip.erase(it);
}
else
it++;
}
op.merge(sublist);
strandSort(ip, op);
}
int main( void )
{
list< int > ip{10, 5, 30, 40, 2, 4, 9};
list< int > op;
strandSort(ip, op);
for ( auto x : op)
cout << x << " ";
return 0;
}
|
Javascript
function strandSort(ip)
{
var sublist=[];
sublist.push(ip[0]);
ip.shift();
var len=ip.length-1;
var len2=sublist.length-1;
var it =0;
while (it<=len){
if (ip[it] >sublist[len2]) {
sublist.push(ip[it]);
len2++;
ip.splice(it,1);
}
else {
it++;
}
}
while (sublist.length>0 && op.length>0){
if (sublist[0]>=op[0]){opp.push(op.shift());}
else {opp.push(sublist.shift());}
}
if (sublist.length==0){
opp=[...opp,...op];
}
if (op.length==0){
opp=[...opp,...sublist];
}
op=[...opp];
opp.length=0;
if (ip.length>0){
strandSort(ip);
}
}
var ip=[10, 5, 30, 40, 2, 4, 9];
var op=[];
var opp=[];
strandSort(ip);
console.log(op);
|
Time complexity: O(N2)
Auxiliary Space: O(N)
More Sorting Algorithms :
Practice Problems on Sorting