Slide 1

Slide 1 text

SEARCHING AND SORTING Algorithms

Slide 2

Slide 2 text

Searching algorithms ■ A search is a method for finding an item of data. ■ Linear search ■ Binary search

Slide 3

Slide 3 text

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.

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

Linear Search ■ A linear search algorithm in python might look like this:

Slide 6

Slide 6 text

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.

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

Binary Search ■ A Binary search algorithm in python might look like this:

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

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.

Slide 14

Slide 14 text

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.

Slide 15

Slide 15 text

Sorting algorithms ■ Bubble sort ■ Insertion sort

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

Bubble sort ■ A bubble sort algorithm in python might look like this:

Slide 19

Slide 19 text

Bubble sort task ■ Extend your program by adding a counter to count the number of comparisons.

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

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

Slide 22

Slide 22 text

Insertion sort ■ An insertion sort algorithm in python might look like this:

Slide 23

Slide 23 text

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

Slide 24

Slide 24 text

Sorting Algorithms ■ https://visualgo.net/sorting