Presentation is loading. Please wait.

Presentation is loading. Please wait.

Winter 2018 CISC101 11/19/2018 CISC101 Reminders

Similar presentations


Presentation on theme: "Winter 2018 CISC101 11/19/2018 CISC101 Reminders"— Presentation transcript:

1 Winter 2018 CISC101 11/19/2018 CISC101 Reminders Last quiz this week in lab. Topics in slides from last week. Last assignment due last day of class (April 6). Help page for viewing graded quizzes in onQ is posted in onQ under “Course Resources”. Winter 2018 CISC101 - Prof. McLeod Prof. Alan McLeod

2 Today USATS. Algorithms – Sorting. Winter 2018 CISC101 - Prof. McLeod

3 Sorting – Overview We will look at three simple sorts: Insertion sort
CISC101 Sorting – Overview We will look at three simple sorts: Insertion sort Selection sort Bubble sort Insertion and Selection can be very useful in certain situations. Bubble sort may be the *worst* sorting algorithm known! (See: Any of these would be easy to code for smaller datasets. Winter 2018 CISC101 - Prof. McLeod Prof. Alan McLeod

4 Before We Start… You need to learn these algorithms for the same reasons you needed to learn searching algorithms. The sort() in Python is way faster – it uses Quicksort, which uses recursion – both topics are outside the scope of this course (see CISC121!). Even if we coded Quicksort it would still be slower because of the interpreted vs. compiled issue. Winter 2018 CISC101 - Prof. McLeod

5 Sorting – Overview – Cont.
The first step in sorting is to select the criteria used for the sort and the direction of the sort. It could be ascending numeric order, or alphabetic order by last name, etc. Winter 2018 CISC101 - Prof. McLeod

6 Sorting – Overview – Cont.
How to sort (which algorithm to use)?: How large is the dataset? What will be critical: memory usage or execution time? Is it necessary that a new element be inserted in order or can it be added at the end and the sort deferred? How often will the algorithm be asked to sort a dataset that is already in order, except for a few newly added elements? Or, will it always have to re-sort a completely disordered dataset? Winter 2018 CISC101 - Prof. McLeod

7 Sorting – Overview – Cont.
Sorting algorithms can be compared on the basis of: The number of comparisons for a dataset of size n, The number of data movements (“swaps”) necessary, and How these measures change with n (Analysis of Complexity…). Often need to consider these measures for best case (data almost in order), average case (random order), and worst case (reverse order). Some algorithms behave the same regardless of the state of the data, and others do better depending on how well the data is initially ordered. Winter 2018 CISC101 - Prof. McLeod

8 Sorting – Overview – Cont.
If sorting simple values like integers or key values, then comparisons are easy to carry out and the comparison efficiency of the algorithm may not be as important as the number of data movements. However, if strings or objects are being compared then the number of comparisons would be best kept to a minimum. Finally, the only real measure of what algorithm is the best is an actual measure of elapsed time. The initial choice can be based on theory alone, but the final choice for a time-critical application must be by actual experimental measurement. Winter 2018 CISC101 - Prof. McLeod

9 Sorting – Overview – Cont.
I will be presenting code samples that sort lists of integers into ascending order because this is easiest to understand. However the logic of the algorithm can be applied directly to lists of strings or other objects, provided the search criteria are specified. Descending order usually only requires that you change the comparison from “>” to “<“. Winter 2018 CISC101 - Prof. McLeod

10 CISC101 Insertion Sort Probably the most “instinctive” kind of sort: Find the location for an element and move all others up one, and insert the element. Pseudocode: Loop through list from i=1 to size-1, selecting element at position i = temp. Locate position for temp (position j, where j <= i), and move all elements above j up one location Put temp at position j. Winter 2018 CISC101 - Prof. McLeod Prof. Alan McLeod

11 Insertion Sort, Cont. def insertionSort(numsList):
for i in range(1, len(numsList)): temp = numsList[i] j = i while j > 0 and temp < numsList[j - 1]: numsList[j] = numsList[j - 1] j = j - 1 numsList[j] = temp Winter 2018 CISC101 - Prof. McLeod

12 27 12 3 18 11 7 12 27 3 18 11 7 3 12 27 18 11 7 3 12 18 27 11 7 3 11 12 18 27 7 3 7 11 12 18 27 Winter 2018 CISC101 - Prof. McLeod

13 Aside - Sorting “in situ”
Our code is sorting the list in place. Saves the memory (and time) required to create a copy of the same list in memory. However, this means that once it is sorted, and since a mutable list is passed by reference, it stays sorted! Winter 2018 CISC101 - Prof. McLeod

14 CISC101 Selection Sort This one works by selecting the smallest element and then putting it in its proper location. Pseudocode: Loop through the list from i=0 to one element short of the end of the list. Select the smallest element in the list between i plus one to the end of the array. Swap this value with the value at position i. Winter 2018 CISC101 - Prof. McLeod Prof. Alan McLeod

15 swap() Function First, a swap() function that will be used by this and other sorts, that works in Python: def swap(numsList, pos1, pos2) : numsList[pos1], numsList[pos2] = numsList[pos2], numsList[pos1] Winter 2018 CISC101 - Prof. McLeod

16 swap() Function, Again Another version that can be done in any language: def swap(numsList, pos1, pos2) : temp = numsList[pos1] numsList[pos1] = numsList[pos2] numsList[pos2] = temp Winter 2018 CISC101 - Prof. McLeod

17 Selection Sort, Cont. def selectionSort(numsList): i = 0
size = len(numsList) while i < size - 1: smallestPos = i j = i + 1 while j < size: if numsList[j] < numsList[smallestPos]: smallestPos = j j = j + 1 if smallestPos != i: swap(numsList, i, smallestPos) i = i + 1 Winter 2018 CISC101 - Prof. McLeod

18 27 12 3 18 11 7 3 12 27 18 11 7 3 7 27 18 11 12 3 7 11 18 27 12 3 7 11 12 27 18 3 7 11 12 18 27 Winter 2018 CISC101 - Prof. McLeod

19 Selection Sort, Cont. Selection sort is “swap efficient”, and insertion sort can be efficient for datasets that are mostly in order. Look at the timings in SortingTestBed.py Winter 2018 CISC101 - Prof. McLeod

20 CISC101 Bubble Sort Is best envisioned as a vertical column of numbers as bubbles. The larger bubbles gradually work their way to the top of the column, with the smaller ones pushed down to the bottom. Pseudocode: Loop through list from i=0 to the end: Loop down from the last element in the list to i. Swap adjacent elements if they are in the wrong order. Winter 2018 CISC101 - Prof. McLeod Prof. Alan McLeod

21 Bubble Sort, Cont. def bubbleSort(numsList): size = len(numsList)
for i in range(0, size): j = size - 1 while j > i : if numsList[j] < numsList[j - 1]: swap(numsList, j, j - 1) j = j - 1 Winter 2018 CISC101 - Prof. McLeod

22 27 12 3 18 11 7 3 27 12 7 18 11 3 7 27 12 11 18 3 7 11 27 12 18 3 7 11 12 27 18 3 7 11 12 18 27 Winter 2018 CISC101 - Prof. McLeod

23 Bubble Sort, Cont. Note that both the comparison and the swap are inside the inner loop (yuk!). Winter 2018 CISC101 - Prof. McLeod

24 Bubble Sort – A Slight Improvement
def bitBetterBubbleSort(numsList): size = len(numsList) i = 0 isSorted = False while i < size and not isSorted: j = size - 1 isSorted = True while j > i : if numsList[j] < numsList[j - 1]: swap(numsList, j, j - 1) j = j - 1 i = i + 1 Winter 2018 CISC101 - Prof. McLeod

25 Bubble Sort, Cont. Possibly the simplest sorting algorithm to code.
Also the slowest sorting algorithm! On average, bubble sort makes n (the size of the dataset) times more moves than selection or insertion sort. Winter 2018 CISC101 - Prof. McLeod

26 Sorting Animations Here are a few that are OK:
CISC101 Sorting Animations Here are a few that are OK: And youtube has videos of many other animations. Winter 2018 CISC101 - Prof. McLeod Prof. Alan McLeod

27 Demo Program See ModifiedSortingTestBed.py to experiment with the effects of the state of the data. How do our sorts compare to list.sort()? (Not very well. How is .sort() so fast?) Winter 2018 CISC101 - Prof. McLeod

28 Timings, Summary For 1000 data points between 1 and 1000, random order case. Sort Millisec Python sort() 0.18 Insertion 40 Selection 44 Bubble 102 Better?Bubble 101 Wow! Winter 2018 CISC101 - Prof. McLeod

29 Observations Insertion sort works remarkably well on data that is almost in order. Selection sort doesn’t care much about the state of the data. Bubble sort is always the worst sort, but it behaves slightly better when the data is more ordered. Winter 2018 CISC101 - Prof. McLeod


Download ppt "Winter 2018 CISC101 11/19/2018 CISC101 Reminders"

Similar presentations


Ads by Google