Lecture-8 Sorting Algorithms
Lecture-8 Sorting Algorithms
Algorithms
(CMP 4272)
Lecture 8 – Sorting
Outline
• Sorting
• Bubble Sort
• Selection Sort
• Insertion Sort
• Quick Sort
• Merge Sort
• Heap Sort
Sorting
• Put simply, sorting requires us to put things in order; either
• in-place or
• out-of-place.
• In-place sorting takes a list or other ADT as input where items must support
a comparison operation [where they can be compared numerically like
integers or alphabetically like strings]. In-place sorting does not have an
output.
• Out-of-place sorting creates a new list to contain sorted items for output
without modifying the input list.
Sorting in Python
• Python has a built-in sorting operation for lists called
Timsort.
• We’ll learn more about this later, but for the exercises in this
lesson use this operation only to test your own code.
Let’s Do It:
1 2 3 4 5
Sort this list yourself – by hand or on a computer, but do the
• Sweep • Select • Sort the • Split the • Pick a
sorting yourself:
through the leftmost list, sort value
the list min/max item into it then and
and sort and a new bring the pivot
[ 5, 13, 7, 1, 0, 3, 4, 19, 8, 7]
numbers
one at a
remove
them
list and
remove
sorted
sub-lists
others
around
time in from the from the back this as
place list one original together lower /
Most importantly, by oneyour
show list
working so you knowhigher
how you
did it.
These are Standard Sorting Algorithms
Selection Insertion
Bubble Sort Merge Sort Quicksort
Sort Sort
• Sweep • Select the • Sort the • Split the • Pick a
through min/max leftmost list, sort it value and
the list and and item into a then bring pivot
sort remove new list the sorted others
numbers them from and sub-lists around this
one at a the list one remove back as lower /
time in by one from the together higher
place original list
Sorting Algorithms
We’re going to be looking at 6 main sorting algorithms before
seeing how the built-in Python algorithm works.
Sorting
Algorithms
10 23 97 10 23 23 10 23 23
Swapping in Python
• Swapping values requires us to use a temporary variable to hold the swap value
to then insert or set it.
10 23 97 10 23 23 10 97 23
97
• This can be hardcoded with an auxiliary variable getting python to do this for you:
• This is done until the algorithm makes a full sweep through the
list and repeated until a full sweep occurs without swapping any
elements.
Bubble Sort Example: [5,1,4,2,8]
First Pass:
[ 5 1 4 2 8 ] –> [ 1 5 4 2 8 ]: 5 > 1 so swap.
[ 1 5 4 2 8 ] –> [ 1 4 5 2 8 ]: 5 > 4 so swap.
[ 1 4 5 2 8 ] –> [ 1 4 2 5 8 ]: 5 > 2 so swap.
[ 1 4 2 5 8 ] –> [ 1 4 2 5 8 ]: 5 < 8 so don’t swap.
Second Pass:
[ 1 4 2 5 8 ] –> [ 1 4 2 5 8 ]: 4 > 1 so don’t swap.
[ 1 4 2 5 8 ] –> [ 1 2 4 5 8 ]: 4 > 2 so swap.
[ 1 2 4 5 8 ] –> [ 1 2 4 5 8 ]: 4 < 5 so don’t swap.
[ 1 2 4 5 8 ] –> [ 1 2 4 5 8 ]: 5 < 8 so don’t swap.
Selection Sort
Selection Sort Algorithm Steps
1. Set min_index to location 0
2. Find the minimum element in the list
3. Swap with value at location min_index
4. Increment min_index to point to next element
5. Repeat until list is sorted
Bubble Sort vs. Selection Sort
When can bubble sort be more efficient (having less operations)
than selection sort?
Selection Sort Out-of-Place
1. Create a new empty list lst_new
2. Find the minimum element in the original list
[lst_origin]
3. Append the minimum element to list_new.
4. Remove it from lst_origin.
5. Repeat until lst_origin is empty.
Insertion Sort
• Insertion Sort is typically performed in-place by maintaining a sub-list of
sorted elements.
• Each element needs to be inserted into the sorted sub-list [usually on the
left of a list], has to find its appropriate place and is then inserted.
Insertion Sort
• 14 is already in the sorted sub-list.
• We then check where 33 goes, but
it’s already in the correct place.
• Next, we must check 27 and move
this to the middle of the sub-list, etc.
70
50 80
40 [] [] 90
30 []
10 []
QuickSort Code
Max-Heap Property:
• In a max-heap, for every node i, the value of the
parent node is greater than or equal to the values of
its children.
• The largest element is always at the root.
Min-Heap Property:
• In a min-heap, for every node i, the value of the
parent node is less than or equal to the values of its
children.
• The smallest element is always at the root.
Heap Sort - Example
• Once the initial Max Heap is constructed the root node
will be the largest value so this is removed from the tree.
1) check they all work with: arr = [64, 34, 25, 12, 22, 11, 90]
2) import time and use t = time.time() and t2 = time.time() and t2-t will tell
you how many seconds the sort took.
3) import copy and import numpy as np and use the following commands:
lst = np.random.permutation(1000) lst_bubble = copy.deepcopy(lst) to
test on a large array.
THANK YOU