Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 11 Searching and Sorting Richard Gesick.

Similar presentations


Presentation on theme: "Lecture 11 Searching and Sorting Richard Gesick."— Presentation transcript:

1 Lecture 11 Searching and Sorting Richard Gesick

2 The focus Searching - examining the contents of the array to see if an element exists within the array Sorting - rearranging the elements of the array to be in a particular order

3 Linear Search Also called sequential search
Examines all values in an array until it finds a match or reaches the end Number of visits for a linear search of an array of n elements - The average search visits n/2 elements - The maximum visits is n A linear search is an O(n) algorithm

4 Linear Search Algorithm
steps = 0; foreach (int temp in G) { steps++; if (temp == find) return true; } return false;

5 Binary Search Locates a value in a sorted array by determining whether the value occurs in the first or second half, then repeating the search in one of the halves Number of visits to search an sorted array of size n? We visit one element (the middle element) then search either the left or right subarray Binary search is a O( log(n) ) algorithm

6 Binary Search Algorithm
int low=0; int high= G.Length; int mid = 0; while (!found) { steps++; mid = (low + high) / 2; if (find == G[mid]) return true; else if (find < G[mid]) high = mid; else low = mid; if (low > high-1 || high < low +1) return false; }

7 Sorting There are many ways to order elements within a collection.  There is typically a trade-off between complexity of the algorithm and the performance of the algorithm. a simple approach to sorting realize that it is slower than other methods that are more complex to code yet more efficient.

8 Bubble Sort Repeatedly pair-wise examine elements and swap them if needed; this is called a "Bubble sort" since elements "bubble" to their correct positions. 

9 Bubble sort explained int [] A={8, 4, 2, 1};
The bubble sort finds the smallest value and then the next smallest etc. It does it by pair-wise comparisons and swaps. 8 and 4, 4 is smaller so it gets swapped, A = 4, 8, 2, 1 4 and 2, 2 is smaller so it gets swapped, A= 2, 8, 4, 1 2 and 1, 1 is smaller so it gets swapped, A= 1,8,4,2 The first pass has been completed so now we start on the same process with index 1 8 and 4, 4 is smaller, swap and A= 1, 4,8,2 4 and 2, 2 is smaller, swap and A= 1, 2, 8, 4 That completes that pass and now the first 2 values are in the correct position. Now the third pass looks at 8 and 4 4 is smaller, swap and A= 1, 2, 4, 8 We’re done. Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

10 Bubble Sort Algorithm for (int i = 0; i < A.Length - 1; i++)
for (int j = i+1; j < A.Length; j++) { if (A[j] < A[i]) int temp = A[j]; A[j] = A[i]; A[i] = temp; } //notice the extra variable temp to make the swap process easy without accidently misassigning values

11 Selection Sort The selection sort algorithm repeatedly finds the smallest element in the unsorted tail region of the array and moves it to the front

12 Sorting an Array of Integers
Array in original order Find the smallest and swap it with the first element Find the next smallest. It is already in the correct place Find the next smallest and swap it with the first element of unsorted portion Repeat When the unsorted portion is of length 1, we are done

13 Selection Sort Algorithm
int i = 0; int j = 0; for (i = 0; i < B.Length - 1; i++) { int minPos = i; for (j = i + 1; j < B.Length; j++) { if (B[j] < B[minPos]) minPos = j; } if (i != minPos && minPos < B.Length) { int temp = B[minPos]; B[minPos] = B[i]; B[i] = temp;

14 The bubble sort and selection sort
This algorithms are slow when run on large data sets because of the large number of swaps that are done during the processing. Both of these algorithms and the insertion sort that is coming up next are considered to be Big-0 of n squared, O(n2). To keep things simple, consider the n to be the growth factor, so if a data set doubles in size, 2 would be 2, while if it tripled in size, n would be 3. O(n2) means that the time required grows by the square of the factor so if n was 2, the time would grow by about a factor of 4. If n was 3, by a factor of about 9.

15 Time Taken By Selection Sort on Various Size Arrays

16 Insertion Sort Consider what you would do to sort a set of cards.
This approach is an insertion sort - taking a new item and place it correctly relative to the other items in the "finished" portion.  You continually maintain two sets - unexamined, unsorted cards and a set of examined/sorted cards.  For each card in the unexamined set, take it out of that set and place it correctly relative to the examined set.

17 Insertion Sort Algorithm
public void insertionSort (int[] list) { for (int index = 1; index < list.Length; index++) int key = list [index]; int position = index; // Shift larger values to the right while (position > 0 && key < list[position-1]) list [position] = list [position - 1]; position--; } list [position] = key;

18 Insertion sort explained
int [] A= { 8, 4, 2, 1}; the key = 4, index = 1 and position =1 4 < 8 and position > 0 so the while loops shifts 8 into the 4’s position. A= 8,8,2,1 then position-- so now position =0. The while loop stops. key (4) gets assigned to A[position] so A = 4,8,2,1 Now index goes to 2, key =2 and position =2 2 < 8 and position > 0 so the while loops shifts 8 into the 2’s position. A= 4,8,8,1 then position-- so now position =1. The while loop continues. 2< 4 so the while loops shifts 4 into the first 8’s position. A=4,4,8,1, position -- so now position=0, the while loop ends key (2) gets assigned to A[position] so A = 2,4,8,1 Then one more pass with 1, shifting 8,4,2 right 1 space each so then A= 1,2,4,8 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

19 Sorting in a C# Program The Array and List classes contains sort methods. To use them, the data type you are sorting must implement the IComparble interface. List<string> myList= new List<string>(); . . . myList.Sort(); To sort an array of integers int [] nums= new int [50]; Array.Sort(nums);


Download ppt "Lecture 11 Searching and Sorting Richard Gesick."

Similar presentations


Ads by Google