Lecture 5 Searching and Sorting Richard Gesick 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 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. 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. Bubble Sort Algorithm for (int i = 0; i < A.Length - 1; i++) for (int j = i; j < A.Length; j++) { if (A[j] < A[i]) { int temp = A[j]; A[j] = A[i]; A[i] = temp; } } 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 Sorting an Array of Integers Array in original order 11 9 17 5 12 Find the smallest and swap it with the first element 5 9 17 11 12 Find the next smallest. It is already in the correct place 5 9 17 11 12 Sorting an Array of Integers (2) Find the next smallest and swap it with the first element of unsorted portion 5 9 11 17 12 Repeat 5 9 11 12 17 When the unsorted portion is of length 1, we are done 5 9 11 12 17 This algorithm is slow when run on large data sets Time Taken By Selection Sort on Various Size Arrays 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; } } 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. Insertion Sort Algorithm List<int> inOrder = new List<int>(); foreach (int temp in C) { bool added = false; for (int i = 0; i < inOrder.Count; i++) { if (temp < inOrder[i]) { inOrder.Insert(i, temp); added = true; break; } } if (!added) inOrder.Add(temp); } Sorting in a C# Program The List class contains sort methods To sort an array of integers convert to a List and call the sort method List<int> E=D.ToList<int>(); E.Sort(); 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 Linear Search Algorithm steps = 0; foreach (int temp in G) { steps++; if (temp == find) return true; } return false; 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 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; }