140 likes | 179 Views
Learn about the Exchange Sort strategy to sort elements efficiently by making n-1 compares of adjacent items and swapping when needed. Follow the step-by-step process to understand how the algorithm works and how to implement it effectively.
E N D
Exchange Sorting CS-240 Dick Steflik
Exchange Sort Strategy Make n-1 compares of adjacent items, swapping when needed Do this n-1 times with one less compare on each pass
Exchange Sort 9 5 7 2 Compare 9 to 5; 9 is greater than 5 so swap them
Exchange Sort 7 2 9 5 Compare 9 to 7, 9 is greater than 7 so swap them
Exchange Sort 7 9 2 5 Compare 9 to 2, 9 is greater than 2 so swap them
Exchange Sort 5 7 2 9 Notice that 9 is where it belongs Start back at the first element...
Exchange Sort 5 7 2 9 Compare 5 to 7, 5 is less than 7 so leave them alone
Exchange Sort 5 7 2 9 Compare 7 to 2, 7 is greater than 2 so swap them
Exchange Sort 5 2 7 9 Notice that 7 is now in the right place Start back at the first element
Exchange Sort 5 2 7 9 Compare 5 to 2, 5 is grerater than 2 so swap them
Exchange Sort 2 5 7 9 End of n-1 pass, sort is done.