mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-27 15:01:08 +00:00
Update odd_even_transposition_parallel.py (#1458)
* Update odd_even_transposition_parallel.py * arr = OddEvenTransposition(arr)
This commit is contained in:
parent
74d96ab355
commit
02b717e364
|
@ -10,7 +10,7 @@ comparisons.
|
|||
They are synchronized with locks and message passing but other forms of
|
||||
synchronization could be used.
|
||||
"""
|
||||
from multiprocessing import Process, Pipe, Lock
|
||||
from multiprocessing import Lock, Pipe, Process
|
||||
|
||||
# lock used to ensure that two processes do not access a pipe at the same time
|
||||
processLock = Lock()
|
||||
|
@ -73,15 +73,11 @@ arr = the list to be sorted
|
|||
|
||||
|
||||
def OddEvenTransposition(arr):
|
||||
|
||||
processArray = []
|
||||
|
||||
resultPipe = []
|
||||
|
||||
# initialize the list of pipes where the values will be retrieved
|
||||
for _ in arr:
|
||||
resultPipe.append(Pipe())
|
||||
|
||||
# creates the processes
|
||||
# the first and last process only have one neighbor so they are made outside
|
||||
# of the loop
|
||||
|
@ -131,21 +127,15 @@ def OddEvenTransposition(arr):
|
|||
for p in range(0, len(resultPipe)):
|
||||
arr[p] = resultPipe[p][0].recv()
|
||||
processArray[p].join()
|
||||
|
||||
return arr
|
||||
|
||||
|
||||
# creates a reverse sorted list and sorts it
|
||||
def main():
|
||||
arr = []
|
||||
|
||||
for i in range(10, 0, -1):
|
||||
arr.append(i)
|
||||
arr = list(range(10, 0, -1))
|
||||
print("Initial List")
|
||||
print(*arr)
|
||||
|
||||
list = OddEvenTransposition(arr)
|
||||
|
||||
arr = OddEvenTransposition(arr)
|
||||
print("Sorted List\n")
|
||||
print(*arr)
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user