Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Searching and Sorting Topics Linear and Binary Searches Selection Sort Bubble Sort."— Presentation transcript:

1

2 Searching and Sorting

3 Topics Linear and Binary Searches Selection Sort Bubble Sort

4 Objectives At the completion of this topic, students should be able to: Explain the difference between a linear and a binary search Write a linear search routine Write a binary search routine Write a bubble sort routine

5 Find the person who is 23 years old.

6 Rules 1.You may only talk to one person at a time 2.You may only ask “How old are you?” 3.The person must respond in years and months

7 Write down your algorithm

8 Searching an Array Linear Search Binary Search

9 Linear Search examScores 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 array contains the score 87. int thisOne = -1; for (int index = 0; index < SIZE; index++) { if (examScores[index] == 87) thisOne = index; } …

10 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 elminate the entire bottom half of the array. Why? Now try again, picking the middle of the remaining array elements.

11 Arrange the people in order of age – youngest to oldest

12 Rules 1.You may only talk to one person at a time 2.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.

13 Write down your algorithm

14 Sorting Sorting means to put data into some specified order. There are many, many algorithms that have been developed to sort data. In this section we will mention two of them: Selection Sort Bubble Sort

15 Selection Sort Algorithm Development

16 Selection Sort Step one: find the lowest card in the hand

17 Selection sort Step two: Swap the lowest card with the left-most card left-most card

18 Selection Sort

19

20

21 Now … Make the second card The left-most card left-most card

22 Selection Sort Find the lowest card in the remaining cards

23 Selection Sort left-most card It is already the left-most card, so no swap is required

24 Selection Sort Left-most card Now make the third card the left-most card

25 Selection Sort And find the lowest card In the remaining cards

26 Selection Sort Swap it with the left-most card

27 Selection Sort

28 Make the 4 th card the left-most card

29 Selection Sort Find the lowest card in the remaining cards

30 Selection Sort The lowest card is the left-most card, so no swap is necessary

31 Selection Sort Make the next card the Leftmost-card

32 Selection Sort Lowest remaining card is the left-most card so no swap is necessary

33 Selection Sort Make the next card the left-most card. It is the last card, so we are done

34 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 card In the set of cards to the right of the current left- most card end yes no start

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


Download ppt "Searching and Sorting Topics Linear and Binary Searches Selection Sort Bubble Sort."

Similar presentations


Ads by Google