Download presentation
Presentation is loading. Please wait.
Published byAllen Roberts Modified over 9 years ago
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 … 62375821164093312
4
So What? OR the phone numbers of everyone in US 222811312802333252192233234 8144710423420011817829935 29181821513921967132195 10879185207178108156130237148 2446519121110026925414993201 2881322195221415320554121262 15253249117185283254199216181 8515611780152190192133209169 7047226264019186148120126 15619417614252296246229210
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 135012399
9
Insertion Sort - Example 135012399 135012399
10
Insertion Sort - Example 135012399 135012399 135012399
11
Insertion Sort - Example 135012399 135012399 135012399 135012399
12
Insertion Sort - Example 135012399 135012399 135012399 135012399113502399
13
Insertion Sort - Example 135012399 135012399 135012399 135012399113502399113235099
14
Insertion Sort - Example 135012399 135012399 135012399 135012399113502399113235099113235099
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(n 2 )
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 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.
17
Merge Sort - Example 27619911854239 27619911854239 27619911854239 27619911854239
18
Merge Sort - Example 27619911854239 27619911854239 27619911854239 27619911854239 27611995183942
19
Merge Sort - Example 27619911854239 27619911854239 27619911854239 27619911854239 27611995183942 1
20
Merge Sort - Example 27619911854239 27619911854239 27619911854239 27619911854239 27611995183942 11271 6199
21
Merge Sort - Example 27619911854239 27619911854239 27619911854239 27619911854239 27611995183942 11271 619955185 3942
22
Merge Sort - Example 27619911854239 27619911854239 27619911854239 27619911854239 27611995183942 11271 619955185 3942 115151815 2715182739151827394215182739426199
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(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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.