Presentation is loading. Please wait.

Presentation is loading. Please wait.

Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Assignment 5 is posted. Exercise 8 is very similar to what you will be doing with assignment 5. Exam.

Similar presentations


Presentation on theme: "Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Assignment 5 is posted. Exercise 8 is very similar to what you will be doing with assignment 5. Exam."— Presentation transcript:

1 Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Assignment 5 is posted. Exercise 8 is very similar to what you will be doing with assignment 5. Exam preparation/topics page is posted. Assn 4 and Quiz 4 sample solutions are posted.

2 Today… Algorithms, Cont.: –Compare search timings. –Start Sorting: Insertion Selection Bubble Winter 2016CISC101 - Prof. McLeod2

3 3 Algorithms, Cont. - Searching We will look at two algorithms: Sequential Search –Brute force! –Works on a dataset in any order. Binary Search –Fast! –Only works on sorted datasets. Winter 2016

4 CISC101 - Prof. McLeod4 Binary Search – Timings See TimingBothSearchesDemo.py Does index() work any faster with a sorted() list? Can index() assume the list is sorted? Compare comparison counts between sequential and binary. Winter 2016

5 Summary You sort datasets just so you can use binary search – it is so much faster! The search functionality built into Python cannot assume that the dataset is sorted, so it will always take longer than our coded binary search. CISC101 - Prof. McLeod5Winter 2016

6 CISC101 - Prof. McLeod6 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: https://www.youtube.com/watch?v=k4RRi_ntQc8) Any of these would be easy to code for smaller datasets. Winter 2016

7 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. CISC101 - Prof. McLeod7Winter 2016

8 CISC101 - Prof. McLeod8 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 2016

9 CISC101 - Prof. McLeod9 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 2016

10 CISC101 - Prof. McLeod10 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 2016

11 CISC101 - Prof. McLeod11 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 2016

12 CISC101 - Prof. McLeod12 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 2016

13 CISC101 - Prof. McLeod13 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 2016

14 CISC101 - Prof. McLeod14 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 2016

15 CISC101 - Prof. McLeod15 2712318117 1227318117 3122718117 3121827117 3 1218277 3711121827

16 CISC101 - Prof. McLeod16 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 it is passed by reference, it stays sorted! Winter 2016

17 CISC101 - Prof. McLeod17 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 2016

18 CISC101 - Prof. McLeod18 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 2016

19 CISC101 - Prof. McLeod19 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 2016

20 CISC101 - Prof. McLeod20 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 2016

21 CISC101 - Prof. McLeod21 2712318117 3122718117 3727181112 3711182712 3711122718 3711121827

22 CISC101 - Prof. McLeod22 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 2016


Download ppt "Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Assignment 5 is posted. Exercise 8 is very similar to what you will be doing with assignment 5. Exam."

Similar presentations


Ads by Google