mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-04-01 11:26:43 +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
|
They are synchronized with locks and message passing but other forms of
|
||||||
synchronization could be used.
|
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
|
# lock used to ensure that two processes do not access a pipe at the same time
|
||||||
processLock = Lock()
|
processLock = Lock()
|
||||||
@ -73,15 +73,11 @@ arr = the list to be sorted
|
|||||||
|
|
||||||
|
|
||||||
def OddEvenTransposition(arr):
|
def OddEvenTransposition(arr):
|
||||||
|
|
||||||
processArray = []
|
processArray = []
|
||||||
|
|
||||||
resultPipe = []
|
resultPipe = []
|
||||||
|
|
||||||
# initialize the list of pipes where the values will be retrieved
|
# initialize the list of pipes where the values will be retrieved
|
||||||
for _ in arr:
|
for _ in arr:
|
||||||
resultPipe.append(Pipe())
|
resultPipe.append(Pipe())
|
||||||
|
|
||||||
# creates the processes
|
# creates the processes
|
||||||
# the first and last process only have one neighbor so they are made outside
|
# the first and last process only have one neighbor so they are made outside
|
||||||
# of the loop
|
# of the loop
|
||||||
@ -131,21 +127,15 @@ def OddEvenTransposition(arr):
|
|||||||
for p in range(0, len(resultPipe)):
|
for p in range(0, len(resultPipe)):
|
||||||
arr[p] = resultPipe[p][0].recv()
|
arr[p] = resultPipe[p][0].recv()
|
||||||
processArray[p].join()
|
processArray[p].join()
|
||||||
|
|
||||||
return arr
|
return arr
|
||||||
|
|
||||||
|
|
||||||
# creates a reverse sorted list and sorts it
|
# creates a reverse sorted list and sorts it
|
||||||
def main():
|
def main():
|
||||||
arr = []
|
arr = list(range(10, 0, -1))
|
||||||
|
|
||||||
for i in range(10, 0, -1):
|
|
||||||
arr.append(i)
|
|
||||||
print("Initial List")
|
print("Initial List")
|
||||||
print(*arr)
|
print(*arr)
|
||||||
|
arr = OddEvenTransposition(arr)
|
||||||
list = OddEvenTransposition(arr)
|
|
||||||
|
|
||||||
print("Sorted List\n")
|
print("Sorted List\n")
|
||||||
print(*arr)
|
print(*arr)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user