Open In App

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:

  1. Initialize:
    • Start with an empty output list.
  2. Extract Sorted Subsequences:
    • Repeatedly extract sorted strands from the input list.
  3. Merge Strands:
    • Merge the extracted strands into the output list.

How Strand Sort works?

Consider the list: [4, 3, 6, 1, 7, 2, 5]

  1. Initialization:
    • Output list: []
    • Input list: [4, 3, 6, 1, 7, 2, 5]
  2. 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]
  3. Termination:
    • The input list is empty.

How to implement Strand Sort in Python?

  1. Initialization:
    • Create an empty list for the sorted output.
  2. 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.
  3. 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.
  4. 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)

Output
Sorted 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.



Next Article
Article Tags :

Similar Reads