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 …
6 23 75 82 1 16 40 93 3 12
So What?
•OR the phone numbers of everyone in US
22 281 131 280 23 33 252 192 233 23481 44 7 104 234 200 118 178 299 3529 181 8 215 139 219 67 132 195 195108 79 185 207 178 108 156 130 237 148244 65 191 211 100 269 254 149 93 201288 132 219 52 214 153 205 54 121 262152 53 249 117 185 283 254 199 216 18185 156 117 80 152 190 192 133 209 16970 47 226 264 0 19 186 148 120 126156 194 176 142 5 2 296 246 229 210
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
13 50 1 23 99
Insertion Sort - Example
13 50 1 23 99
13 50 1 23 99
Insertion Sort - Example
13 50 1 23 99
13 50 1 23 99
13 50 1 23 99
Insertion Sort - Example
13 50 1 23 99
13 50 1 23 99
13 50 1 23 99
13 50 1 23 99
Insertion Sort - Example
13 50 1 23 99
13 50 1 23 99
13 50 1 23 99
13 50 1 23 99
1 13 50 23 99
Insertion Sort - Example
13 50 1 23 99
13 50 1 23 99
13 50 1 23 99
13 50 1 23 99
1 13 50 23 99
1 13 23 50 99
Insertion Sort - Example
13 50 1 23 99
13 50 1 23 99
13 50 1 23 99
13 50 1 23 99
1 13 50 23 99
1 13 23 50 99
1 13 23 50 99
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)
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 - Example27 61 99 1 18 5 42 39
27 61 99 1 18 5 42 39
27 61 99 1 18 5 42 39
27 61 99 1 18 5 42 39
Merge Sort - Example27 61 99 1 18 5 42 39
27 61 99 1 18 5 42 39
27 61 99 1 18 5 42 39
27 61 99 1 18 5 42 39
27 61 1 99 5 18 39 42
Merge Sort - Example27 61 99 1 18 5 42 39
27 61 99 1 18 5 42 39
27 61 99 1 18 5 42 39
27 61 99 1 18 5 42 39
27 61 1 99 5 18 39 42
1
Merge Sort - Example27 61 99 1 18 5 42 39
27 61 99 1 18 5 42 39
27 61 99 1 18 5 42 39
27 61 99 1 18 5 42 39
27 61 1 99 5 18 39 42
11 271 27 61 99
Merge Sort - Example27 61 99 1 18 5 42 39
27 61 99 1 18 5 42 39
27 61 99 1 18 5 42 39
27 61 99 1 18 5 42 39
27 61 1 99 5 18 39 42
11 271 27 61 99 55 185 18 39 42
Merge Sort - Example27 61 99 1 18 5 42 39
27 61 99 1 18 5 42 39
27 61 99 1 18 5 42 39
27 61 99 1 18 5 42 39
27 61 1 99 5 18 39 42
11 271 27 61 99 55 185 18 39 42
11 51 5 181 5 18 271 5 18 27 391 5 18 27 39 421 5 18 27 39 42 61 99
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(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