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 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
So What? No big deal – just sort it! But what about …
So What? OR the phone numbers of everyone in US
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!
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)
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!
Insertion Sort - Example
Insertion Sort - Example
Insertion Sort - Example
Insertion Sort - Example
Insertion Sort - Example
Insertion Sort - Example
Insertion Sort - Example
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(n 2 )
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 n sublists, 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.
Merge Sort - Example
Merge Sort - Example
Merge Sort - Example
Merge Sort - Example
Merge Sort - Example
Merge Sort - Example
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
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(n 2 ) 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