this is an implementation of oddeven transposition sort it works by performing a series of parallel swaps between odd and even pairs of variables in the list this implementation represents each variable in the list with a process and each process communicates with its neighboring processes in the list to perform comparisons they are synchronized with locks and message passing but other forms of synchronization could be used lock used to ensure that two processes do not access a pipe at the same time note this breaks testing on build runner may work better locally processlock lock the function run by the processes that sorts the list position the position in the list the process represents used to know which neighbor we pass our value to value the initial value at listposition lsend rsend the pipes we use to send to our left and right neighbors lrcv rrcv the pipes we use to receive from our left and right neighbors resultpipe the pipe used to send results back to main we perform n swaps since after n swaps we know we are sorted we could stop early if we are sorted already but it takes as long to find out we are sorted as it does to sort the list with this algorithm send your value to your right neighbor receive your right neighbor s value take the lower value since you are on the left send your value to your left neighbor receive your left neighbor s value take the higher value since you are on the right after all swaps are performed send the values back to main the function which creates the processes that perform the parallel swaps arr the list to be sorted oddeventranspositionlistrange10 1 sortedlistrange10 1 true oddeventranspositiona x c sortedx a c true oddeventransposition1 9 42 0 2 8 sorted1 9 42 0 2 8 true oddeventranspositionfalse true false sortedfalse false true true oddeventransposition1 32 0 9 sortedfalse false true false oddeventransposition1 32 0 9 sorted1 0 32 9 0 true unsortedlist 442 98 554 266 491 985 53 529 82 429 oddeventranspositionunsortedlist sortedunsortedlist true unsortedlist 442 98 554 266 491 985 53 529 82 429 oddeventranspositionunsortedlist sortedunsortedlist 1 false initialize the list of pipes where the values will be retrieved creates the processes the first and last process only have one neighbor so they are made outside of the loop start the processes wait for the processes to end and write their values to the list creates a reverse sorted list and sorts it lock used to ensure that two processes do not access a pipe at the same time note this breaks testing on build runner may work better locally process_lock lock the function run by the processes that sorts the list position the position in the list the process represents used to know which neighbor we pass our value to value the initial value at list position lsend rsend the pipes we use to send to our left and right neighbors lrcv rrcv the pipes we use to receive from our left and right neighbors resultpipe the pipe used to send results back to main we perform n swaps since after n swaps we know we are sorted we could stop early if we are sorted already but it takes as long to find out we are sorted as it does to sort the list with this algorithm send your value to your right neighbor receive your right neighbor s value take the lower value since you are on the left send your value to your left neighbor receive your left neighbor s value take the higher value since you are on the right after all swaps are performed send the values back to main the function which creates the processes that perform the parallel swaps arr the list to be sorted odd_even_transposition list range 10 1 sorted list range 10 1 true odd_even_transposition a x c sorted x a c true odd_even_transposition 1 9 42 0 2 8 sorted 1 9 42 0 2 8 true odd_even_transposition false true false sorted false false true true odd_even_transposition 1 32 0 9 sorted false false true false odd_even_transposition 1 32 0 9 sorted 1 0 32 9 0 true unsorted_list 442 98 554 266 491 985 53 529 82 429 odd_even_transposition unsorted_list sorted unsorted_list true unsorted_list 442 98 554 266 491 985 53 529 82 429 odd_even_transposition unsorted_list sorted unsorted_list 1 false initialize the list of pipes where the values will be retrieved creates the processes the first and last process only have one neighbor so they are made outside of the loop start the processes wait for the processes to end and write their values to the list creates a reverse sorted list and sorts it
from multiprocessing import Lock, Pipe, Process








def oe_process(position, value, l_send, r_send, lr_cv, rr_cv, result_pipe):
    process_lock = Lock()

    
    
    
    for i in range(10):
        if (i + position) % 2 == 0 and r_send is not None:
            
            process_lock.acquire()
            r_send[1].send(value)
            process_lock.release()

            
            process_lock.acquire()
            temp = rr_cv[0].recv()
            process_lock.release()

            
            value = min(value, temp)
        elif (i + position) % 2 != 0 and l_send is not None:
            
            process_lock.acquire()
            l_send[1].send(value)
            process_lock.release()

            
            process_lock.acquire()
            temp = lr_cv[0].recv()
            process_lock.release()

            
            value = max(value, temp)
    
    result_pipe[1].send(value)





def odd_even_transposition(arr):
    
    process_array_ = []
    result_pipe = []
    
    for _ in arr:
        result_pipe.append(Pipe())
    
    
    
    temp_rs = Pipe()
    temp_rr = Pipe()
    process_array_.append(
        Process(
            target=oe_process,
            args=(0, arr[0], None, temp_rs, None, temp_rr, result_pipe[0]),
        )
    )
    temp_lr = temp_rs
    temp_ls = temp_rr

    for i in range(1, len(arr) - 1):
        temp_rs = Pipe()
        temp_rr = Pipe()
        process_array_.append(
            Process(
                target=oe_process,
                args=(i, arr[i], temp_ls, temp_rs, temp_lr, temp_rr, result_pipe[i]),
            )
        )
        temp_lr = temp_rs
        temp_ls = temp_rr

    process_array_.append(
        Process(
            target=oe_process,
            args=(
                len(arr) - 1,
                arr[len(arr) - 1],
                temp_ls,
                None,
                temp_lr,
                None,
                result_pipe[len(arr) - 1],
            ),
        )
    )

    
    for p in process_array_:
        p.start()

    
    for p in range(len(result_pipe)):
        arr[p] = result_pipe[p][0].recv()
        process_array_[p].join()
    return arr



def main():
    arr = list(range(10, 0, -1))
    print("Initial List")
    print(*arr)
    arr = odd_even_transposition(arr)
    print("Sorted List\n")
    print(*arr)


if __name__ == "__main__":
    main()