Top Banner
Searching and Sorting by Kittipitch Kuptavanich adapted to English by Prakarn Unachak
22

Searching and Sorting - Chiang Mai University 201110 - Integrated Mathematics - Searching & Sorting 3. ... Problem Solving with Algorithms and Data Structures Using Python - Miller

Jul 27, 2018

Download

Documents

VuHuong
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 and Sorting - Chiang Mai University 201110 - Integrated Mathematics - Searching & Sorting 3. ... Problem Solving with Algorithms and Data Structures Using Python - Miller

Searching and Sorting

by Kittipitch Kuptavanich

adapted to English by Prakarn Unachak

Page 2: Searching and Sorting - Chiang Mai University 201110 - Integrated Mathematics - Searching & Sorting 3. ... Problem Solving with Algorithms and Data Structures Using Python - Miller

Searching

201110 - Integrated Mathematics - Searching & Sorting 2

Page 3: Searching and Sorting - Chiang Mai University 201110 - Integrated Mathematics - Searching & Sorting 3. ... Problem Solving with Algorithms and Data Structures Using Python - Miller

Searching

One of the most encountered problem in computing

In basic searching, we will try to find out whether an item is in a collection of items or not.

True/false – Does 18 exist in the list? (No)

With modification, it can also return the location of the item. – Where is 77? (5th item)

201110 - Integrated Mathematics - Searching & Sorting 3

Page 4: Searching and Sorting - Chiang Mai University 201110 - Integrated Mathematics - Searching & Sorting 3. ... Problem Solving with Algorithms and Data Structures Using Python - Miller

Linear Search

If the collection is unsorted, i.e. not ordered, we can use linear search (also known as sequential search) to look for an item (called key).

1. Start from the beginning, then:

2. Look at the current item, if matches the key, return yes.

3. If the current item does not match the key, check if this is the last item in the list, if so, return no. (since we have reach the last item and hasn’t found the item yet.)

4. If we are not at the last item, move to the next item and go back to 2.

Start

201110 - Integrated Mathematics - Searching & Sorting 4

Page 5: Searching and Sorting - Chiang Mai University 201110 - Integrated Mathematics - Searching & Sorting 3. ... Problem Solving with Algorithms and Data Structures Using Python - Miller

Linear Search (2)

However, if the list is sorted, we don’t have to search until we have reached the end.

For example, if the list is in ascending order (from smallest number to largest number), we can stop once the current item is larger than the key.

But if we know that the list is sorted, there is a better way to do searching.

key = 53

17 20 26 31 44 54 55 65 77 93

Source: Problem Solving with Algorithms and Data Structures Using Python - Miller and Ranum

201110 - Integrated Mathematics - Searching & Sorting 5

Page 6: Searching and Sorting - Chiang Mai University 201110 - Integrated Mathematics - Searching & Sorting 3. ... Problem Solving with Algorithms and Data Structures Using Python - Miller

Linear Search (3) - Exercise

We want to find key = 18 using linear search in an unsorted list:

Which numbers to do we need to compare with the key before we can find 18?

Try again with key = 12

201110 - Integrated Mathematics - Searching & Sorting 6

15 17 2 19 18 0 8 13 19 14

Page 7: Searching and Sorting - Chiang Mai University 201110 - Integrated Mathematics - Searching & Sorting 3. ... Problem Solving with Algorithms and Data Structures Using Python - Miller

Linear Search (4) - Exercise

We want to find key = 13 using linear search in an sorted list:

Which numbers to do we need to compare with the key before we can stop?

201110 - Integrated Mathematics - Searching & Sorting 7

3 5 6 8 11 12 14 15 17 18

Page 8: Searching and Sorting - Chiang Mai University 201110 - Integrated Mathematics - Searching & Sorting 3. ... Problem Solving with Algorithms and Data Structures Using Python - Miller

Binary Search

Considering looking for a word (key) in the dictionary.

The words are ordered. The dictionary contains a sorted list of words.

If we open a page (g) randomly:

• If the word came before the key, we can just look for the word in pages after that page.

• If the word came after the key, we can just look for the word in pages before that page.

• So, we can basically divide the size of the list in half in each step.

201110 - Integrated Mathematics - Searching & Sorting 8

Source: Problem Solving with Algorithms and Data Structures Using Python - Miller and Ranum

Page 9: Searching and Sorting - Chiang Mai University 201110 - Integrated Mathematics - Searching & Sorting 3. ... Problem Solving with Algorithms and Data Structures Using Python - Miller

Binary Search (2)

or Bisection Search, which can be use to find an item in ordered list.

key > g: lo = mid + 1 mid = (lo + hi)/2 (round down)

key < g: hi = mid - 1 mid = (lo + hi)/2 (round down)

lo hi

0 g max

lo hi

0 g max

lo hi

0 g max

201110 - Integrated Mathematics - Searching & Sorting 9

mid

mid

mid

Source: Problem Solving with Algorithms and Data Structures Using Python - Miller and Ranum

Page 10: Searching and Sorting - Chiang Mai University 201110 - Integrated Mathematics - Searching & Sorting 3. ... Problem Solving with Algorithms and Data Structures Using Python - Miller

Binary Search (3)

For example, we have a sorted list of numbers

And want to search for key = 23

201110 - Integrated Mathematics - Searching & Sorting 10

Source: Problem Solving with Algorithms and Data Structures Using Python - Miller and Ranum

Binary search will stop when:(1) An item matching

the key is found, or(2) lo = hi, and the

item does not match the key.

Page 11: Searching and Sorting - Chiang Mai University 201110 - Integrated Mathematics - Searching & Sorting 3. ... Problem Solving with Algorithms and Data Structures Using Python - Miller

Binary Search (3)

And we want to find key = 50

201110 - Integrated Mathematics - Searching & Sorting 11

Source: Problem Solving with Algorithms and Data Structures Using Python - Miller and Ranum

Binary search will stop when:(1) An item matching

the key is found, or(2) lo = hi, and the

item does not match the key.

Page 12: Searching and Sorting - Chiang Mai University 201110 - Integrated Mathematics - Searching & Sorting 3. ... Problem Solving with Algorithms and Data Structures Using Python - Miller

Binary Search (4) - Exercise

If we want to find key = 8 using binary search in an ordered list

Which numbers will be compared with the key?

201110 - Integrated Mathematics - Searching & Sorting 12

[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] [14]

3 4 7 8 11 14 16 17 20 23 26 29 30 31 34

Source: Problem Solving with Algorithms and Data Structures Using Python - Miller and Ranum

Page 13: Searching and Sorting - Chiang Mai University 201110 - Integrated Mathematics - Searching & Sorting 3. ... Problem Solving with Algorithms and Data Structures Using Python - Miller

Binary Search (5) - Exercise

If we want to find key = 15 using binary search in an ordered list

Which numbers will be compared with the key?

201110 - Integrated Mathematics - Searching & Sorting 13

[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] [14]

3 4 7 8 11 14 16 17 20 23 26 29 30 31 34

Source: Problem Solving with Algorithms and Data Structures Using Python - Miller and Ranum

Page 14: Searching and Sorting - Chiang Mai University 201110 - Integrated Mathematics - Searching & Sorting 3. ... Problem Solving with Algorithms and Data Structures Using Python - Miller

Sorting

201110 - Integrated Mathematics - Searching & Sorting 14

Page 15: Searching and Sorting - Chiang Mai University 201110 - Integrated Mathematics - Searching & Sorting 3. ... Problem Solving with Algorithms and Data Structures Using Python - Miller

Sorting

As you can see, if we can have an ordered list of data, we can use binary search, which can greatly reduce search time.

So, we need to sort an unordered list, to get an ordered one.

We will talk about two examples of sortings:

Insertion sort

Merge sort.

201110 - Integrated Mathematics - Searching & Sorting 15

Page 16: Searching and Sorting - Chiang Mai University 201110 - Integrated Mathematics - Searching & Sorting 3. ... Problem Solving with Algorithms and Data Structures Using Python - Miller

Insertion SortStarting from the leftmost

item.

Move it to the front until we find an item smaller than it, then put it after that item, or the beginning, if no such item is found.

3 7 4 9 5 2 6 1

3 7 4 9 5 2 6 1

3 4 7 9 5 2 6 1

3 4 7 9 5 2 6 1

3 4 5 7 9 2 6 1

2 3 4 5 7 9 6 1

2 3 4 5 6 7 9 1

3 7 4 9 5 2 6 1

Sorted Section

16

First number, no move

3 > 7, no move

Swap 7 and 4, then stop

No move

Swap 5 with 9 and then 7

To insert 5:(1) 9 > 5, so we swap

9 and 5(2) 7 > 5, so we swap

7 and 5(3) 4 < 5 stop

Page 17: Searching and Sorting - Chiang Mai University 201110 - Integrated Mathematics - Searching & Sorting 3. ... Problem Solving with Algorithms and Data Structures Using Python - Miller

17

6 5 3 1 8 7 2 4

5 3 1 8 7 2 4

3 1 8 7 2 4

1 8 7 2 4

8 7 2 4

7 2 4

2 4

4

Insertion SortPractice 1

Page 18: Searching and Sorting - Chiang Mai University 201110 - Integrated Mathematics - Searching & Sorting 3. ... Problem Solving with Algorithms and Data Structures Using Python - Miller

Merge Algorithm

If we have two ordered lists, we have a way to combine them to get a bigger, but still ordered list.

The bigger list will contains all members of the two lists.

This process is called merging.

201110 - Integrated Mathematics - Searching & Sorting 19

1 3 4 5 6

2 5 7 9 10

1 2 3 4 5 5 6 7 9 10

Page 19: Searching and Sorting - Chiang Mai University 201110 - Integrated Mathematics - Searching & Sorting 3. ... Problem Solving with Algorithms and Data Structures Using Python - Miller

Merge Algorithm [2]

We have order list A and B

Let C be an empty list

We will start looking at the beginning of each list

let a be the current item at list A and b be the current item at list B

If a < b add a into list C and look at the next element in A

Else, add b into list C and look at the next element in B

Repeat until a list is empty, then we add the rest of the other list to C.

201110 - Integrated Mathematics - Searching & Sorting 20

1 3 4 5 6

2 5 7 9 10

A

B

Page 20: Searching and Sorting - Chiang Mai University 201110 - Integrated Mathematics - Searching & Sorting 3. ... Problem Solving with Algorithms and Data Structures Using Python - Miller

Merge Algorithm [3]

201110 - Integrated Mathematics - Searching & Sorting 21

a

b

c

d

1

1 2

1 2 3

1 2 3 4

1 3 4 5 6 2 5 7 9 10

1 3 4 5 6 2 5 7 9 10

1 3 4 5 6 2 5 7 9 10

1 3 4 5 6 2 5 7 9 10

e

f

g

h

1 3 4 5 6 2 5 7 9 10

1 2 3 4 5

1 3 4 5 6 2 5 7 9 10

1 2 3 4 5 5

1 3 4 5 6 2 5 7 9 10

1 2 3 4 5 5 6

1 3 4 5 6 2 5 7 9 10

1 2 3 4 5 5 6 7 9 10

Page 21: Searching and Sorting - Chiang Mai University 201110 - Integrated Mathematics - Searching & Sorting 3. ... Problem Solving with Algorithms and Data Structures Using Python - Miller

Merge Sort

22

3 7 4 9 5 2 6 1

3 7 4 9 5 2 6 1

3 7 4 9 5 2 6 1

3 7 4 9 5 2 6 1

• For Merge Sort, we divide the list up into smaller list, then keep dividing until we reach list of individual elements, use merging to combine them back up into sorted list.

201110 - Integrated Mathematics - Searching & Sorting

Page 22: Searching and Sorting - Chiang Mai University 201110 - Integrated Mathematics - Searching & Sorting 3. ... Problem Solving with Algorithms and Data Structures Using Python - Miller

Merge Sort

23

3 7 4 9 5 2 6 1

3 7 4 9 5 2 6 1

3 7 4 9 5 2 6 1

3 7 4 9 5 2 6 1

201110 - Integrated Mathematics - Searching & Sorting