Book Chapter 03 - Simple Sorting Algorithms
Book Chapter 03 - Simple Sorting Algorithms
Data Structures
Why:
A fundamental data processing operation
Usually done at very large scale, so efficiency matters
Simple approach: Bubble sort
Compare each element (except the last one) with its neighbor to
the right
If they are out of order, swap them
This puts the largest element at the very end
The last element is now in the correct and final place
Compare each element (except the last two) with its neighbor to
the right
If they are out of order, swap them
This puts the second largest element next to last
The last two elements are now in their correct and final places
Compare each element (except the last three) with its neighbor
to the right
Continue as above until you have no unsorted elements on the left
Example of bubble sort
72854 27548 25478 24578
27854 27548 25478 24578
27854 25748 24578 (done)
27584 25478
27548
Code for bubble sort
Analysis of bubble sort
for last in range(self.__nItems-1, 0, -1):
for inner in range(last):
if self.__a[inner] > self.__a[inner+1]:
# code for element swap omitted
12000000
10000000
8000000
O
Time
6000000
4000000
2000000
0
0 1000 2000 3000 4000 5000 6000
sorted
Insertion sort code
Invariant for Insertion Sort
At any given point in the algorithm, the elements to
the left of outer are always in order
When outer == 1 this is trivially true
After each pass, outer increases until it reaches the number
of items
Big O analysis of insertion sort
The 1st time through the outer loop we do one
comparison.
The 2nd time through the outer loop we do one or two
comparisons
The 3rd time through the outer loop we do one, or two, or
three comparisons
The Nth time through the outer loop we do one, two, …
or N comparisons, but on average about N/2
comparisons.
So, the total amount of work (numbers of comparisons
and moves) is roughly:
• (1 + 2 + 3 + 4 + … + N) / 2, or
• N * (N + 1) / 4
Discarding constants, we find that insertion sort is O(N2)
What would the Big-O of insertion sort be, if we were to
Summary
Bubble sort, selection sort, and insertion sort are all O(N2)
As we will see later, we can do much better than this with
somewhat more complicated sorting algorithms
Within O(N2),
Bubble sort is very slow, and should probably never be used for anything
Selection sort is intermediate in speed
Insertion sort is usually the fastest of the three--in fact, for small arrays
(say, 10 or 15 elements), insertion sort is faster than more complicated
sorting algorithms
Selection sort and insertion sort are “good enough” for small
arrays
Shuffling (unsorting) an Array
Even an apparently O(N2) algorithm
can be fast sometimes!
https://www.cs.usfca.edu/~galles/visualization/
ComparisonSort.html
IMPORTANT: Which of the 3 sorting algorithms
works very well if the Array is almost-sorted
already?
How could we prove it?