-
-
Notifications
You must be signed in to change notification settings - Fork 46.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Odd-Even Transposition Sort #769
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
""" | ||
This is an implementation of odd-even 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. | ||
""" | ||
from multiprocessing import Process, Pipe, Lock | ||
|
||
#lock used to ensure that two processes do not access a pipe at the same time | ||
processLock = Lock() | ||
|
||
""" | ||
The function run by the processes that sorts the list | ||
|
||
position = the position in the list the prcoess 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 | ||
""" | ||
def oeProcess(position, value, LSend, RSend, LRcv, RRcv, resultPipe): | ||
global processLock | ||
|
||
#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 | ||
for i in range(0, 10): | ||
|
||
if( (i + position) % 2 == 0 and RSend != None): | ||
#send your value to your right neighbor | ||
processLock.acquire() | ||
RSend[1].send(value) | ||
processLock.release() | ||
|
||
#receive your right neighbor's value | ||
processLock.acquire() | ||
temp = RRcv[0].recv() | ||
processLock.release() | ||
|
||
#take the lower value since you are on the left | ||
value = min(value, temp) | ||
elif( (i + position) % 2 != 0 and LSend != None): | ||
#send your value to your left neighbor | ||
processLock.acquire() | ||
LSend[1].send(value) | ||
processLock.release() | ||
|
||
#receive your left neighbor's value | ||
processLock.acquire() | ||
temp = LRcv[0].recv() | ||
processLock.release() | ||
|
||
#take the higher value since you are on the right | ||
value = max(value, temp) | ||
#after all swaps are performed, send the values back to main | ||
resultPipe[1].send(value) | ||
|
||
""" | ||
the function which creates the processes that perform the parallel swaps | ||
|
||
arr = the list to be sorted | ||
""" | ||
def OddEvenTransposition(arr): | ||
|
||
processArray = [] | ||
tempRrcv = None | ||
tempLrcv = None | ||
|
||
resultPipe = [] | ||
|
||
#initialize the list of pipes where the values will be retrieved | ||
for a 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 | ||
tempRs = Pipe() | ||
tempRr = Pipe() | ||
processArray.append(Process(target = oeProcess, args = (0, arr[0], None, tempRs, None, tempRr, resultPipe[0]))) | ||
tempLr = tempRs | ||
tempLs = tempRr | ||
|
||
for i in range(1, len(arr) - 1): | ||
tempRs = Pipe() | ||
tempRr = Pipe() | ||
processArray.append(Process(target = oeProcess, args = (i, arr[i], tempLs, tempRs, tempLr, tempRr, resultPipe[i]))) | ||
tempLr = tempRs | ||
tempLs = tempRr | ||
|
||
processArray.append(Process(target = oeProcess, args = (len(arr) - 1, arr[len(arr) - 1], tempLs, None, tempLr, None, resultPipe[len(arr) - 1]))) | ||
|
||
#start the processes | ||
for p in processArray: | ||
p.start() | ||
|
||
#wait for the processes to end and write their values to the list | ||
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) | ||
print("Initial List") | ||
print(*arr) | ||
|
||
list = OddEvenTransposition(arr) | ||
|
||
print("Sorted List\n") | ||
print(*arr) | ||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
""" | ||
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() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@cclauss We can use the
spawn
method to start the subprocess. Thespawn
method is considered safer than thefork
method. We will get a warning for thefork
method in Python 3.12+.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice! Can you please make this a pull request?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I opened a PR to resolve this.
spawn
start method in multiprocessing programs #11391