Lecture 23 Simple Sorting Annotated

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 22

Lecture 23 – Simple Sorting Algorithms

Last last lecture


 We saw how to …
 … search data structures (arrays) two ways:
 Linear search
 Time efficiency of worst case scenario: O(n)

 Space efficiency: O(1)

 Binary search Most time


efficient since
 Time efficiency of worst case scenario: O(log2 n)
log2 n < n
 Space efficiency: O(1)

Copyright © Anne Lavergne, School of Computing Science, Simon Fraser University 2


Today’s menu
 Investigate sorting algorithms
 bubble sort -> Lab 7
 selection sort
 insertion sort
 Analyze the time and space efficiency of their best, worst and
average case scenarios

Copyright © Anne Lavergne, School of Computing Science, Simon Fraser University 3


Sorting
 Definition: Process of placing elements in a particular sort order based on
the value of a/some search key(s)
•Ascending/descending sort order
 Why sorting?
 Easier to deal with sorted data: easier to search (e.g. binary search)
 Common operation but time consuming
 What can be sorted?
 Internal data (data fits in memory)
 External data (data that must reside on secondary storage)
 How to sort?

Copyright © Anne Lavergne, School of Computing Science, Simon Fraser University 4


How selection sort works
 Array has n elements and the entire array is considered unsorted
 Start with first element (at index 0)

 Until the array is sorted So, the actual


sorting is done
1. Find (i.e., select) the smallest (or largest) element when we select
in the unsorted section of array an element.
 This is done by comparing 1 element with all other elements
2. Swap it with the first element in the unsorted section of array
3. The sorted section of array has just grown by one element

Copyright © Anne Lavergne, School of Computing Science, Simon Fraser University 6


Selection sort is in place algorithm
 in-place: algorithm does not require additional space i.e., array(s) Space Efficiency
Analysis
 Selection sort starts with an unsorted array:

 As the array is being sorted, the unsorted section decreases and the sorted section
increases:

Copyright © Anne Lavergne, School of Computing Science, Simon Fraser University 7


Demo - Let’s have a look at selection sort

Copyright © Anne Lavergne, School of Computing Science, Simon Fraser University 8


Time efficiency analysis of selection sort
Unsorted elements Number of
comparisons required
to select “the one”
(smallest or largest)
n n-1
n-1 n-2
… …
3 2
2 1
1 0
n(n-1)/2
Copyright © Anne Lavergne, School of Computing Science, Simon Fraser University 9
Time efficiency analysis of selection sort
 In total, selection sort …
 … makes n (n – 1) / 2 comparisons
 … performs n – 1 swaps

 Would the way the data is organized affect the number of


operations selection sort perform (affect its time efficiency)?
 For example:
 If the data was already sorted (in the desired sort order, e.g.,
ascending)?
 If the data was sorted but in the other sort order (e.g., descending)?
 If the data was unsorted?
Copyright © Anne Lavergne, School of Computing Science, Simon Fraser University 10
Summary – selection sort
 Time efficiency
 Best case scenario:
 Average case scenario:
 Worst case scenario:

 Space efficiency:

Copyright © Anne Lavergne, School of Computing Science, Simon Fraser University 11


How insertion sort works
 Array has n elements
 At the start, insertion sort considers the first element of the array to be already
sorted -> sorted section
 Starts with the second element (at index 1)
 Until the array is sorted
1. Pick the first element of the unsorted section and store it in a temp variable
2. Comparing it with each element of the sorted section (starting by the last
element and moving towards the first element), looking for where in this
sorted section it should be placed (i.e., in its proper sort order)
3. Shift the elements in the sorted section up one position (starting with the last So, the actual
element of sorted section all the way down to the desired place) to make space sorting is done
for this element (if needed) when we insert
an element.
4. Insert it in this newly vacated place in the sorted section
Copyright © Anne Lavergne, School of Computing Science, Simon Fraser University 13
Insertion sort is in place algorithm
 in-place: algorithm does not require additional space i.e., array(s) Space Efficiency
Analysis
 Insertion sort starts with a sorted section of 1 element:

 As the array is being sorted, the unsorted section decreases and the sorted section
increases:

Copyright © Anne Lavergne, School of Computing Science, Simon Fraser University 14


Demo - Let’s have a look at insertion sort

Copyright © Anne Lavergne, School of Computing Science, Simon Fraser University 15


Time efficiency analysis of insertion sort
Sorted Worst-case Worst-case
Elements Comparison Shift
1 1 1
2 2 2
… … …
n-1 n-1 n-1
n(n-1)/2 n(n-1)/2

Copyright © Anne Lavergne, School of Computing Science, Simon Fraser University 16


Time efficiency analysis of insertion sort
 Time efficiency of insertion sort is affected by the way data is organized
in the array to be sorted

 In the best case scenario, the array is

 Requires n - 1 comparisons
 No shift is required

Copyright © Anne Lavergne, School of Computing Science, Simon Fraser University 17


Time efficiency analysis of insertion sort
 In the worst case scenario, the array is

 Every element in sorted section of array has to be shifted


 The outer loop runs n-1 times
In the first iteration, one comparison and one shift

 …
 In the last iteration, n-1 comparisons and n-1 shifts
 For all elements: (n-1) * (n-1) comparisons and shifts

Copyright © Anne Lavergne, School of Computing Science, Simon Fraser University 18


Summary – insertion sort
 Time efficiency
 Best case scenario:
 Average case scenario:
 Worst case scenario:

 Space efficiency:

Copyright © Anne Lavergne, School of Computing Science, Simon Fraser University 19


Conclusion – Simple sorting algorithms
 Insertion sort
 Efficient: for small n’s
 More efficient in practice than most other simple quadratic (i.e., O(n2))
algorithms
 Stable: does not change the relative order of elements with equal keys
 Both sorts
 In-place: only requires a constant amount O(1) of additional memory space

Copyright © Anne Lavergne, School of Computing Science, Simon Fraser University 20


Summary
 We can now …
 Define the best, worst, and average case running time and space
efficiency of
 insertion sort

 selection sort

 Considering the “simple” sorting algorithms, identify the most efficient


one and explain why it is the most efficient

Copyright © Anne Lavergne, School of Computing Science, Simon Fraser University 21


Next Lecture
 Back to C:
 We have seen how to create composite data types (data structures) using
arrays
 We shall introduce another way of creating composite data types (data
structures)
-> struct

Copyright © Anne Lavergne, School of Computing Science, Simon Fraser University 22

You might also like