Sorting - Classes

SORTING ALGORITHMS
OVERVIEW
Why is Sorting important?
 Sorting algorithms
 Comparing Sorting Algorithms

WHY IS SORTING IMPORTANT?

Computers often use large data sets


Sorted data sets are easier to use
Humans like sorted lists

Our brains are comparison engines
SORTING ALGORITHMS
Bubble Sort
 Insertion sort
 Merge Sort

BUBBLE SORT
Slowest Algorithm (Bubble sort has worst-case
and average complexity both О(n2))
 Moves through the array n-1 times

INSERTION SORT
Faster than Bubble Sort but still slow (The worst
case insertion sort has a quadratic running time
(i.e., O(n2)).
 Works by moving an element to anywhere in the
array it should be.
 moves through the array once.

MERGE SORT
Better algorithm (O(n log n)) and can be done
recursively
 Break the array into small parts and sort.
 When the small parts are reassembled, put them
in order

COMPARING SORTING ALGORITHMS
In CS we use O() notation (big 'O' notation)
 It describes the limiting behavior of the function
when the argument tends towards a particular
value or infinity

SOME O() NOTATIONS
Notation
Name
Example
O(1)
Constant
Checking a number
is even or odd
O(log n)
Logarithmic
Finding an item in a
sorted array
O(n)
Linear
Finding an item in an
unsorted array
O(n2)
Quadratic
Bubble Sort
WHAT TO REMEMBER
There are many types of sorting algorithms
 For small data sets, anything is probably quick
enough
 For large data sets, algorithm complexity
becomes very important

REFERENCES
http://en.wikipedia.org/wiki/Bubble_sort
 http://en.wikipedia.org/wiki/Insertion_sort
 http://en.wikipedia.org/wiki/Merge_sort
 http://en.wikipedia.org/wiki/Big_O_notation
