12.07.2015 Views

CECS 103 Week 13 Bubble Sort Algorithm Chapter 7 Arrays and ...

CECS 103 Week 13 Bubble Sort Algorithm Chapter 7 Arrays and ...

CECS 103 Week 13 Bubble Sort Algorithm Chapter 7 Arrays and ...

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Quiz 6 – ANSWERS<strong>CECS</strong> <strong>103</strong> <strong>Week</strong> <strong>13</strong>Lecture: Finish <strong>Chapter</strong> 7Assign Program #6Program #5 (Due Nov. 18, midnight)Lab: Exercise in graphics with java1. Given the list of integers below, givethe intermediate sequences for eachof the 5 passes through the list,assuming a <strong>Bubble</strong> <strong>Sort</strong> algorithm(3 points)34 76 23 12 5 162. Write the algorithm for the <strong>Bubble</strong><strong>Sort</strong> (5 points)<strong>CECS</strong> <strong>103</strong> Fall 2002 Skubic 1<strong>CECS</strong> <strong>103</strong> Fall 2002 Skubic 6<strong>Bubble</strong> <strong>Sort</strong> <strong>Algorithm</strong>1. Initialize …n = number of items in list2. Repeat for n-1 times or until no swaps aremade2.1 Set CurrentValue to first item in list2.2 Set NextValue to second item in list2.3 Initialize Swap flag2.4 Repeat until CurrentValue points to the lastitem in the list2.4.1 If CurrentValue > NextValuethen swap elements in CurrentValue <strong>and</strong>NextValue; Set Swap flag2.4.2 Increment CurrentValue <strong>and</strong> NextValue<strong>CECS</strong> <strong>103</strong> Fall 2002 Skubic 7<strong>Chapter</strong> 7 <strong>Arrays</strong> <strong>and</strong> VectorsDeclaring <strong>Arrays</strong>, Creating <strong>Arrays</strong>, <strong>and</strong> Initializing<strong>Arrays</strong>Array of ObjectsCopying <strong>Arrays</strong>Multidimensional <strong>Arrays</strong><strong>Sort</strong>ing <strong>Arrays</strong> – Selection <strong>Sort</strong>, <strong>Bubble</strong> <strong>Sort</strong>Searching <strong>Arrays</strong> – Linear Search, Binary SearchThe Vector ClassNumeric Wrapper ClassesComm<strong>and</strong>-Line Arguments<strong>CECS</strong> <strong>103</strong> Fall 2002 Skubic 81


Linear SearchExample 7.3index key0 2212345678958292904211643075Search the list sequentially:How many comparisons does ittake to find…29?42?75?What happens when the list isVERY long?<strong>CECS</strong> <strong>103</strong> Fall 2002 Skubic 9public class LinearSearch{public static void main(String[] args) // Main method{int[] list = new int[10];System.out.print("The list is "); // Create the list r<strong>and</strong>omly <strong>and</strong> display itfor (int i=0; i


public class BinarySearch{public static void main(String[] args) // Main method{int[] list = new int[10];System.out.print("The list is "); // Create the list r<strong>and</strong>omly <strong>and</strong> display itfor (int i=0; i up) // The list has been exhausted without a matchreturn -1;int mid = (low + up) / 2;System.out.print("Enter a key "); // Prompt the user to enter a keyif (key < list[mid])int key = MyInput.readInt();return binarySearch(key, list, low, mid-1);else if (key == list[mid])int index = binarySearch(key, list); // Search for key using Binary Searchreturn mid;if (index != -1)elseSystem.out.println("The key is found in index "+index);return binarySearch(key, list, mid+1, up);else}System.out.println("The key is not found in the list");}} <strong>CECS</strong> <strong>103</strong> Fall 2002 Skubic <strong>13</strong><strong>CECS</strong> <strong>103</strong> Fall 2002 Skubic 14How many comparisons areneeded to find…index key0 21234567891123293042586475902?30?90?3 comparisons1 comparisons4 comparisons<strong>CECS</strong> <strong>103</strong> Fall 2002 Skubic 15In the worst case, thecomparisons needed are …List size Comparisons8 = 2 3 416=2 4 532=2 5 664=2 6 7128=2 7 8256=2 8 9512=2 9 101024=2 10 11But the listmust besorted first!<strong>CECS</strong> <strong>103</strong> Fall 2002 Skubic 163

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!