
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Strand Sort in C++
In C++, the strand sort is a recursive sorting algorithm. It is used to extract increasing subsequences repeatedly (called strands) from the input list and merge them into a sorted list. There are multiple libraries that can be used for different purposes. This sorting is one of them.
This sorting technique is particularly good for sorting linked lists, but can be used with arrays too.
The following is a list of approaches for strand sorting in C++:
These approaches is to extract sorted strands (in increasing/descending order) from the unsorted list and merge them one by one into a final sorted list. This continues until the input list is empty.
Basic strand sort using vectors
This is the simplest way to demonstrate the basic strand sort using vectors.
A vector is simply a list that can store items in a specific order for easy access.
Syntax
Following is the syntax for the basic strand sort using vectors:
void strandSort(vector<int>& input, list<int>& output); list<int> merge(list<int>& a, list<int>& b);
Example
In this example, we sort the numbers in ascending order by repeatedly taking increasing subsequences from the input list and combining them into a single sorted list.
#include<iostream> #include<vector> #include<list> using namespace std; list<int>merge(list<int>& a, list<int>& b) { list<int> result; while (!a.empty() && !b.empty()) { if (a.front() < b.front()) { result.push_back(a.front()); a.pop_front(); } else { result.push_back(b.front()); b.pop_front(); } } result.splice(result.end(), a); result.splice(result.end(), b); return result; } void strandSort(vector<int>& input, list<int>& output) { while (!input.empty()) { list<int> strand; strand.push_back(input[0]); input.erase(input.begin()); for (auto it = input.begin(); it != input.end();) { if (*it >= strand.back()) { strand.push_back(*it); it = input.erase(it); } else { ++it; } } output = merge(output, strand); } } int main() { vector<int> input = {10, 5, 30, 40, 2, 4}; list<int> output; strandSort(input, output); for (int val : output) cout << val << " "; return 0; }
Output
The above program produces the following result:
2 4 5 10 30 40
Strand sort in descending order
The Strand sort in descending order means arranging elements from highest to the lowest.
Example
In this example, we organize a list of numbers into descending order by extracting and merging smaller descending sequences from the input.
#include <iostream> #include <vector> #include <list> using namespace std; // Function to merge two lists in descending order list<int> mergeDesc(list<int>& a, list<int>& b) { list<int> result; while (!a.empty() && !b.empty()) { if (a.front() > b.front()) { result.push_back(a.front()); a.pop_front(); } else { result.push_back(b.front()); b.pop_front(); } } result.splice(result.end(), a); result.splice(result.end(), b); return result; } // Function to perform strand sort in descending order void strandSortDesc(vector<int>& input, list<int>& output) { while (!input.empty()) { list<int> strand; strand.push_back(input[0]); input.erase(input.begin()); for (auto it = input.begin(); it != input.end();) { if (*it <= strand.back()) { strand.push_back(*it); it = input.erase(it); } else { ++it; } } output = mergeDesc(output, strand); } } int main() { vector<int> input = {4, 10, 3, 5, 1}; // Example input list<int> output; // Perform strand sort in descending order strandSortDesc(input, output); // Print the sorted output cout << "Sorted list in descending order: "; for (int num : output) { cout << num << " "; } cout << endl; return 0; }
Output
The above program produces the following result:
Sorted list in descending order: 10 5 4 3 1
Strand sort with duplicate values
The Strand sort also works correct when the input contains duplicate values.
Example
In this program, we sort numbers in ascending order by creating subsequences from the input and merging them into the final sorted list.
#include<iostream> #include<vector> #include<list> using namespace std; // Function to perform Strand Sort void strandSort(vector<int>& input, list<int>& output) { while (!input.empty()) { list<int> strand; strand.push_back(input.front()); input.erase(input.begin()); for (auto it = input.begin(); it != input.end();) { if (*it >= strand.back()) { strand.push_back(*it); it = input.erase(it); } else { ++it; } } output.merge(strand); } } int main() { vector<int> input = {8, 3, 8, 1, 3, 5}; list<int> output; strandSort(input, output); for (int val : output) cout << val << " "; return 0; }
Output
The above program produces the following result:
1 3 3 5 8 8