1 / 11

Exchange Sorting

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.

boulayt
Download Presentation

Exchange Sorting

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Exchange Sorting CS-240 Dick Steflik

  2. 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

  3. Exchange Sort 9 5 7 2 Compare 9 to 5; 9 is greater than 5 so swap them

  4. Exchange Sort 7 2 9 5 Compare 9 to 7, 9 is greater than 7 so swap them

  5. Exchange Sort 7 9 2 5 Compare 9 to 2, 9 is greater than 2 so swap them

  6. Exchange Sort 5 7 2 9 Notice that 9 is where it belongs Start back at the first element...

  7. Exchange Sort 5 7 2 9 Compare 5 to 7, 5 is less than 7 so leave them alone

  8. Exchange Sort 5 7 2 9 Compare 7 to 2, 7 is greater than 2 so swap them

  9. Exchange Sort 5 2 7 9 Notice that 7 is now in the right place Start back at the first element

  10. Exchange Sort 5 2 7 9 Compare 5 to 2, 5 is grerater than 2 so swap them

  11. Exchange Sort 2 5 7 9 End of n-1 pass, sort is done.

More Related