160 likes | 281 Views
Topic 9B – Array Sorting and Searching. Algorithms. An algorithm is a set of steps that can be followed to solve a problem. Designing and developing an algorithm is often the most difficult part of the problem-solving process.
E N D
Algorithms • An algorithm is a set of steps that can be followed to solve a problem. • Designing and developing an algorithm is often the most difficult part of the problem-solving process. • For each problem, there can be many different algorithms that correctly solve the problem. CISC105 – Topic 9B
Algorithms • Some algorithms may operate much faster than others that do the same thing (algorithms that solve the same problem may vary in efficiency). • Some algorithms may be easier to understand and may “make more sense” than others that do the same thing (algorithms that solve the same problem may vary in complexity). CISC105 – Topic 9B
Introduction to Sorting • Many of the algorithms that operate on data contained within an array require the data in that array to be sorted. • This means that the data contained in the array needs to be in order, from lowest-to-highest, or vice versa. • We will examine a simple technique for putting the array in lowest-to-highest order. • Note that once this problem is solved, the reverse, putting the array in highest-to-lowest order, is trivial. CISC105 – Topic 9B
Introduction to Sorting • There are many approaches to sorting. • Some sorting algorithms are much better than others. • We will examine one of the simplest sorting algorithms, a selection sort. • This algorithm is intuitive (easy-to-understand), although it is not very efficient. CISC105 – Topic 9B
Selection Sort:The Basic Idea • The basic idea of the selection sort is to process the array from left-to-right (index 0, index 1, etc…) • At each element, we look to the right and find the lowest value. Once we find the lowest value (to the right of the current element), we swap the two elements. • Then, we move on to the next element and repeat the process. CISC105 – Topic 9B
Selection Sort:The Algorithm (1) current_element = 0 (2) Find index_of_min, the index of the smallest element in the subarray, array[current_element] to array[size – 1] (3) If current_element does not equal index_of_min, swap elements at current_element and index_of_min (4) If current_element = size – 1 , stop. Else current_element++ & goto step (2) CISC105 – Topic 9B
Selection Sort : An Example 24 98 4 3 55 62 3 4 24 98 24 98 55 62 98 98 X[0] X[1] X[2] X[3] X[4] X[5] CISC105 – Topic 9B
Sorting Algorithms • Keep in mind that a selection sort is simply one method of sorting an array. • It is a very simple-to-understand approach. • It is also not very efficient. • Other sorting algorithms that are much more efficient, although much harder-to-understand, include the bubble sort and the quicksort. We will not discuss these algorithms. CISC105 – Topic 9B
Introduction to Searching • Searching an array is a closely related problem to sorting an array. • The simplest approach to array searching is the linear search. • This searching method consists of examining the array elements from left to right. • If the target is found, we save the index where it was found and stop. If not, and there are still more array elements to the right, we move to the next element and repeat. CISC105 – Topic 9B
The Linear Search • Thus, we simply start at index 0 and see if the target is there. If so, we stop. • If not, we move to index 1 and see if the target is there. • If not, we move to index 2 and see if the target is there. • etc… CISC105 – Topic 9B
Linear Search: An Example We will perform a linear search on the array X in order to find the target value of 55. 24 98 4 3 55 62 X[0] X[1] X[2] X[3] X[4] X[5] 55 is located at index 4! CISC105 – Topic 9B
Searching Algorithms • The linear search is not a very efficient algorithm. • Notice that this algorithm does not depend on, or require, that the array is sorted, or in any order at all. • If the array is sorted, we can use a much more efficient searching algorithm, the binary search. CISC105 – Topic 9B
The Binary Search • The binary search is very intuitive. (1) Begin with the entire array (2) Select the middle element. (3) If the target is less than the middle element, choose the next subarray to be the half of the array to the left (smaller than) the middle element. If the target is greater than the middle element, choose the next subarray to be the half of the array to the right (greater than) the middle element. (4) Then, repeat on the new subarray. CISC105 – Topic 9B
Binary Search: An Example We will perform a binary search on the array X in order to find the target value of 55. 3 4 24 25 55 62 98 X[0] X[1] X[2] X[3] X[4] X[5] X[6] 55 is located at index 4! CISC105 – Topic 9B
Summary • Searching and sorting arrays are common problems in computer science. • Both problems, like almost all problems, have multiple solutions. • Solutions vary in terms of their complexity and efficiency. • We have examined the selection sort, the linear search, and the binary search. CISC105 – Topic 9B