Problem Solving #6: Search & Sort ICS
2 Outline Review of Key Topics Review of Key Topics Problem 1: Recursive Binary Search … Problem 1: Recursive Binary Search … Problem 2: Tracing …. Problem 2: Tracing …. Problem 3: Element Insertion … Problem 3: Element Insertion … Problem 4: Sorted Array Reverse … Problem 4: Sorted Array Reverse … Problem 5: Partition Method … Problem 5: Partition Method …
Form Groups of 3 Students and Work on the Following Problem
4 Review of Main Topics Search – –Sequential (linear) search – –Binary search Sort – –Selection sort – –Insertion sort – –Bubble sort – –Merge sort – –Quick sort
5 Problem 1: public static int binarySearch(int[] list, int listLength, int key) { int first = 0, last = listLength - 1; int mid; boolean found = false; while (first <= last && !found) { mid = (first + last) / 2; if (list[mid] == key) found = true; else if(list[mid] > key) last = mid - 1; else first = mid + 1; } if (found) return mid; else return –1; } //end binarySearch Recall
6 Problem 1 … a) Implement the binary search using recursion b) Write a recursive equation for its running time c) Find a closed form solution for the recursive equation in part (b)
7 Solution 1 int recBS(int[] a, int target, int lo, int hi){ 2 int mid = -1; 3 if(lo <= hi) { 4 mid = (hi + lo) / 2; 5 if(target < a[mid]) 6 mid = recBinary(a, target, lo, mid - 1); 7 else if(target > a[mid]) 8 mid = recBinary(a, target, mid + 1, hi); 9 } 10 return mid; 11 }
8 Solution … T(n) = T(n/2) + 1 T(n) = T(n/2) + 1 T(n) = {T(n/2 2 )+1}+1 T(n) = {T(n/2 2 )+1}+1 = T(n/2 2 )+2 = T(n/2 2 )+2 = {T(n/2 3 )+1}+2 = {T(n/2 3 )+1}+2 = T(n/2 3 )+3 = T(n/2 3 )+3 …… …… = T(n/2 k )+k = T(n/2 k )+k = T(1)+k when n/2 k =1 k=log 2 n = T(1)+k when n/2 k =1 k=log 2 n Since T(1) = 1 then T(n) = O(log 2 n)
9 Problem 2 public static void insertionSort(int[] list, int listLength) { int firstOutOfOrder, location; int temp; for (firstOutOfOrder = 1; firstOutOfOrder < listLength; firstOutOfOrder++) if (list[firstOutOfOrder] < list[firstOutOfOrder - 1]) { temp = list[firstOutOfOrder]; location = firstOutOfOrder; do { list[location] = list[location - 1]; location--; } while(location > 0 && list[location - 1] > temp); list[location] = temp; } } //end insertionSort public static void selectionSort(int[] list, int listLength) { int index; int smallestIndex; int minIndex; int temp; for (index = 0; index < listLength – 1; index++) { smallestIndex = index; for (minIndex = index + 1; minIndex < listLength; minIndex++) if (list[minIndex] < list[smallestIndex]) smallestIndex = minIndex; temp = list[smallestIndex]; list[smallestIndex] = list[index]; list[index] = temp; } Recall
10 Problem 2 … Given the following array Given the following array A = {4, 8, 3, 5, 1, 7} Trace each of the following sorting algorithms and show the array content after each iteration Trace each of the following sorting algorithms and show the array content after each iteration –Insertion sort –Selection sort
11 Problem 3 Insert a given item in its right place in a sorted array such that the array remains sorted Insert a given item in its right place in a sorted array such that the array remains sorted What is the running time? What is the running time? Can you think of another alternative? Can you think of another alternative?
12 Problem 4 Reverse the content of an ordered array Reverse the content of an ordered array –Using a temporary array –In the same array What is its running time? What is its running time?
13 Problem 5 Given the following array Given the following array A = {4, 8, 3, 5, 1, 7} Trace the partition method in quick sort. Trace the partition method in quick sort. Derive its running time if the array has length n. Derive its running time if the array has length n. private int partition(int from, int to) { int pivot = a[from]; int i = from - 1; int j = to + 1; while (i pivot) j--; if (i < j) swap(i, j); } return j; } Recall