Programming for Engineers in Python Lecture 9 Sorting

  • Slides: 31
Download presentation
Programming for Engineers in Python Lecture 9: Sorting, Searching and Time Complexity Analysis Autumn

Programming for Engineers in Python Lecture 9: Sorting, Searching and Time Complexity Analysis Autumn 2011 -12 1

Lecture 8: Highlights Design a recursive algorithm by 1. Solving big instances using the

Lecture 8: Highlights Design a recursive algorithm by 1. Solving big instances using the solution to smaller instances 2. Solving directly the base cases Recursive algorithms have 1. Stopping criteria 2. Recursive case(s) 3. Construction of a solution using solution to 2 smaller instances

Today • Information • Importance of quick access to information • How can it

Today • Information • Importance of quick access to information • How can it be done? • Preprocessing the data enables fast access to what we are interested in • Example: dictionary • The most basic data structure in Python: list • Sorting preprocessing • Searching fast access • Time complexity 3

Information There about 20, 000, 000 web pages in the internet 4

Information There about 20, 000, 000 web pages in the internet 4

Sorting • A sorted array is an array whose values are in ascending/descending order

Sorting • A sorted array is an array whose values are in ascending/descending order • Very useful • Sorted array example: 1 2 5 8 9 13 • Super easy in Python – the sorted function 5 67

Why is it Important to Sort Information? • To find a value, and fast!

Why is it Important to Sort Information? • To find a value, and fast! • Finding M values in a list of size N • Naive solution: given a query, traverse the list and find the value • Not efficient, average of N/2 operations per query • Better: sort the array once and than perform each query much faster 6

Why not Use Dictionaries? • Good idea! • Not appropriate for all applications: •

Why not Use Dictionaries? • Good idea! • Not appropriate for all applications: • Find the 5 most similar results to the query • Query's percentile • We will refer to array elements of the form (key, value) 7

Naïve Search in a General Array • Find location of a value in a

Naïve Search in a General Array • Find location of a value in a given array 8

Binary Search (requires a sorted array) • Input: sorted array A, query k •

Binary Search (requires a sorted array) • Input: sorted array A, query k • Output: corresponding value / not found • Algorithm: • Check the middle element in A • If the corresponding key equals k return corresponding value • If k < middle find k in A[0: middle-1] • If k > middle find k in A[middle+1: end] 9

Example Searching for 56 index 0 1 2 3 4 5 6 7 8

Example Searching for 56 index 0 1 2 3 4 5 6 7 8 9 value -5 -3 0 4 8 11 22 56 57 97 10

Example Searching for 4 index 0 1 2 3 4 5 6 7 8

Example Searching for 4 index 0 1 2 3 4 5 6 7 8 9 value -5 -3 0 4 8 11 22 56 57 97 11

Code –Binary Search 12

Code –Binary Search 12

Binary Search – 2 nd Try 13

Binary Search – 2 nd Try 13

Time Complexity • Worst case: • Array size decreases with every recursive call •

Time Complexity • Worst case: • Array size decreases with every recursive call • Every step is extremely fast (constant number of operations - c) • There at most log 2(n) steps • Total of approximately c*log 2(n) • For n = 1, 000 binary search will take 20 steps - much faster than the naive search 14

Time Complexity על רגל אחת • Algorithms complexity is measured by run time and

Time Complexity על רגל אחת • Algorithms complexity is measured by run time and space (memory) • Ignoring quick operations that execute constant number of times (independent of input size) • Approximate time complexity in order of magnitude, denoted with O (http: //en. wikipedia. org/wiki/Big_O_notation) • Example: n = 1, 000 • O(n 2) = constant * trillion (Tera) • O(n) = constant * million (Mega) 15 • O(log 2(n)) = constant * 20

Order of Magnitude n log 2 n n 2 1 0 0 1 16

Order of Magnitude n log 2 n n 2 1 0 0 1 16 4 64 256 8 2, 048 65, 536 4, 096 12 49, 152 16, 777, 216 65, 536 16 1, 048, 565 4, 294, 967, 296 20 20, 971, 520 1, 099, 511, 627, 776 24 402, 653, 183 281, 474, 976, 710, 656 1, 048, 576 16, 777, 216 16

Graphical Comparison 17

Graphical Comparison 17

Code – Iterative Binary Search 18

Code – Iterative Binary Search 18

http: //www. doughellmann. com/Py. MOTW/timeit/ Testing Efficiency Preparations (Tutorial for timeit: http: //www. doughellmann.

http: //www. doughellmann. com/Py. MOTW/timeit/ Testing Efficiency Preparations (Tutorial for timeit: http: //www. doughellmann. com/Py. MOTW/timeit/) 19

Testing Efficiency 20

Testing Efficiency 20

Results 21

Results 21

Until now we assumed that the array is sorted… How to sort an array

Until now we assumed that the array is sorted… How to sort an array efficiently? 22

Bubble Sort Example 7 2 8 5 4 2 7 5 4 8 2

Bubble Sort Example 7 2 8 5 4 2 7 5 4 8 2 5 4 7 8 2 4 5 7 8 2 7 8 5 4 2 5 7 4 8 2 4 5 7 8 2 7 5 8 4 2 5 4 7 8 (done) 2 7 5 4 8 24

Code – Bubble Sort n iterations i iterations constant (n-1 + n-2 + n-3

Code – Bubble Sort n iterations i iterations constant (n-1 + n-2 + n-3 + …. + 1) * const ~ ½ * n 2 25

We showed that it is possible to sort in O(n 2) Can we do

We showed that it is possible to sort in O(n 2) Can we do it faster? Yes! – Merge Sort 27

Comparing Bubble Sort with sorted 28

Comparing Bubble Sort with sorted 28

The Idea Behind Merge Sort • Sorting a short array is much faster than

The Idea Behind Merge Sort • Sorting a short array is much faster than a long array • Two sorted arrays can be merged to a combined sorted array quite fast (O(n)) 29

Generic Sorting • We would like to sort all kinds of data types •

Generic Sorting • We would like to sort all kinds of data types • Ascending / descending order • What is the difference between the different cases? • Same algorithm! • Are we required to duplicate the same algorithm for each data type? 30

The Idea Behind Generic Sorting • Write a single function that will be able

The Idea Behind Generic Sorting • Write a single function that will be able to sort all types in ascending and descending order • What are the parameters? • The list • Ascending/descending order • A comparative function 31