public static int linearSearch(int [] list, int val){ for (int i = 0; i < list.length; i++) if (list[i] == val) return i; return 1; }Exercise 1: Referring to Example 1, how many entries do you have to examine (on average) to find the position of val in list? How many entries do you have to examine (on average) to determine that val is not in list? How would you use linear search on an array of double?
public static int linearSearch(Comparable [] list, Comparable val){ for (int i = 0; i < list.length; i++) if (list[i].compareTo(val) == 0) return i; return 1; }Exercise 3: Describe a linear search strategy for looking up a name in the telephone book. Do you use a linear search when using the telephone book?
public static int binarySearch(int [] list, int value) { int low = 0; int high = list.length  1; while (low <= high) { int mid = (low + high)/2; if (list[mid] == value) return mid; else if (list[mid] < value) low = mid + 1; else high = mid  1; } return 1; }Exercise 4: How would the code change if the array list were sorted in descending order? Exercise 5: For an array of size n, how many times would you expect to repeat the dividing process?
n  log_{2} (n) 
2^{5} = 32  
2^{10} = 1024  
2^{15} = 32,768  
2^{20} = 1,048,576 
public static void selectionSort(Comparable [] list) { for (int index = 0; index < list.length  1; index++) { int min = findMinPosition(list, index); Comparable temp = list[min]; list[min] = list[index]; list[index] = temp; } } public static int findMinPosition(Comparable [] list, int start) { int min = start; for (int scan = start + 1; scan < list.length; scan++) if (list[min].compareTo(list[scan]) > 0 ) min = scan; return min; }Exercise 7: Fill in the following table with the steps of the selection sort. Show the minimum position at each stage.
You can find a solution here.
list[0]  list[1]  list[2]  list[3]  list[4]  list[5]  list[6]  list[7]  list[8]  list[9] 










public static void selectionSort(int [] list) { for (int index = 0; index < list.length  1; index++) { int min = findMinPosition(list, index); int temp = list[min]; list[min] = list[index]; list[index] = temp; } } public static int findMinPosition(int [] list, int start) { int min = start; for (int scan = start + 1; scan < list.length; scan++) if (list[min] > list[scan]) min = scan; return min; }
Exercise 9: Compute the order of the selectionSort algorithm
for n elements.
Ans: The first time through the loop, you examine n elements, the
second time n  1, etc. The sum of the integers from 1 to n is n*(n+1)/2. Therefore
the selectionSort is O(n^{2}).
public static void printArray(Comparable[] list, String msg) { System.out.print("\n["); for (int i = 0; i < list.length; i++) System.out.print(" " + list[i]); System.out.println(" ]: " + msg); }
public static void insertionSort (Comparable [] list) { for (int index = 1; index < list.length; index++) { insertItem(list, index); } } public static void insertItem(Comparable [] list, int index) { Comparable key = list[index]; int position = index; // shift larger values to the right while (position > 0 && key.compareTo(list[position1]) < 0) { list[position] = list[position1]; position; } list[position] = key; }Exercise 10: Fill in the following table with the steps of the insertion sort. You can find a solution here.
list[0]  list[1]  list[2]  list[3]  list[4]  list[5]  list[6]  list[7]  list[8]  list[9] 










You can find a sample to print out here and a solution here.
You can also find additional examples here.
For many programs, the running time mainly depends on how many times the body of the (inner) loop is executed.
Example 5: Suppose it takes at most 10 ms. to do a linear search
on an array if size 100,000. How long will it take to do a linear search
on a similar array of size 1,000,000.
Answer: Since the number of times through he loop is the size of the
array (in the worst case) and the second array is 10 times an large, it
should take 10 times as long, or 100ms.
In general:
loop iterations of A running time of A  =  loop iterations of B running time of BIn the above example, if the unknown running time is t:
100,000 10 ms.  =  1,000,000 tSolving gives t = 100 ms.
Exercise 11: It takes at most 2 seconds to sort an array of size 10,000 using a selection sort. How long would it take to sort a similar array of size 30,000.
Answer: Since it takes about n^{2}/2 iterations of the inner loop to do an insertion sort on an arry of size n:
(10,000)^{2}/2 2 seconds  =  (30,000)^{2}/2 t secondst = 18 seconds.
Notice that the factor of 1/2 on the left side cancels out, so we get the same answer if we use n^{2} for the number of iterations instead of n^{2}/2. This is another way of saying that the selection sort algorithm is O(n^{2}).
Here is a summary for the algorithms we have done so far.
In each case the array has size n.
algorithm  loop iterations 
linear search  n 
binary search  log(n) 
selection sort  n^{2 } 
insertion sort  n^{2} 
private static final double BILLION = 1000000000.0; long startTime, endTime; startTime = System.nanoTime(); QuadraticSort.insertionSort(list); endTime = System.nanoTime(); System.out.println("Number of seconds to do the insertion sort is "+ (endTime  startTime)/BILLION);
Add some timing tests to the Case Study 14. How large can the arrays get before sorting takes a long time?
Below are some useful methods for creating large arrays to sort:
public static void printArray(int[] array) { for (int i = 0; i < array.length; i++) System.out.print(array[i] + " "); System.out.println(); } public static int[] makeArray(int n) { Random rand = new Random(); int[] array = new int[n]; for (int i = 0; i < n; i++) array[i] = rand.nextInt(10*n); return array; } public static int[] cloneArray(int[] array) { int[] newArray = new int[array.length]; for (int i = 0;i < array.length; i++) newArray[i] = array[i]; return newArray; }
The quadratic sorts we discussed here are too slow for large arrays because n^{2} grows quickly for large values of n. Next semester we will look at sorting methods that take about n log_{2} steps.
n  log_{2}(n)  n log_{2}(n)  n^{2 } 
2^{5} = 32  
2^{10} = 1024  
2^{15} = 32,768  
2^{20} = 1,048,576 
The Java Arrays class has efficient static methods for sorting
arrays. For example, to sort an array, list, of ints
you can use:
Arrays.sort(list).