Strand Sort in Python Last Updated : 01 Jun, 2024 Comments Improve Suggest changes Like Article Like Report Strand Sort is a sorting algorithm that works by repeatedly pulling out sorted subsequences from the unsorted list and merging them to form a sorted list. It is particularly useful for linked lists due to its efficiency in handling such data structures. Strand Sort Algorithm:Initialize:Start with an empty output list.Extract Sorted Subsequences:Repeatedly extract sorted strands from the input list.Merge Strands:Merge the extracted strands into the output list.How Strand Sort works?Consider the list: [4, 3, 6, 1, 7, 2, 5] Initialization:Output list: []Input list: [4, 3, 6, 1, 7, 2, 5]Extract Strands:First strand: [4, 6, 7]Remaining list: [3, 1, 2, 5]Merge [4, 6, 7] into output list: [4, 6, 7]Second strand: [3, 5]Remaining list: [1, 2]Merge [3, 5] into output list: [3, 4, 5, 6, 7]Third strand: [1, 2]Remaining list: []Merge [1, 2] into output list: [1, 2, 3, 4, 5, 6, 7]Termination:The input list is empty.How to implement Strand Sort in Python?Initialization:Create an empty list for the sorted output.While the Input List is Not Empty:Extract the first strand (a sorted subsequence) from the input list.Merge this strand into the output list.Extracting a Strand:Start from the first element of the input list.Iterate through the list, and whenever an element is greater than or equal to the last added element in the strand, add it to the strand.Remove the added elements from the input list.Merging:Merge the extracted strand with the output list to maintain the sorted order.Python Implementation for Strand Sort:Here’s the implementation of the Strand Sort algorithm in Python: Python def merge(sorted_list, strand): result = [] i = j = 0 while i < len(sorted_list) and j < len(strand): if sorted_list[i] <= strand[j]: result.append(sorted_list[i]) i += 1 else: result.append(strand[j]) j += 1 result.extend(sorted_list[i:]) result.extend(strand[j:]) return result def strand_sort(arr): sorted_list = [] while arr: strand = [arr.pop(0)] i = 0 while i < len(arr): if arr[i] > strand[-1]: strand.append(arr.pop(i)) else: i += 1 sorted_list = merge(sorted_list, strand) return sorted_list # Example usage: arr = [4, 3, 6, 1, 7, 2, 5] sorted_arr = strand_sort(arr) print("Sorted array:", sorted_arr) OutputSorted array: [1, 2, 3, 4, 5, 6, 7] Complexity Analysis:Time Complexity: The worst-case time complexity is O(n^2), where n is the number of elements in the array. This is due to the repeated extraction and merging processes.Space Complexity: The space complexity is O(n), as additional space is required to store the intermediate strands and the final sorted list. Comment More infoAdvertise with us Next Article Must Do Coding Questions - Topic-wise S srinam Follow Improve Article Tags : DSA Python-DSA Similar Reads Interview PreparationInterview Preparation For Software DevelopersMust Coding Questions - Company-wise Must Do Coding Questions - Topic-wiseCompany-wise Practice ProblemsCompany PreparationCompetitive ProgrammingSoftware Design-PatternsCompany-wise Interview ExperienceExperienced - Interview ExperiencesInternship - Interview ExperiencesPractice @GeeksforgeeksProblem of the DayTopic-wise PracticeDifficulty Level - SchoolDifficulty Level - BasicDifficulty Level - EasyDifficulty Level - MediumDifficulty Level - HardLeaderboard !!Explore More...Data StructuresArraysLinked ListStackQueueBinary TreeBinary Search TreeHeapHashingGraphAdvance Data StructuresMatrixStringAll Data StructuresAlgorithmsAnalysis of AlgorithmsSearching AlgorithmsSorting AlgorithmsPattern SearchingGeometric AlgorithmsMathematical AlgorithmsRandomized AlgorithmsGreedy AlgorithmsDynamic ProgrammingDivide & ConquerBacktrackingBranch & BoundAll AlgorithmsProgramming LanguagesCC++JavaPythonC#Go LangSQLPHPScalaPerlKotlinWeb TechnologiesHTMLCSSJavaScriptBootstrapTailwind CSSAngularJSReactJSjQueryNodeJSPHPWeb DesignWeb BrowserFile FormatsComputer Science SubjectsOperating SystemsDBMSComputer NetworkComputer Organization & ArchitectureTOCCompiler DesignDigital Elec. & Logic DesignSoftware EngineeringEngineering MathematicsData Science & MLComplete Data Science CourseData Science TutorialMachine Learning TutorialDeep Learning TutorialNLP TutorialMachine Learning ProjectsData Analysis TutorialTutorial LibraryPython TutorialDjango TutorialPandas TutorialKivy TutorialTkinter TutorialOpenCV TutorialSelenium TutorialGATE CSGATE CS NotesGate CornerPrevious Year GATE PapersLast Minute Notes (LMNs)Important Topic For GATE CSGATE CoursePrevious Year Paper: CS exams Like