odd even transposition single threaded Algorithm

The Odd-Even Transposition Single Threaded Algorithm is a simple yet efficient sorting technique based on the concept of comparing and swapping adjacent elements in an array. It is a variation of the bubble sort algorithm and is particularly well-suited for parallel processing in computer systems. The algorithm operates in two phases - odd phase and even phase. During the odd phase, the algorithm compares and swaps adjacent elements with odd indices (i.e., elements at positions 1, 3, 5, etc.) if they are out of order. During the even phase, it performs the same operation on elements with even indices (i.e., elements at positions 0, 2, 4, etc.). The algorithm iterates through these two phases until the entire array is sorted. In the single threaded version of the Odd-Even Transposition Algorithm, the comparison and swapping operations are executed sequentially, without the use of parallel processing. This version can be easily implemented using a single loop to iterate through the array, where the loop counter is incremented by two in each iteration. The algorithm checks if the current iteration is odd or even based on the loop counter, and then compares the adjacent elements with either odd or even indices accordingly. If the elements are out of order, they are swapped. The process is repeated until no more swaps are made, indicating that the array is sorted. Although this single threaded version does not take advantage of the parallelism inherent in the algorithm, it still provides a simple and straightforward approach for sorting arrays on single processor systems.
"""
This is a non-parallelized implementation of odd-even transpostiion sort.

Normally the swaps in each set happen simultaneously, without that the algorithm
is no better than bubble sort.
"""


def OddEvenTransposition(arr):
    for i in range(0, len(arr)):
        for i in range(i % 2, len(arr) - 1, 2):
            if arr[i + 1] < arr[i]:
                arr[i], arr[i + 1] = arr[i + 1], arr[i]
        print(*arr)

    return arr


# creates a list and sorts it
def main():
    list = []

    for i in range(10, 0, -1):
        list.append(i)
    print("Initial List")
    print(*list)

    list = OddEvenTransposition(list)

    print("Sorted List\n")
    print(*list)


if __name__ == "__main__":
    main()

LANGUAGE:

DARK MODE: