1 / 24

New Mexico Computer Science For All

New Mexico Computer Science For All. Sorting Algorithms Maureen Psaila-Dombrowski. Sorting. Sorting is something we do without even thinking about it. Some one deals us a hand of cards - we put them in order

jpaulson
Download Presentation

New Mexico Computer Science For All

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. New Mexico Computer Science For All Sorting Algorithms Maureen Psaila-Dombrowski

  2. Sorting • Sorting is something we do without even thinking about it. • Some one deals us a hand of cards -we put them in order • Empty the dishwasher – silverware goes one place, plates and glasses go to another…. • Sorting is arranging a group of things (set) according to some criteria: • Increasing/decreasing order • Size • Kind • Class • Brightness • Nearly anything that can describe the items

  3. So What? • No big deal – just sort it! • But what about …

  4. So What? • OR the phone numbers of everyone in US

  5. Sorting in Computer Science • Sorting is extremely and increasingly important. • Data sets can be HUGE • Bring order to the mess • Need order to better access and use the data • Goal of Sorting in Computer Science • Given a set (container) of n elements • Figure out how to sort them (the criteria to use). • Create an Algorithm to sort them • Have the computer sort them!

  6. Sorting Algorithms • Many types of sorting algorithms, a few are: • Insertion Sort • Bubble Sort • Merge Sort • Selection Sort • Quick Sort • Heap Sort • Shell Sort • Each has its place • Look at two of them (sort numbers in ascending order)

  7. Insertion Sort • Commonly done by everyone • don’t think about it • Looks like: • Start with an Unsorted List (think of a hand of cards) • Take the first number  new Sorted List • Then • Take next number from Unsorted List • Put in its correct sorted place in the Sorted List • Keep doing that until entire Unsorted List is Sorted!

  8. Insertion Sort - Example

  9. Insertion Sort - Example

  10. Insertion Sort - Example

  11. Insertion Sort - Example

  12. Insertion Sort - Example

  13. Insertion Sort - Example

  14. Insertion Sort - Example

  15. Insertion Sort • Pros • Very simple to understand • Very simple to implement • Little memory needed • Efficient for small data sets • Cons • Takes a long time • Inefficient for large data sets • Requires a large number of operations (shifts) • O(n2)

  16. Merge Sort • Commonly used by programmers • A Recursive, Divide and Conquer technique • Looks like: • Keep Dividing the unsorted list (recursive) • smaller and smaller lists • until you get nsublists, each containing 1 element • a list of 1 element is considered sorted • Keep Merge and Sort pairs of lists • compare the first element in each unsorted list and put the smallest into a new sorted list • each iteration creates larger lists • repeat until all lists are sorted.

  17. Merge Sort - Example

  18. Merge Sort - Example

  19. Merge Sort - Example

  20. Merge Sort - Example

  21. Merge Sort - Example

  22. Merge Sort - Example

  23. Merge Sort • Pros • Takes less memory than other sorting methods • Takes less time than other sorting methods • O(n(logn)) • Cons • More difficult to understand • More difficult to implement

  24. Summary • Sorting: arranging a group of things (set) according to some criteria (Increasing/decreasing order, Size, Kind…) • Sorting is extremely and increasingly important. • Many types of sorting algorithms • Insertion Sort: Commonly done by everyone • Very simple to understand and implement, Little memory needed, Efficient for small data sets • Takes a long time, Inefficient for large data sets, Requires a large number of operations (shifts), and is O(n2) • Merge Sort: Commonly used by Programmers • A Recursive, Divide and Conquer technique • Takes less time and memory than other sorting methods (O(n(logn)) • Somewhat more difficult to understand and implement

More Related