Top Banner
Searching and Sorting
34

Searching and Sorting Topics Linear and Binary Searches Selection Sort Bubble Sort.

Jan 01, 2016

Download

Documents

Nancy Gibbs
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 Topics Linear and Binary Searches Selection Sort Bubble Sort.

Searching and Sorting

Page 2: Searching and Sorting Topics Linear and Binary Searches Selection Sort Bubble Sort.

Topics

Linear and Binary SearchesSelection SortBubble Sort

Page 3: Searching and Sorting Topics Linear and Binary Searches Selection Sort Bubble Sort.

Objectives

At the completion of this topic, students should be able to:

Explain the difference between a linear and a binary searchWrite a linear search routineWrite a binary search routineWrite a bubble sort routine

Page 4: Searching and Sorting Topics Linear and Binary Searches Selection Sort Bubble Sort.

Find the person who is 23 years old.

Page 5: Searching and Sorting Topics Linear and Binary Searches Selection Sort Bubble Sort.

Rules1. You may only talk to one person at a time2. You may only ask “How old are you?”3. The person must respond in years and months

Page 6: Searching and Sorting Topics Linear and Binary Searches Selection Sort Bubble Sort.

Write down your algorithm

Page 7: Searching and Sorting Topics Linear and Binary Searches Selection Sort Bubble Sort.

Searching an Array

Linear SearchBinary Search

Page 8: Searching and Sorting Topics Linear and Binary Searches Selection Sort Bubble Sort.

Linear SearchexamScores

0

1

2

3

4

5

6

7

8

9

72

98

56

87

64

83

77

91

66

70

Problem: Determine which element in the arraycontains the score 87.

int thisOne = -1;

for (int index = 0; index < SIZE; index++){ if (examScores[index] == 87) thisOne = index;}

Page 9: Searching and Sorting Topics Linear and Binary Searches Selection Sort Bubble Sort.

Binary Search

In general, a binary search is much, much faster than a linear search, but requires that the array be sorted.

examScores

72

98

87

64

83

77

91

66

70

Start in the middle

Is this the one you are looking for (87)?

If not, is this number smaller than 87?

In this case it is. Therefore, we can elminatethe entire bottom half of the array. Why?

Now try again, picking the middle of the remaining array elements.

Page 10: Searching and Sorting Topics Linear and Binary Searches Selection Sort Bubble Sort.

Arrange the people in order of age – youngest to oldest

Page 11: Searching and Sorting Topics Linear and Binary Searches Selection Sort Bubble Sort.

Rules1. You may only talk to one person at a time2. You may only ask “How old are you?”3. Responses will be in years and months.4. You are only allowed to keep track of two people’s ages at any one time.5. You may only ask two people to switch places at this time.6. A person cannot move unless asked to.

Page 12: Searching and Sorting Topics Linear and Binary Searches Selection Sort Bubble Sort.

Write down your algorithm

Page 13: Searching and Sorting Topics Linear and Binary Searches Selection Sort Bubble Sort.

Sorting

Sorting means to put data into some specified order.

There are many, many algorithms that have been developedto sort data. In this section we will mention two of them:

Selection Sort

Bubble Sort

Page 14: Searching and Sorting Topics Linear and Binary Searches Selection Sort Bubble Sort.

Selection Sort

Algorithm Development

Page 15: Searching and Sorting Topics Linear and Binary Searches Selection Sort Bubble Sort.

Selection Sort

7 3 9 6 5 2

Step one:find the lowest cardin the hand

Page 16: Searching and Sorting Topics Linear and Binary Searches Selection Sort Bubble Sort.

Selection sort

7 3 9 6 5 2

Step two:Swap the lowest card withthe left-most card

left-most card

Page 17: Searching and Sorting Topics Linear and Binary Searches Selection Sort Bubble Sort.

Selection Sort

7 3 9 6 52

Page 18: Searching and Sorting Topics Linear and Binary Searches Selection Sort Bubble Sort.

Selection Sort

3 9 6

7

52

Page 19: Searching and Sorting Topics Linear and Binary Searches Selection Sort Bubble Sort.

Selection Sort

3 9 67

52

Page 20: Searching and Sorting Topics Linear and Binary Searches Selection Sort Bubble Sort.

Selection Sort

3 9 67

52

Now … Make the second card The left-most card

left-most card

Page 21: Searching and Sorting Topics Linear and Binary Searches Selection Sort Bubble Sort.

Selection Sort

3 9 67

52

Find the lowest cardin the remaining cards

Page 22: Searching and Sorting Topics Linear and Binary Searches Selection Sort Bubble Sort.

3 9 67

52

Selection Sort

left-most card

It is already the left-most card, sono swap is required

Page 23: Searching and Sorting Topics Linear and Binary Searches Selection Sort Bubble Sort.

Selection Sort

3 9 67

52Left-most card

Now make the thirdcard the left-most card

Page 24: Searching and Sorting Topics Linear and Binary Searches Selection Sort Bubble Sort.

Selection Sort

3 9 67

52

And find the lowest cardIn the remaining cards

Page 25: Searching and Sorting Topics Linear and Binary Searches Selection Sort Bubble Sort.

Selection Sort

3 9 6

5

72

Swap it with the left-most card

Page 26: Searching and Sorting Topics Linear and Binary Searches Selection Sort Bubble Sort.

Selection Sort

3 5 69

72

Page 27: Searching and Sorting Topics Linear and Binary Searches Selection Sort Bubble Sort.

Selection Sort

3 5 69

72

Make the 4th cardthe left-most card

Page 28: Searching and Sorting Topics Linear and Binary Searches Selection Sort Bubble Sort.

Selection Sort

3 5 69

72

Find the lowest cardin the remaining cards

Page 29: Searching and Sorting Topics Linear and Binary Searches Selection Sort Bubble Sort.

Selection Sort

3 5 69

72

The lowest card is theleft-most card, so noswap is necessary

Page 30: Searching and Sorting Topics Linear and Binary Searches Selection Sort Bubble Sort.

Selection Sort

3 5 69

72

Make the next cardthe Leftmost-card

Page 31: Searching and Sorting Topics Linear and Binary Searches Selection Sort Bubble Sort.

Selection Sort

3 5 69

72

Lowest remaining cardis the left-most card sono swap is necessary

Page 32: Searching and Sorting Topics Linear and Binary Searches Selection Sort Bubble Sort.

Selection Sort

3 5 69

72

Make the next card the left-mostcard. It is the last card, so we aredone

Page 33: Searching and Sorting Topics Linear and Binary Searches Selection Sort Bubble Sort.

Activity Diagram High Level View

Find lowest card in hand

Swap it with the left-most

card

Make the left-most card the card to the right of the current

left-most card

Is this the last card?

Find the lowest cardIn the set of cards to the right of the current left-most card

endyes

no

start

Page 34: Searching and Sorting Topics Linear and Binary Searches Selection Sort Bubble Sort.

Study Lab #24 to see how a Bubble Sort works.