Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Searching and Sorting Algorithms

AllenHeard
January 05, 2017

Searching and Sorting Algorithms

Year 12 Lesson

AllenHeard

January 05, 2017
Tweet

More Decks by AllenHeard

Other Decks in Education

Transcript

  1. Searching algorithms ▪ A search is a method for finding

    an item of data. ▪ Linear search ▪ Binary search
  2. Linear search ▪ The simplest type of search is a

    linear search (serial or sequential search), which basically starts at the beginning of a file, reading each record until the required record is found. ▪ Not an efficient method of searching but if the data is not sorted, this is the only method of searching.
  3. Linear Search ▪ Take the following unordered list: ▪ If

    we want to search for Doris for instance, we have to make 10 comparisons before we find it (worst case scenario in this instance) best case is that Doris is at the start of the list. Fred Sarah Alfie Susan Bill Hans Blodwyn Robert David Doris
  4. Linear search task ▪ Implement the linear search as described

    and using the algorithm, test that it works with items in and not in the list ▪ Hard code a list in the program and then add code to the algorithm to ask the user for a term to search for. ▪ Add a counter to report how many searches have been done for each item searched for. ▪ Add the functionality to add an item to the list if it is not found.
  5. Binary Search ▪ Binary search is used to find an

    item in an ordered list. ▪ For example, we want to find a number in the list below: ▪ To search for an item, look in the middle of the list and see if the number you want is in the middle, above the middle or below the middle. If it is in the middle, you have found the item. If it is higher than the middle value, then adjust the bottom of the list so that you search in a smaller list starting one above the middle of the list. If the number is lower than the middle value, then adjust the top of the list so that you search in a smaller list which has its highest position one less than the middle position. 2 5 7 12 14 21 28 31 36
  6. Binary Search ▪ Suppose you have the following sorted list

    below and are using the recursive binary search algorithm. ▪ Which group of numbers correctly shows the sequence of comparisons used to find the key 8? ▪ 11, 5, 6, 8 ▪ 12, 6, 11, 8 ▪ 3, 5, 6, 8, ▪ 18, 12, 6, 8 3 5 6 8 11 12 14 15 17 18
  7. Binary Search ▪ Suppose you have the following sorted list

    below and are using the recursive binary search algorithm. ▪ Which group of numbers correctly shows the sequence of comparisons used to find the key 8? ▪ 11, 5, 6, 8 ▪ 12, 6, 11, 8 ▪ 3, 5, 6, 8, ▪ 18, 12, 6, 8 3 5 6 8 11 12 14 15 17 18
  8. Binary Search ▪ Suppose you have the following sorted list

    below and are using the recursive binary search algorithm. ▪ Which group of numbers correctly shows the sequence of comparisons used to find the key 16? ▪ 11, 14, 17 ▪ 18, 17, 15 ▪ 14, 17, 15 ▪ 12, 17, 15 3 5 6 8 11 12 14 15 17 18
  9. Binary Search ▪ Suppose you have the following sorted list

    below and are using the recursive binary search algorithm. ▪ Which group of numbers correctly shows the sequence of comparisons used to find the key 16? ▪ 11, 14, 17 ▪ 18, 17, 15 ▪ 14, 17, 15 ▪ 12, 17, 15 3 5 6 8 11 12 14 15 17 18
  10. Binary search task ▪ Implement the binary search as described

    and using the algorithm, test that it works with items in and not in the list ▪ Add a counter to report how many searches have been done for each item searched for.
  11. Sorting algorithms ▪ Sorting is a very common operation in

    computing, yet in practice it is quite a tricky operation to program. ▪ Application programs such as Microsoft Excel make sorting seem very simple (both ascending and descending), and yet the programming involved is quite complex. ▪ Sorting can be split into 2 main parts, namely internal sorts and external sorts. Internal sorts are the main type of sorts and include such examples such as quicksort, bubble sort and insertion sort. These sorts are small enough to fit into the computer’s memory.
  12. Bubble sort ▪ Start from the left and compare each

    pair of numbers. If the smallest is on the left, then leave them. If not, swap them around. ▪ The bubble sort is quick for nearly sorted files. For example, files which have been recently sorted but may have had a few more records added at the end. ▪ Move on to compare the next pair of numbers. ▪ C = Number of comparisons E = number of exchanges (swaps) ▪ Using the steps indicated above, perform the first pass of the algorithm across the list. ▪ Next, perform further passes until the list is sorted. 23 12 34 17 21 18
  13. Bubble sort ▪ After first pass the largest number should

    be on the right. C=5 and E=4 ▪ After the second pass the last two numbers are in the correct position: ▪ After the third pass: ▪ After the fourth pass: ▪ Once a pass produces no exchanges then the file is sorted and the algorithm can stop. 12 23 17 21 18 34 C=5 E=4 12 17 21 18 23 34 C=4 E=3 12 17 18 21 23 34 C=3 E=1 12 17 18 21 23 34 C=2 E=0
  14. Bubble sort task ▪ Extend your program by adding a

    counter to count the number of comparisons.
  15. Insertion sort ▪ Insertion sort is the simplest sorting algorithm.

    The algorithm has three main steps, as outlined below. Consider the following list of numbers: ▪ Note: Lower elements mean elements to the left of a number and the upper elements mean the elements to the right of a number. ▪ 1. Start with the second number in the list (12 in the above list). You will see why in a moment. ▪ 2. Compare the above number with all the other lower elements and insert the number in the right place. ▪ 3. Repeat the above two stages, but moving to the third, then fourth and fifth etc. numbers until the last number has been considered in stage (1). 21 12 32 17 19 25
  16. Insertion sort ▪ 1. Start with the second number in

    the list. ▪ 2. Compare the number with all the other lower elements and insert the number in the right place. ▪ 3. Repeat the above two stages, but moving to the third, then fourth and fifth etc. numbers until the last number has been considered in stage (1). 21 12 32 17 19 25
  17. Insertion sort task ▪ Add a counter to report how

    many comparisons have been done during the sorting process. ▪ Compare the number of comparisons for the insertion sort with the bubble sort