Top Banner
Lecture 12 Lect. PhD. Arthur Molnar Searching The searching problem Searching algorithms Binary search Search in Python Sorting The sorting problem Selection sort Insertion sort Bubble Sort Quick Sort Lambda Expressions Searching. Sorting. Lambda expressions. Lect. PhD. Arthur Molnar Babes-Bolyai University [email protected]
49

Searching. Sorting. Lambda expressions.arthur/FP2019/Lecture Notes/12.Searching.Sorti… · Binary search Search in Python Sorting The sorting problem Selection sort Insertion sort

Jun 15, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Searching. Sorting. Lambda expressions.arthur/FP2019/Lecture Notes/12.Searching.Sorti… · Binary search Search in Python Sorting The sorting problem Selection sort Insertion sort

Lecture 12

Lect. PhD.Arthur Molnar

Searching

The searchingproblem

Searchingalgorithms

Binary search

Search in Python

Sorting

The sortingproblem

Selection sort

Insertion sort

Bubble Sort

Quick Sort

LambdaExpressions

Searching. Sorting. Lambda expressions.

Lect. PhD. Arthur Molnar

Babes-Bolyai University

[email protected]

Page 2: Searching. Sorting. Lambda expressions.arthur/FP2019/Lecture Notes/12.Searching.Sorti… · Binary search Search in Python Sorting The sorting problem Selection sort Insertion sort

Lecture 12

Lect. PhD.Arthur Molnar

Searching

The searchingproblem

Searchingalgorithms

Binary search

Search in Python

Sorting

The sortingproblem

Selection sort

Insertion sort

Bubble Sort

Quick Sort

LambdaExpressions

Overview

1 SearchingThe searching problemSearching algorithmsBinary searchSearch in Python

2 SortingThe sorting problemSelection sortInsertion sortBubble SortQuick Sort

3 Lambda Expressions

Page 3: Searching. Sorting. Lambda expressions.arthur/FP2019/Lecture Notes/12.Searching.Sorti… · Binary search Search in Python Sorting The sorting problem Selection sort Insertion sort

Lecture 12

Lect. PhD.Arthur Molnar

Searching

The searchingproblem

Searchingalgorithms

Binary search

Search in Python

Sorting

The sortingproblem

Selection sort

Insertion sort

Bubble Sort

Quick Sort

LambdaExpressions

Feedback for the course

You can write feedback at academicinfo.ubbcluj.ro

It is both important as well as anonymous

Write both what you like (so we keep&improve it) andwhat you don’t

Best if you write about all activities (lecture,seminar andlaboratory)

Page 4: Searching. Sorting. Lambda expressions.arthur/FP2019/Lecture Notes/12.Searching.Sorti… · Binary search Search in Python Sorting The sorting problem Selection sort Insertion sort

Lecture 12

Lect. PhD.Arthur Molnar

Searching

The searchingproblem

Searchingalgorithms

Binary search

Search in Python

Sorting

The sortingproblem

Selection sort

Insertion sort

Bubble Sort

Quick Sort

LambdaExpressions

Searching

Data are available in the internal memory, as a sequenceof records (k1, k2, ..., kn)

Search a record having a certain value for one of its fields,called the search key.

If the search is successful, we have the position of therecord in the given sequence.

We approach the search problem’s two possibilitiesseparately:

Searching with unordered keysSearching with ordered keys

Page 5: Searching. Sorting. Lambda expressions.arthur/FP2019/Lecture Notes/12.Searching.Sorti… · Binary search Search in Python Sorting The sorting problem Selection sort Insertion sort

Lecture 12

Lect. PhD.Arthur Molnar

Searching

The searchingproblem

Searchingalgorithms

Binary search

Search in Python

Sorting

The sortingproblem

Selection sort

Insertion sort

Bubble Sort

Quick Sort

LambdaExpressions

Searching - unordered keys

Problem specification

Data: a, n, (ki , i = 0, .., n − 1), where n ∈ N, n ≥ 0.

Results: p, where (0 ≤ p ≤ n − 1, a = kp) or p = −1, ifkey is not found.

Page 6: Searching. Sorting. Lambda expressions.arthur/FP2019/Lecture Notes/12.Searching.Sorti… · Binary search Search in Python Sorting The sorting problem Selection sort Insertion sort

Lecture 12

Lect. PhD.Arthur Molnar

Searching

The searchingproblem

Searchingalgorithms

Binary search

Search in Python

Sorting

The sortingproblem

Selection sort

Insertion sort

Bubble Sort

Quick Sort

LambdaExpressions

Searching - unordered keys

de f s ea r chSeq ( e l , l ) :’ ’ ’Search f o r an e l ement i n l i s te l − e l ementl − l i s t o f e l ement sReturn the p o s i t i o n o f the e lement , −1 i f not

found’ ’ ’poz = −1f o r i i n range (0 , l e n ( l ) ) :

i f e l == l [ i ] :poz = i

r e t u r n poz

Computational complexity is T (n) =n−1∑i=0

1 = n ∈ Θ(n)

Page 7: Searching. Sorting. Lambda expressions.arthur/FP2019/Lecture Notes/12.Searching.Sorti… · Binary search Search in Python Sorting The sorting problem Selection sort Insertion sort

Lecture 12

Lect. PhD.Arthur Molnar

Searching

The searchingproblem

Searchingalgorithms

Binary search

Search in Python

Sorting

The sortingproblem

Selection sort

Insertion sort

Bubble Sort

Quick Sort

LambdaExpressions

Searching - unordered keys

de f s ea r chSeq ( e l , l ) :’ ’ ’Search f o r an e l ement i n l i s te l − e l ementl − l i s t o f e l ement sReturn the p o s i t i o n o f the e lement , −1 i f not

found’ ’ ’i = 0wh i l e i<l e n ( l ) and e l != l [ i ] :

i += 1i f i<l e n ( l ) :

r e t u r n ir e t u r n −1

What is the difference between this and the previous version?

Page 8: Searching. Sorting. Lambda expressions.arthur/FP2019/Lecture Notes/12.Searching.Sorti… · Binary search Search in Python Sorting The sorting problem Selection sort Insertion sort

Lecture 12

Lect. PhD.Arthur Molnar

Searching

The searchingproblem

Searchingalgorithms

Binary search

Search in Python

Sorting

The sortingproblem

Selection sort

Insertion sort

Bubble Sort

Quick Sort

LambdaExpressions

Searching - unordered keys

Best case: the element is at the first position,T (n) ∈ Θ(1).

Worst case: the element is in the n-1 position,T (n) ∈ Θ(n).

Average case: if distributing the element uniformly, theloop can be executed 0, 1, .., n − 1 times, soT (n) = 1+2+..+n−1

n ∈ Θ(n).

Overall complexity is O(n)

Page 9: Searching. Sorting. Lambda expressions.arthur/FP2019/Lecture Notes/12.Searching.Sorti… · Binary search Search in Python Sorting The sorting problem Selection sort Insertion sort

Lecture 12

Lect. PhD.Arthur Molnar

Searching

The searchingproblem

Searchingalgorithms

Binary search

Search in Python

Sorting

The sortingproblem

Selection sort

Insertion sort

Bubble Sort

Quick Sort

LambdaExpressions

Searching - ordered keys

Problem specification

Data: a, n, (ki , i = 0, .., n − 1), where n ∈ N, n ≥ 0, andk0 < k1 < ... < kn−1;

Results: p, where (p = 0 and a ≤ k0) or (p = n anda > kn−1) or (0 < p ≤ n − 1) and (kp−1 < a ≤ kp).

Page 10: Searching. Sorting. Lambda expressions.arthur/FP2019/Lecture Notes/12.Searching.Sorti… · Binary search Search in Python Sorting The sorting problem Selection sort Insertion sort

Lecture 12

Lect. PhD.Arthur Molnar

Searching

The searchingproblem

Searchingalgorithms

Binary search

Search in Python

Sorting

The sortingproblem

Selection sort

Insertion sort

Bubble Sort

Quick Sort

LambdaExpressions

Searching - ordered keys

de f s ea r chSeq ( e l , l ) :’ ’ ’Search f o r an e l ement i n l i s te l − e l ementl − l i s t o f o r d e r ed e l ement sReturn the p o s i t i o n o f the f i r s t occu r r ence , o r

p o s i t i o n where e l ement can be i n s e r t e d’ ’ ’i f l e n ( l ) == 0 : r e t u r n 0poz = −1f o r i i n range (0 , l e n ( l ) ) :

i f e l<=l [ i ] :poz = i

i f poz == −1: r e t u r n l e n ( l )r e t u r n poz

Computational complexity is T (n) =n−1∑i=0

1 = n ∈ Θ(n)

Page 11: Searching. Sorting. Lambda expressions.arthur/FP2019/Lecture Notes/12.Searching.Sorti… · Binary search Search in Python Sorting The sorting problem Selection sort Insertion sort

Lecture 12

Lect. PhD.Arthur Molnar

Searching

The searchingproblem

Searchingalgorithms

Binary search

Search in Python

Sorting

The sortingproblem

Selection sort

Insertion sort

Bubble Sort

Quick Sort

LambdaExpressions

Searching - ordered keys

de f s e a r c hSu c c e s o r ( e l , l ) :’ ’ ’Search f o r an e l ement i n l i s te l − e l ementl − l i s t o f o r d e r ed e l ement sReturn the p o s i t i o n o f the f i r s t occu r r ence , o r

p o s i t i o n where e l ement can be i n s e r t e d’ ’ ’i f l e n ( l )==0 or e l<=l [ 0 ] :

r e t u r n 0i f e l>=l [ −1 ] :

r e t u r n l e n ( l )i = 0wh i l e i<l e n ( l ) and e l> l [ i ] :

i += 1r e t u r n i

Page 12: Searching. Sorting. Lambda expressions.arthur/FP2019/Lecture Notes/12.Searching.Sorti… · Binary search Search in Python Sorting The sorting problem Selection sort Insertion sort

Lecture 12

Lect. PhD.Arthur Molnar

Searching

The searchingproblem

Searchingalgorithms

Binary search

Search in Python

Sorting

The sortingproblem

Selection sort

Insertion sort

Bubble Sort

Quick Sort

LambdaExpressions

Searching - ordered keys

Best case: the element is at the first position,T (n) ∈ Θ(1).

Worst case: the element is in the n-1 position,T (n) ∈ Θ(n).

Average case: if distributing the element uniformly, theloop can be executed 0, 1, .., n − 1 times, soT (n) = 1+2+..+n−1

n ∈ Θ(n).

Overabll complexity is O(n)

Page 13: Searching. Sorting. Lambda expressions.arthur/FP2019/Lecture Notes/12.Searching.Sorti… · Binary search Search in Python Sorting The sorting problem Selection sort Insertion sort

Lecture 12

Lect. PhD.Arthur Molnar

Searching

The searchingproblem

Searchingalgorithms

Binary search

Search in Python

Sorting

The sortingproblem

Selection sort

Insertion sort

Bubble Sort

Quick Sort

LambdaExpressions

Searching algorithms

Sequential search

Keys are successively examinedKeys may not be ordered

Binary search

Uses the divide and conquer techniqueKeys are ordered

Page 14: Searching. Sorting. Lambda expressions.arthur/FP2019/Lecture Notes/12.Searching.Sorti… · Binary search Search in Python Sorting The sorting problem Selection sort Insertion sort

Lecture 12

Lect. PhD.Arthur Molnar

Searching

The searchingproblem

Searchingalgorithms

Binary search

Search in Python

Sorting

The sortingproblem

Selection sort

Insertion sort

Bubble Sort

Quick Sort

LambdaExpressions

Recursive binary-search algorithm

de f b i n a r yS e a r c h ( key , data , l e f t , r i g h t ) :’ ’ ’Search f o r an e l ement i n an o rd e r ed l i s tkey − e l ement to s e a r c hl e f t , r i g h t − bounds o f the s e a r c hReturn i n s e r t i o n p o s i t i o n o f key tha t keeps l i s t

o r d e r ed’ ’ ’i f l e f t >= r i g h t − 1 :

r e t u r n r i g h tmidd l e = ( l e f t + r i g h t ) // 2i f key < data [ midd l e ] :

r e t u r n b i n a r yS e a r c h ( key , data , l e f t , m idd l e )e l s e :

r e t u r n b i n a r yS e a r c h ( key , data , middle , r i g h t)

p r i n t ( b i n a r yS e a r c h (2000 , data , 0 , l e n ( data ) ) )

Page 15: Searching. Sorting. Lambda expressions.arthur/FP2019/Lecture Notes/12.Searching.Sorti… · Binary search Search in Python Sorting The sorting problem Selection sort Insertion sort

Lecture 12

Lect. PhD.Arthur Molnar

Searching

The searchingproblem

Searchingalgorithms

Binary search

Search in Python

Sorting

The sortingproblem

Selection sort

Insertion sort

Bubble Sort

Quick Sort

LambdaExpressions

Recursive binary-search function

de f s e a r c h ( key , data ) :’ ’ ’Search f o r an e l ement i n an o rd e r ed l i s tkey − e l ement to s e a r c hdata − the l i s tReturn i n s e r t i o n p o s i t i o n o f key tha t keeps l i s t

o r d e r ed’ ’ ’i f l e n ( data ) == 0 or key < data [ 0 ] :

r e t u r n 0i f key > data [ −1 ] :

r e t u r n l e n ( data )r e t u r n b i n a r yS e a r c h ( key , data , 0 , l e n ( data ) )

Page 16: Searching. Sorting. Lambda expressions.arthur/FP2019/Lecture Notes/12.Searching.Sorti… · Binary search Search in Python Sorting The sorting problem Selection sort Insertion sort

Lecture 12

Lect. PhD.Arthur Molnar

Searching

The searchingproblem

Searchingalgorithms

Binary search

Search in Python

Sorting

The sortingproblem

Selection sort

Insertion sort

Bubble Sort

Quick Sort

LambdaExpressions

Binary-search recurrence

The recurrence: T(n) =

{1, n = 1

T (n2 ) + 1, n > 1

Page 17: Searching. Sorting. Lambda expressions.arthur/FP2019/Lecture Notes/12.Searching.Sorti… · Binary search Search in Python Sorting The sorting problem Selection sort Insertion sort

Lecture 12

Lect. PhD.Arthur Molnar

Searching

The searchingproblem

Searchingalgorithms

Binary search

Search in Python

Sorting

The sortingproblem

Selection sort

Insertion sort

Bubble Sort

Quick Sort

LambdaExpressions

Iterative binary-search function

de f b i n a r yS e a r c h ( key , data ) :’ ’ ’− s p e c i f i c a t i o n −’ ’ ’i f l e n ( data ) == 0 or key < data [ 0 ] :

r e t u r n 0i f key > data [ −1 ] :

r e t u r n l e n ( data )l e f t = 0r i g h t = l e n ( data )wh i l e r i g h t − l e f t > 1 :

midd l e = ( l e f t + r i g h t ) // 2i f key <= data [ midd l e ] :

r i g h t = midd lee l s e :

l e f t = midd ler e t u r n r i g h t

Page 18: Searching. Sorting. Lambda expressions.arthur/FP2019/Lecture Notes/12.Searching.Sorti… · Binary search Search in Python Sorting The sorting problem Selection sort Insertion sort

Lecture 12

Lect. PhD.Arthur Molnar

Searching

The searchingproblem

Searchingalgorithms

Binary search

Search in Python

Sorting

The sortingproblem

Selection sort

Insertion sort

Bubble Sort

Quick Sort

LambdaExpressions

Search problem runtime complexity

Algorithm Best case Average Worst case Overall

Sequential Θ(n) Θ(n) Θ(n) Θ(n)

Succesor Θ(1) Θ(n) Θ(n) O(n)

Binary-search Θ(1) Θ(log2 n) Θ(log2 n) O(log2 n)

Page 19: Searching. Sorting. Lambda expressions.arthur/FP2019/Lecture Notes/12.Searching.Sorti… · Binary search Search in Python Sorting The sorting problem Selection sort Insertion sort

Lecture 12

Lect. PhD.Arthur Molnar

Searching

The searchingproblem

Searchingalgorithms

Binary search

Search in Python

Sorting

The sortingproblem

Selection sort

Insertion sort

Bubble Sort

Quick Sort

LambdaExpressions

Searching in Python

Collections and search

Examine the source code in ex34 search.py

Iterators

Examine the source code in ex35 iterators.py

Page 20: Searching. Sorting. Lambda expressions.arthur/FP2019/Lecture Notes/12.Searching.Sorti… · Binary search Search in Python Sorting The sorting problem Selection sort Insertion sort

Lecture 12

Lect. PhD.Arthur Molnar

Searching

The searchingproblem

Searchingalgorithms

Binary search

Search in Python

Sorting

The sortingproblem

Selection sort

Insertion sort

Bubble Sort

Quick Sort

LambdaExpressions

The sorting problem

Sorting

Rearrange a data collection in such a way that the elements ofthe collection verify a given order.

Internal sort - data to be sorted are available in theinternal memory

External sort - data is available as a file (on externalmedia)

In-place sort - transforms the input data into the output,only using a small additional space. Its opposite is calledout-of-place.

Sorting stability - we say that sorting is stable when theoriginal order of multiple records having the same key ispreserved

Page 21: Searching. Sorting. Lambda expressions.arthur/FP2019/Lecture Notes/12.Searching.Sorti… · Binary search Search in Python Sorting The sorting problem Selection sort Insertion sort

Lecture 12

Lect. PhD.Arthur Molnar

Searching

The searchingproblem

Searchingalgorithms

Binary search

Search in Python

Sorting

The sortingproblem

Selection sort

Insertion sort

Bubble Sort

Quick Sort

LambdaExpressions

Demo

Stable sort example

Examine the source code in ex35 stableSort.py

Page 22: Searching. Sorting. Lambda expressions.arthur/FP2019/Lecture Notes/12.Searching.Sorti… · Binary search Search in Python Sorting The sorting problem Selection sort Insertion sort

Lecture 12

Lect. PhD.Arthur Molnar

Searching

The searchingproblem

Searchingalgorithms

Binary search

Search in Python

Sorting

The sortingproblem

Selection sort

Insertion sort

Bubble Sort

Quick Sort

LambdaExpressions

The sorting problem

Elements of the data collection are called records

A record is formed by one or more components, calledfields

A key K is associated to each record, and is usually one ofthe fields.

We say that a collection of n records is:

Sorted in increasing order by the key K : if K (i) ≤ K (j) for0 ≤ i < j < nSorted in decreasing order: if K (i) ≥ K (j) for0 ≤ i < j < n

Page 23: Searching. Sorting. Lambda expressions.arthur/FP2019/Lecture Notes/12.Searching.Sorti… · Binary search Search in Python Sorting The sorting problem Selection sort Insertion sort

Lecture 12

Lect. PhD.Arthur Molnar

Searching

The searchingproblem

Searchingalgorithms

Binary search

Search in Python

Sorting

The sortingproblem

Selection sort

Insertion sort

Bubble Sort

Quick Sort

LambdaExpressions

Internal sorting

Problem specification

Data: n,K , where K = (k1, k2, ..., kn), ki ∈ R, i = 1, n

Results: K ′, where K ′ is a permutation of K , havingsorted elements: k ′1 ≤ k ′2 ≤ ... ≤ k ′n.

Page 24: Searching. Sorting. Lambda expressions.arthur/FP2019/Lecture Notes/12.Searching.Sorti… · Binary search Search in Python Sorting The sorting problem Selection sort Insertion sort

Lecture 12

Lect. PhD.Arthur Molnar

Searching

The searchingproblem

Searchingalgorithms

Binary search

Search in Python

Sorting

The sortingproblem

Selection sort

Insertion sort

Bubble Sort

Quick Sort

LambdaExpressions

Sorting algorithms

A few algorithms that we will study:

Selection sort

Insertion sort

Bubble sort

Quick sort

Page 25: Searching. Sorting. Lambda expressions.arthur/FP2019/Lecture Notes/12.Searching.Sorti… · Binary search Search in Python Sorting The sorting problem Selection sort Insertion sort

Lecture 12

Lect. PhD.Arthur Molnar

Searching

The searchingproblem

Searchingalgorithms

Binary search

Search in Python

Sorting

The sortingproblem

Selection sort

Insertion sort

Bubble Sort

Quick Sort

LambdaExpressions

Selection Sort

Determine the element having the minimal key, and swapit with the first element.

Resume the procedure for the remaining elements, until allelements have been considered.

Page 26: Searching. Sorting. Lambda expressions.arthur/FP2019/Lecture Notes/12.Searching.Sorti… · Binary search Search in Python Sorting The sorting problem Selection sort Insertion sort

Lecture 12

Lect. PhD.Arthur Molnar

Searching

The searchingproblem

Searchingalgorithms

Binary search

Search in Python

Sorting

The sortingproblem

Selection sort

Insertion sort

Bubble Sort

Quick Sort

LambdaExpressions

Selection sort algorithm

de f s e l e c t i o n S o r t ( data ) :f o r i i n range ( l e n ( data ) ) :

m in i ndex = i# Find sma l l e s t e l ement i n the r e s t o f the

l i s tf o r j i n range ( i +1, l e n ( data ) ) :

i f data [ j ] < data [ m in i ndex ] :m in i ndex = j

data [ i ] , data [ m in i ndex ] = data [ m in i ndex ] ,data [ i ]

Page 27: Searching. Sorting. Lambda expressions.arthur/FP2019/Lecture Notes/12.Searching.Sorti… · Binary search Search in Python Sorting The sorting problem Selection sort Insertion sort

Lecture 12

Lect. PhD.Arthur Molnar

Searching

The searchingproblem

Searchingalgorithms

Binary search

Search in Python

Sorting

The sortingproblem

Selection sort

Insertion sort

Bubble Sort

Quick Sort

LambdaExpressions

Selection sort - time complexity

The total number of comparisons isn−1∑i=1

n∑j=i+1

1 =n(n − 1)

2∈ Θ(n2)

Independent of the input data size, what are the best,average, worst-case computational complexities?

Page 28: Searching. Sorting. Lambda expressions.arthur/FP2019/Lecture Notes/12.Searching.Sorti… · Binary search Search in Python Sorting The sorting problem Selection sort Insertion sort

Lecture 12

Lect. PhD.Arthur Molnar

Searching

The searchingproblem

Searchingalgorithms

Binary search

Search in Python

Sorting

The sortingproblem

Selection sort

Insertion sort

Bubble Sort

Quick Sort

LambdaExpressions

Selection sort - space complexity

In-place algorithms. Algorithms that use a small(constant) quantity of additional memory.

Out-of-place or not-in-space algorithms. Algorithms thatuse a non-constant quantity of extra-space.

The additional memory required by selection sort is O(1).

Selection sort is an in-place sorting algorithm.

Page 29: Searching. Sorting. Lambda expressions.arthur/FP2019/Lecture Notes/12.Searching.Sorti… · Binary search Search in Python Sorting The sorting problem Selection sort Insertion sort

Lecture 12

Lect. PhD.Arthur Molnar

Searching

The searchingproblem

Searchingalgorithms

Binary search

Search in Python

Sorting

The sortingproblem

Selection sort

Insertion sort

Bubble Sort

Quick Sort

LambdaExpressions

Direct selection sort

de f d i r e c t S e l e c t i o n S o r t ( data ) :f o r i i n range (0 , l e n ( data ) − 1) :

# S e l e c t the sm a l l e s t e l ementf o r j i n range ( i + 1 , l e n ( data ) ) :

i f data [ j ] < data [ i ] :data [ i ] , data [ j ] = data [ j ] , data [ i ]

Page 30: Searching. Sorting. Lambda expressions.arthur/FP2019/Lecture Notes/12.Searching.Sorti… · Binary search Search in Python Sorting The sorting problem Selection sort Insertion sort

Lecture 12

Lect. PhD.Arthur Molnar

Searching

The searchingproblem

Searchingalgorithms

Binary search

Search in Python

Sorting

The sortingproblem

Selection sort

Insertion sort

Bubble Sort

Quick Sort

LambdaExpressions

Direct selection sort

Overall time complexity:n−1∑i=1

n∑j=i+1

1 =n(n − 1)

2∈ Θ(n2)

Page 31: Searching. Sorting. Lambda expressions.arthur/FP2019/Lecture Notes/12.Searching.Sorti… · Binary search Search in Python Sorting The sorting problem Selection sort Insertion sort

Lecture 12

Lect. PhD.Arthur Molnar

Searching

The searchingproblem

Searchingalgorithms

Binary search

Search in Python

Sorting

The sortingproblem

Selection sort

Insertion sort

Bubble Sort

Quick Sort

LambdaExpressions

Insertion Sort

Traverse the elements.

Insert the current element at the right position in thesubsequence of already sorted elements.

The sub-sequence containing the already processedelements is kept sorted, so that, at the end of thetraversal, the whole sequence is sorted.

Page 32: Searching. Sorting. Lambda expressions.arthur/FP2019/Lecture Notes/12.Searching.Sorti… · Binary search Search in Python Sorting The sorting problem Selection sort Insertion sort

Lecture 12

Lect. PhD.Arthur Molnar

Searching

The searchingproblem

Searchingalgorithms

Binary search

Search in Python

Sorting

The sortingproblem

Selection sort

Insertion sort

Bubble Sort

Quick Sort

LambdaExpressions

Insertion Sort - Algorithm

de f i n s e r t S o r t ( data ) :f o r i i n range (1 , l e n ( data ) ) :

i nd e x = i − 1elem = data [ i ]# I n s e r t i n t o c o r r e c t p o s i t i o nwh i l e i ndex >= 0 and elem < data [ i ndex ] :

data [ i nd ex + 1 ] = data [ i nd ex ]i nd ex −= 1

data [ i ndex + 1 ] = elem

Page 33: Searching. Sorting. Lambda expressions.arthur/FP2019/Lecture Notes/12.Searching.Sorti… · Binary search Search in Python Sorting The sorting problem Selection sort Insertion sort

Lecture 12

Lect. PhD.Arthur Molnar

Searching

The searchingproblem

Searchingalgorithms

Binary search

Search in Python

Sorting

The sortingproblem

Selection sort

Insertion sort

Bubble Sort

Quick Sort

LambdaExpressions

Insertion Sort - time complexity

Maximum number of iterations (worst case) happens if theinitial array is sorted in a descending order:

T (n) =n∑

i=2

(i − 1) =n(n − 1)

2∈ Θ(n2)

Page 34: Searching. Sorting. Lambda expressions.arthur/FP2019/Lecture Notes/12.Searching.Sorti… · Binary search Search in Python Sorting The sorting problem Selection sort Insertion sort

Lecture 12

Lect. PhD.Arthur Molnar

Searching

The searchingproblem

Searchingalgorithms

Binary search

Search in Python

Sorting

The sortingproblem

Selection sort

Insertion sort

Bubble Sort

Quick Sort

LambdaExpressions

Insertion Sort - time complexity

Minimum number of iterations (best case) happens if theinitial array is already sorted:

T (n) =n∑

i=2

1 = n − 1 ∈ Θ(n)

Page 35: Searching. Sorting. Lambda expressions.arthur/FP2019/Lecture Notes/12.Searching.Sorti… · Binary search Search in Python Sorting The sorting problem Selection sort Insertion sort

Lecture 12

Lect. PhD.Arthur Molnar

Searching

The searchingproblem

Searchingalgorithms

Binary search

Search in Python

Sorting

The sortingproblem

Selection sort

Insertion sort

Bubble Sort

Quick Sort

LambdaExpressions

Insertion Sort - Space complexity

Time complexity - The overall time complexity of insertionsort is O(n2).

Space complexity - The complexity of insertion sort is θ(1)

Insertion sort is an in-place sorting algorithm.

Page 36: Searching. Sorting. Lambda expressions.arthur/FP2019/Lecture Notes/12.Searching.Sorti… · Binary search Search in Python Sorting The sorting problem Selection sort Insertion sort

Lecture 12

Lect. PhD.Arthur Molnar

Searching

The searchingproblem

Searchingalgorithms

Binary search

Search in Python

Sorting

The sortingproblem

Selection sort

Insertion sort

Bubble Sort

Quick Sort

LambdaExpressions

Bubble Sort

Compares pairs of consecutive elements that are swappedif not in the expected order.

The comparison process ends when all pairs of consecutiveelements are in the expected order.

Page 37: Searching. Sorting. Lambda expressions.arthur/FP2019/Lecture Notes/12.Searching.Sorti… · Binary search Search in Python Sorting The sorting problem Selection sort Insertion sort

Lecture 12

Lect. PhD.Arthur Molnar

Searching

The searchingproblem

Searchingalgorithms

Binary search

Search in Python

Sorting

The sortingproblem

Selection sort

Insertion sort

Bubble Sort

Quick Sort

LambdaExpressions

Bubble Sort - Algorithm

de f bubb l eSo r t ( data ) :done = Fa l s ewh i l e not done :

done = Truef o r i i n range (0 , l e n ( data ) − 1) :

i f data [ i ] > data [ i +1] :data [ i ] , data [ i +1] = data [ i +1] , data

[ i ]done = Fa l s e

Page 38: Searching. Sorting. Lambda expressions.arthur/FP2019/Lecture Notes/12.Searching.Sorti… · Binary search Search in Python Sorting The sorting problem Selection sort Insertion sort

Lecture 12

Lect. PhD.Arthur Molnar

Searching

The searchingproblem

Searchingalgorithms

Binary search

Search in Python

Sorting

The sortingproblem

Selection sort

Insertion sort

Bubble Sort

Quick Sort

LambdaExpressions

Bubble Sort - Complexity

Best-case running time complexity order is θ(n)

Worst-case running time complexity order is θ(n2)

Average running-time complexity order is θ(n2)

Space complexity, additional memory required is θ(1)

Bubble sort is an in-place sorting algorithm.

Page 39: Searching. Sorting. Lambda expressions.arthur/FP2019/Lecture Notes/12.Searching.Sorti… · Binary search Search in Python Sorting The sorting problem Selection sort Insertion sort

Lecture 12

Lect. PhD.Arthur Molnar

Searching

The searchingproblem

Searchingalgorithms

Binary search

Search in Python

Sorting

The sortingproblem

Selection sort

Insertion sort

Bubble Sort

Quick Sort

LambdaExpressions

Quick Sort

Based on the divide and conquer technique

1 Divide: partition array into 2 sub-arrays such thatelements in the lower part ≤ elements in the higher part.

Partitioning

Re-arrange the elements so that the element called pivotoccupies the final position in the sub-sequence. If i is thatposition: kj ≤ ki ≤ kl , for Left ≤ j < i < l ≤ Right

2 Conquer: recursively sort the 2 sub-arrays.

3 Combine: trivial since sorting is done in place.

Page 40: Searching. Sorting. Lambda expressions.arthur/FP2019/Lecture Notes/12.Searching.Sorti… · Binary search Search in Python Sorting The sorting problem Selection sort Insertion sort

Lecture 12

Lect. PhD.Arthur Molnar

Searching

The searchingproblem

Searchingalgorithms

Binary search

Search in Python

Sorting

The sortingproblem

Selection sort

Insertion sort

Bubble Sort

Quick Sort

LambdaExpressions

Quick Sort - partitioning algorithm

de f p a r t i t i o n ( data , l e f t , r i g h t ) :p i v o t = data [ l e f t ]i = l e f tj = r i g h twh i l e i != j :

# Find an e lement sma l l e r than the p i v o twh i l e data [ j ] >= p i v o t and i < j :

j −= 1data [ i ] = data [ j ]# Find an e lement l a r g e r than the p i v o twh i l e data [ i ] <= p i v o t and i < j :

i += 1data [ j ] = data [ i ]

# Place the p i v o t i n p o s i t i o ndata [ i ] = p i v o tr e t u r n i

Page 41: Searching. Sorting. Lambda expressions.arthur/FP2019/Lecture Notes/12.Searching.Sorti… · Binary search Search in Python Sorting The sorting problem Selection sort Insertion sort

Lecture 12

Lect. PhD.Arthur Molnar

Searching

The searchingproblem

Searchingalgorithms

Binary search

Search in Python

Sorting

The sortingproblem

Selection sort

Insertion sort

Bubble Sort

Quick Sort

LambdaExpressions

Quick Sort - algorithm

de f qu i c kSo r t ( data , l e f t , r i g h t ) :# P a r t i t i o n the l i s tpos = p a r t i t i o n ( data , l e f t , r i g h t )# Order l e f t s i d ei f l e f t < pos − 1 :

qu i c kSo r t ( data , l e f t , pos − 1)# Order r i g h t s i d ei f pos + 1 < r i g h t :

q u i c kSo r t ( data , pos + 1 , r i g h t )

Page 42: Searching. Sorting. Lambda expressions.arthur/FP2019/Lecture Notes/12.Searching.Sorti… · Binary search Search in Python Sorting The sorting problem Selection sort Insertion sort

Lecture 12

Lect. PhD.Arthur Molnar

Searching

The searchingproblem

Searchingalgorithms

Binary search

Search in Python

Sorting

The sortingproblem

Selection sort

Insertion sort

Bubble Sort

Quick Sort

LambdaExpressions

Quick Sort - time complexity

The run time of quick-sort depends on the distribution ofsplits

The partitioning function requires linear time

Best case, the partitioning function splits the arrayevenly: T (n) = 2T (n2 ) + Θ(n),T (n) ∈ Θ(n log2 n)

Page 43: Searching. Sorting. Lambda expressions.arthur/FP2019/Lecture Notes/12.Searching.Sorti… · Binary search Search in Python Sorting The sorting problem Selection sort Insertion sort

Lecture 12

Lect. PhD.Arthur Molnar

Searching

The searchingproblem

Searchingalgorithms

Binary search

Search in Python

Sorting

The sortingproblem

Selection sort

Insertion sort

Bubble Sort

Quick Sort

LambdaExpressions

Quick Sort - best partitioning

We partition n elements log2 n times, soT (n) ∈ Θ(n log2 n)

Page 44: Searching. Sorting. Lambda expressions.arthur/FP2019/Lecture Notes/12.Searching.Sorti… · Binary search Search in Python Sorting The sorting problem Selection sort Insertion sort

Lecture 12

Lect. PhD.Arthur Molnar

Searching

The searchingproblem

Searchingalgorithms

Binary search

Search in Python

Sorting

The sortingproblem

Selection sort

Insertion sort

Bubble Sort

Quick Sort

LambdaExpressions

Quick Sort - worst partitioning

In the worst case, function Partition splits the array suchthat one side of the partition has only one element:T (n) = T (1) + T (n − 1) + Θ(n) = T (n − 1) + Θ(n) =n∑

k=1

Θ(k) ∈ Θ(n2)

Page 45: Searching. Sorting. Lambda expressions.arthur/FP2019/Lecture Notes/12.Searching.Sorti… · Binary search Search in Python Sorting The sorting problem Selection sort Insertion sort

Lecture 12

Lect. PhD.Arthur Molnar

Searching

The searchingproblem

Searchingalgorithms

Binary search

Search in Python

Sorting

The sortingproblem

Selection sort

Insertion sort

Bubble Sort

Quick Sort

LambdaExpressions

Quick Sort - Worst case

Worst case partitioning appears when the input array issorted or reverse sorted, so n elements are partitioned ntimes, T (n) ∈ Θ(n2)

Page 46: Searching. Sorting. Lambda expressions.arthur/FP2019/Lecture Notes/12.Searching.Sorti… · Binary search Search in Python Sorting The sorting problem Selection sort Insertion sort

Lecture 12

Lect. PhD.Arthur Molnar

Searching

The searchingproblem

Searchingalgorithms

Binary search

Search in Python

Sorting

The sortingproblem

Selection sort

Insertion sort

Bubble Sort

Quick Sort

LambdaExpressions

Sorting runtime complexity

Algorithm Worst case Average

Selection sort Θ(n2) Θ(n2)

Insertion sort Θ(n2) Θ(n2)

Bubble sort Θ(n2) Θ(n2)

Quick sort Θ(n2) Θ(n log2 n)

Page 47: Searching. Sorting. Lambda expressions.arthur/FP2019/Lecture Notes/12.Searching.Sorti… · Binary search Search in Python Sorting The sorting problem Selection sort Insertion sort

Lecture 12

Lect. PhD.Arthur Molnar

Searching

The searchingproblem

Searchingalgorithms

Binary search

Search in Python

Sorting

The sortingproblem

Selection sort

Insertion sort

Bubble Sort

Quick Sort

LambdaExpressions

Demo

Sorting

Examine the source code in ex37 sort.py

Page 48: Searching. Sorting. Lambda expressions.arthur/FP2019/Lecture Notes/12.Searching.Sorti… · Binary search Search in Python Sorting The sorting problem Selection sort Insertion sort

Lecture 12

Lect. PhD.Arthur Molnar

Searching

The searchingproblem

Searchingalgorithms

Binary search

Search in Python

Sorting

The sortingproblem

Selection sort

Insertion sort

Bubble Sort

Quick Sort

LambdaExpressions

Lambda expressions

Lambda expressions

Small anonymous functions, that you define and use in thesame place.

Syntactically restricted to a single expression.

Can reference variables from the containing scope (justlike nested functions).

They are syntactic sugar for a function definition.

Page 49: Searching. Sorting. Lambda expressions.arthur/FP2019/Lecture Notes/12.Searching.Sorti… · Binary search Search in Python Sorting The sorting problem Selection sort Insertion sort

Lecture 12

Lect. PhD.Arthur Molnar

Searching

The searchingproblem

Searchingalgorithms

Binary search

Search in Python

Sorting

The sortingproblem

Selection sort

Insertion sort

Bubble Sort

Quick Sort

LambdaExpressions

Demo

Lambda Expressions

Examine the source code in ex38 lambdas.py