1 / 34

Searching and Sorting

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 search Write a linear search routine Write a binary search routine

herb
Télécharger la présentation

Searching and Sorting

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Searching and Sorting

  2. Topics Linear and Binary Searches Selection Sort Bubble Sort

  3. 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

  4. Find the person who is 23 years old.

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

  6. Write down your algorithm

  7. Searching an Array Linear Search Binary Search

  8. 0 1 2 3 4 5 6 7 8 9 Linear Search examScores 72 Problem: Determine which element in the array contains the score 87. 98 56 int thisOne = -1; for (int index = 0; index < SIZE; index++) { if (examScores[index] == 87) thisOne = index; } … 87 64 83 77 91 66 70

  9. Binary Search examScores In general, a binary search is much, much faster than a linear search, but requires that the array be sorted. 98 91 87 83 77 Start in the middle 72 Is this the one you are looking for (87)? 70 If not, is this number smaller than 87? 66 In this case it is. Therefore, we can elminate the entire bottom half of the array. Why? 64 Now try again, picking the middle of the remaining array elements.

  10. Arrange the people in order of age – youngest to oldest

  11. Rules You may only talk to one person at a time You may only ask “How old are you?” Responses will be in years and months. 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.

  12. Write down your algorithm

  13. 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

  14. Selection Sort Algorithm Development

  15. Selection Sort Step one: find the lowest card in the hand

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

  17. Selection Sort

  18. Selection Sort

  19. Selection Sort

  20. Selection Sort Now … Make the second card The left-most card left-most card

  21. Selection Sort Find the lowest card in the remaining cards

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

  23. Selection Sort Now make the third card the left-most card Left-most card

  24. Selection Sort And find the lowest card In the remaining cards

  25. Selection Sort Swap it with the left-most card

  26. Selection Sort

  27. Selection Sort Make the 4th card the left-most card

  28. Selection Sort Find the lowest card in the remaining cards

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

  30. Selection Sort Make the next card the Leftmost-card

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

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

  33. Activity Diagram High Level View start Make the left-most card the card to the right of the current left-most card Find lowest card in hand Swap it with the left-most card yes end Is this the last card? Find the lowest card In the set of cards to the right of the current left-most card no

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

More Related