COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

Size: px
Start display at page:

Download "COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand"

Transcription

1 COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

2 COSC 236 Web Site You will always find the course material at: or or From this site you can click on the COSC-236 tab to download the PowerPoint lectures, the Quiz solutions and the Laboratory assignments. 2

3 3

4 Review of Quiz 21 Use Quiz 20 as your model for the input dialogue Set up the random numbers as specified in item 2, Quiz 21 The main program will use: int inversions = inversion(array); To count the inversions. Next calculate the percent The main program prints out the array, inversions and percent Finally, you need to write the method inversion to count the inversions (using Lecture 20 slide 65) 4

5 Review of Quiz 21 import java.util.*; The input dialogue: 1. Prompt for the size of the array 2. Get size from console and create the array public class Quiz21 { public static void main(string[] args) { Scanner console = new Scanner(System.in); System.out.print("How many random integers do you want? "); int number = console.nextint(); double[] array = new double[number]; // array to store random numbers System.out.print("What is the minimum number? "); int min = console.nextint(); System.out.print("What is the maximum number? "); int max = console.nextint(); int range = max - min +1; 3. Prompt for minimum 4. Get minimum from console 5. Prompt for maximum 6. Get maximum from console 7. Set random numbers 5

6 Review of Quiz 21 Complete the main program: Random r = new Random(); for (int i = 0; i < number; i++) { // generate random number array[i] = r.nextint(range) + min; int inversions = inversion(array); double percent = 200.0*inversions/number/(number-1); System.out.printf("Original array = %s contains %d inversions (%.1f%s).\n", Arrays.toString(array), inversions, percent, "%"); NOTE: Trick to print out % in printf statement 1. Create random object 2. Fill array with random numbers 3. Calculate inversions 4. Calculate percent 5. Print results 6

7 Review of Quiz 21 Method inversion(): public static int inversion(double[] data) { int count = 0; for (int i = 0; i < data.length - 1; i++) { for (int j = i + 1; j < data.length; j++) { if (data[i] > data[j]) { count++; return count; 1. Returns integer (# of inversions) 2. Accepts an array 3. Initialize the inversion count 4. Optimized inversion algorithm 5. Returns inversion count 7

8 Review of Quiz 21 import java.util.*; public class Quiz21 { public static void main(string[] args) { Scanner console = new Scanner(System.in); System.out.print("How many random integers do you want? "); int number = console.nextint(); double[] array = new double[number]; // array to store random numbers System.out.print("What is the minimum number? "); int min = console.nextint(); System.out.print("What is the maximum number? "); int max = console.nextint(); int range = max - min +1; Random r = new Random(); Complete Program: for (int i = 0; i < number; i++) { // generate random number array[i] = r.nextint(range) + min; int inversions = inversion(array); double percent = 200.0*inversions/number/(number-1); System.out.printf("Original array = %s contains %d inversions (%.1f%s).\n", Arrays.toString(array), inversions, percent, "%"); public static int inversion(double[] data) { int count = 0; for (int i = 0; i < data.length - 1; i++) { for (int j = i + 1; j < data.length; j++) { if (data[i] > data[j]) { count++; // System.out.println("(" + data[i] + ", " + data[j] + ")"); return count; This program is suitable for copy and paste to DrJava 8

9 Review of Quiz 21 OUTPUT: This is the array of random numbers printed out using Arrays.toString() This is the actual number of inversions in the array This is the percentage of inversions in the array 9

10 Review of Quiz 21 OUTPUT: If the percent of inversions is 0%, the array is sorted in ascending order If the percent of inversions is 100%, the array is sorted in descending order This array is nearly a perfect random array (50% inversions) With only 10 elements in the array, your results can vary greatly from the ideal 50% 10

11 Introduction (pp ) Binary Search(pp ) Sorting (pp ) Shuffling (pp ) Custom Ordering with Comparators (pp ) 13.2 Program Complexity Introduction (pp ) Empirical Analysis (pp ) Complexity Classes (pp ) 11

12 Introduction (pp ) In this chapter we ll look at ways to use Java s class libraries to search and sort data. We ll practice implementing some searching and sorting algorithms and talk more generally about how to observe and analyze the runtimes of algorithms. 12

13 Introduction (pp ) Since we have skipped a number of chapters: There will be things in this chapter that we have not covered (part of COSC-237) We will either ignore these or mention them briefly Our goal is to explore searching and sorting which we can do with what we have already covered 13

14 Introduction (pp ) How could we search an array to find the index of a particular value (target)? A simple approach would be to start at the beginning of the array (index = 0) and search sequentially until we found the desired value (target) This is called a sequential search The nice thing about a sequential search is that it works on any array whether sorted or unsorted 14

15 Introduction (pp ) Sequential search sequential search: Locates a target value in an array/list by examining each element from start to finish. How many elements will it need to examine? Example: Searching the array below for the value 42: index value i Notice that the array is sorted. Could we take advantage of this? Target found at index = elements were searched 15

16 Introduction (pp ) Binary search (13.1) binary search: Locates a target value in a sorted array/list by successively eliminating half of the array from consideration. How many elements will it need to examine? Example: Searching the array below for the value 42: Target found at index = 10 index value Only 3 elements were searched min mid max 16

17 Introduction (pp ) Binary Search(pp ) Sorting (pp ) Shuffling (pp ) Custom Ordering with Comparators (pp ) 13.2 Program Complexity Introduction (pp ) Empirical Analysis (pp ) Complexity Classes (pp ) 17

18 Introduction (pp ) Binary Search(pp ) 18

19 Binary Search(pp ) An algorithm that searches for a value in a sorted list by repeatedly dividing the search space in half The Binary Search Algorithm looks at the center element and determines if the target is below or above the center The algorithm then discards the half where the target does not lie and repeats the process on the half where the target does lie 19

20 Binary Search(pp ) Let s search for the number 73 in a sorted array of integers 20

21 Binary Search(pp ) Let s search for the number 73 in a sorted array of integers Discard lower region 21

22 Binary Search(pp ) Let s search for the number 73 in a sorted array of integers Discard upper region 22

23 Binary Search(pp ) Let s search for the number 73 in a sorted array of integers Discard lower region 23

24 Binary Search(pp ) Let s search for the number 73 in a sorted array of integers Discard lower region 24

25 Binary Search(pp ) Let s search for the number 73 in a sorted array of integers Discard lower region The number must be 73 25

26 Binary Search(pp ) Java includes a binary search method for arrays: import java.util.*; Returns the index of the location in the array numbers of the target integer 29 public class Lecture22S23 { public static void main(string[] args) { int[] numbers = {-3, 2, 8, 12, 17, 29, 44, 58, 79; int index = Arrays.binarySearch(numbers, 29); System.out.println("29 is found at index " + index); 26

27 Binary Search(pp ) Java includes a binary search method for arrays: import java.util.*; public class Lecture22S23 { public static void main(string[] args) { int[] numbers = {-3, 2, 8, 12, 17, 29, 44, 58, 79; int index = Arrays.binarySearch(numbers, 29); System.out.println("29 is found at index " + index); Output from program using binary sort However, this only works for sorted arrays If the array is not sorted, we must sort the array before the binary search 27

28 Introduction (pp ) Binary Search(pp ) Sorting (pp ) Shuffling (pp ) Custom Ordering with Comparators (pp ) 13.2 Program Complexity Introduction (pp ) Empirical Analysis (pp ) Complexity Classes (pp ) 28

29 Introduction (pp ) Binary Search(pp ) Sorting (pp ) 29

30 Sorting (pp ) We already know that Java has a sort method: import java.util.*; public class Lecture22S30 { public static void main(string[] args) { int[] numbers = {79, 17, -3, 2, 8, 58, 12, 44, 29; System.out.println("Original array: " + Arrays.toString(numbers)); Arrays.sort(numbers); System.out.println(" Sortedl array: " + Arrays.toString(numbers)); int index = Arrays.binarySearch(numbers, 29); System.out.println("29 is found at index " + index); 30

31 Sorting (pp ) We already know that Java has a sort method: import java.util.*; public class Lecture22S30 { public static void main(string[] args) { int[] numbers = {79, 17, -3, 2, 8, 58, 12, 44, 29; System.out.println("Original array: " + Arrays.toString(numbers)); Arrays.sort(numbers); System.out.println(" Sorted array: " + Arrays.toString(numbers)); int index = Arrays.binarySearch(numbers, 29); System.out.println("29 is found at index " + index); Start with an unsorted array Print out the original unsorted array Use Java Arrays.sort() 31

32 Sorting (pp ) We already know that Java has a sort method: import java.util.*; Print out the sorted array to confirm that it is sorted Invoke the binary search to look for the integer 29 in the array numbers Print out the result of the binary search public class Lecture22S30 { public static void main(string[] args) { int[] numbers = {79, 17, -3, 2, 8, 58, 12, 44, 29; System.out.println("Original array: " + Arrays.toString(numbers)); Arrays.sort(numbers); System.out.println(" Sorted array: " + Arrays.toString(numbers)); int index = Arrays.binarySearch(numbers, 29); System.out.println("29 is found at index " + index); 32

33 Sorting (pp ) We already know that Java has a sort method: import java.util.*; public class Lecture22S30 { public static void main(string[] args) { int[] numbers = {79, 17, -3, 2, 8, 58, 12, 44, 29; System.out.println("Original array: " + Arrays.toString(numbers)); Arrays.sort(numbers); System.out.println(" Sortedl array: " + Arrays.toString(numbers)); int index = Arrays.binarySearch(numbers, 29); System.out.println("29 is found at index " + index); the sorted array But we do not know the index in the original unsorted array The binary search finds the index in 33

34 The Arrays class Class Arrays in java.util has many useful array methods: Method name binarysearch(array, value) binarysearch(array, minindex, maxindex, value) copyof(array, length) equals(array1, array2) fill(array, value) sort(array) tostring(array) Description returns the index of the given value in a sorted array (or < 0 if not found) returns index of given value in a sorted array between indexes min /max - 1 (< 0 if not found) returns a new resized copy of an array returns true if the two arrays contain same elements in the same order sets every element to the given value arranges the elements into sorted order returns a string representing the array, such as "[10, 30, -25, 17]" Syntax: Arrays.methodName(parameters) 34

35 Arrays.binarySearch // searches an entire sorted array for a given value // returns its index if found; a negative number if not found // Precondition: array is sorted Arrays.binarySearch(array, value) // searches given portion of a sorted array for a given value // examines minindex (inclusive) through maxindex (exclusive) // returns its index if found; a negative number if not found // Precondition: array is sorted Arrays.binarySearch(array, minindex, maxindex, value) The binarysearch method in the Arrays class searches an array very efficiently if the array is sorted. You can search the entire array, or just a range of indexes (useful for "unfilled" arrays such as the one in ArrayIntList) If the array is not sorted, you may need to sort it first 35

36 Using binarysearch // index int[] a = {-4, 2, 7, 9, 15, 19, 25, 28, 30, 36, 42, 50, 56, 68, 85, 92; int index = Arrays.binarySearch(a, 0, 16, 42); // index1 is 10 int index2 = Arrays.binarySearch(a, 0, 16, 21); // index2 is -7 binarysearch returns the index where the value is found if the value is not found, binarysearch returns: -(insertionpoint + 1) where insertionpoint is the index where the element would have been, if it had been in the array in sorted order. To insert the value into the array, negate insertionpoint + 1 int indextoinsert21 = -(index2 + 1); // 6 36

37 Binary search code // Returns the index of an occurrence of target in a, // or a negative number if the target is not found. // Precondition: elements of a are in sorted order public static int binarysearch(int[] a, int target) { int min = 0; int max = a.length - 1; while (min <= max) { int mid = (min + max) / 2; if (a[mid] < target) { min = mid + 1; else if (a[mid] > target) { max = mid - 1; else { return mid; return -(min + 1); Algorithm proceeds until min is greater than max Calculate new middle // target found // target not found If the target is above the middle, change minimum to one more than middle. If the target is below the middle, change maximum to one less than middle Target found return middle Target not found, return insertion point (location where target should be) 37

38 Introduction (pp ) Binary Search(pp ) Sorting (pp ) Shuffling (pp ) Custom Ordering with Comparators (pp ) 13.2 Program Complexity Introduction (pp ) Empirical Analysis (pp ) Complexity Classes (pp ) 38

39 Introduction (pp ) Binary Search(pp ) Sorting (pp ) Shuffling (pp ) 39

40 Shuffling (pp ) The task of shuffling data, or rearranging the elements into a random order, is perhaps the opposite of sorting. Why would one want to do this? One application for shuffling is a card game program. You might have a card deck stored as a list of Card objects. If the cards are in a predictable order, the game will be boring. You d like to shuffle the deck of cards, rearranging them into a random ordering each time. This is a case in which chaos is preferable to order. 40

41 Shuffling (pp ) Another application is a situation in which you want a random permutation of a list of numbers. You can acquire a random permutation of the numbers from 1 through 5, for example, by storing those numbers into a list and shuffling the list. The Collections class has a method called shuffle that accepts a list as its parameter and rearranges its elements randomly. 41

42 Shuffling (pp ) import java.util.*; public class Lecture22S42 { public static void main(string[] args) { String[] ranks = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King", "Ace"; String[] suits = {"Clubs", "Diamonds", "Hearts", "Spades"; List<String> deck = new ArrayList<String>(); for (String rank : ranks) { // build sorted deck for (String suit : suits) { deck.add(rank + " of " + suit); Collections.shuffle(deck); System.out.println("Top card = " + deck.get(0)); Specify a string array with the desired order for the ranks Specify a string array with the desired order for the suits Create ArrayList object (See Chapter 10) Build deck sorted by rank and suit Shuffle the deck Display the top card 42

43 Shuffling (pp ) import java.util.*; public class Lecture22S42 { public static void main(string[] args) { String[] ranks = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King", "Ace"; String[] suits = {"Clubs", "Diamonds", "Hearts", Each "Spades"; time you run the List<String> deck = new ArrayList<String>(); for (String rank : ranks) { // build sorted deck program, a new card will for (String suit : suits) { appear at random deck.add(rank + " of " + suit); Collections.shuffle(deck); System.out.println("Top card = " + deck.get(0)); 43

44 Shuffling (pp ) import java.util.*; public class Lecture22S42 { public static void main(string[] args) { String[] ranks = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King", "Ace"; String[] suits = {"Clubs", "Diamonds", "Hearts", "Spades"; List<String> deck = new ArrayList<String>(); for (String rank : ranks) { // build sorted deck for (String suit : suits) { deck.add(rank + " of " + suit); Collections.shuffle(deck); System.out.println("Top card = " + deck.get(0)); 44

45 Introduction (pp ) Binary Search(pp ) Sorting (pp ) Shuffling (pp ) Custom Ordering with Comparators (pp ) 13.2 Program Complexity Introduction (pp ) Empirical Analysis (pp ) Complexity Classes (pp ) 45

46 Introduction (pp ) Binary Search(pp ) Sorting (pp ) Shuffling (pp ) Custom Ordering with Comparators (pp ) 46

47 Custom Ordering with Comparators (pp ) Recall the problem of sorting strings in alphabetical order: String[] strings = {"Foxtrot", "alpha", "echo", "golf", "bravo", "hotel", "Charlie", "DELTA"; Arrays.sort(strings); System.out.println(Arrays.toString(strings)); 47

48 Custom Ordering with Comparators (pp ) Recall the problem of sorting strings in alphabetical order: String[] strings = {"Foxtrot", "alpha", "echo", "golf", "bravo", "hotel", "Charlie", "DELTA"; Arrays.sort(strings); System.out.println(Arrays.toString(strings)); In situations such as these, you can define your own external comparison functions with an object called a comparator. 48

49 Custom Ordering with Comparators (pp ) Use of comparator to do case insensitive sort: // sort Strings using case-insensitive Comparator String[] strings = {"Foxtrot", "alpha", "echo", "golf", "bravo", "hotel", "Charlie", "DELTA"; Arrays.sort(strings, String.CASE_INSENSITIVE_ORDER); System.out.println(Arrays.toString(strings)); 49

50 Binary search and objects Can we binarysearch an array of Strings? Operators like < and > do not work with String objects. But we do think of strings as having an alphabetical ordering. natural ordering: Rules governing the relative placement of all values of a given type. comparison function: Code that, when given two values A and B of a given type, decides their relative ordering: A < B, A == B, A > B 50

51 The compareto method (10.2) The standard way for a Java class to define a comparison function for its objects is to define a compareto method. Example: in the String class, there is a method: public int compareto(string other) A call of A.compareTo(B) will return: a value < 0 if A comes "before" B in the ordering, a value > 0 if A comes "after" B in the ordering, or 0 if A and B are considered "equal" in the ordering. 51

52 Introduction (pp ) Binary Search(pp ) Sorting (pp ) Shuffling (pp ) Custom Ordering with Comparators (pp ) 13.2 Program Complexity Introduction (pp ) Empirical Analysis (pp ) Complexity Classes (pp ) 52

53 Introduction (pp ) Binary Search(pp ) Sorting (pp ) Shuffling (pp ) Custom Ordering with Comparators (pp ) 13.2 Program Complexity Introduction (pp ) 53

54 13.2 Program Complexity Introduction (pp ) Runtime complexity is a measure of the computer resources used by running the code Empirical analysis consists of running the code to see how long it takes Algorithm analysis looks at the code itself and mathematically calculates the efficiency 54

55 Runtime Efficiency (13.2) efficiency: A measure of the use of computing resources by code. can be relative to speed (time), memory (space), etc. most commonly refers to run time Assume the following: Any single Java statement takes the same amount of time to run. A method call's runtime is measured by the total of the statements inside the method's body. A loop's runtime, if the loop repeats N times, is N times the runtime of the statements in its body. 55

56 Efficiency examples statement1; statement2; statement3; 3 for (int i = 1; i <= N; i++) { statement4; for (int i = 1; i <= N; i++) { statement5; statement6; statement7; N 3N 4N

57 Efficiency examples 2 for (int i = 1; i <= N; i++) { for (int j = 1; j <= N; j++) { statement1; for (int i = 1; i <= N; i++) { statement2; statement3; statement4; statement5; N 2 4N N 2 + 4N How many statements will execute if N = 10? If N = 1000? 57

58 Algorithm growth rates (13.2) We measure runtime in proportion to the input data size, N. growth rate: Change in runtime as N changes. Say an algorithm runs 0.4N N 2 + 8N + 17 statements. Consider the runtime when N is extremely large. We ignore constants like 25 because they are tiny next to N. The highest-order term (N 3 ) dominates the overall runtime. We say that this algorithm runs "on the order of" N 3. or O(N 3 ) for short ("Big-Oh of N cubed") 58

59 Complexity classes complexity class: A category of algorithm efficiency based on the algorithm's relationship to the input size N. Class Big-Oh If you double N,... Example constant O(1) unchanged 10ms logarithmic O(log 2 N) increases slightly 175ms linear O(N) doubles 3.2 sec log-linear O(N log 2 N) slightly more than doubles 6 sec quadratic O(N 2 ) quadruples 1 min 42 sec cubic O(N 3 ) multiplies by 8 55 min exponential O(2 N ) multiplies drastically 5 * years 59

60 Collection efficiency Efficiency of various operations on different collections: Method ArrayList SortedIntList Stack Queue add (or push) O(1) O(N) O(1) O(1) add(index, value) O(N) - - indexof O(N) O(?) - - get O(1) O(1) - - remove O(N) O(N) O(1) O(1) set O(1) O(1) - - size O(1) O(1) O(1) O(1) 60

61 Binary search (13.1, 13.3) binary search successively eliminates half of the elements. Algorithm: Examine the middle element of the array. If it is too big, eliminate the right half of the array and repeat. If it is too small, eliminate the left half of the array and repeat. Else it is the value we're searching for, so stop. Which indexes does the algorithm examine to find value 22? What is the runtime complexity class of binary search? index value

62 Binary search runtime For an array of size N, it eliminates ½ until 1 element remains. N, N/2, N/4, N/8,..., 4, 2, 1 How many divisions does it take? Think of it from the other direction: How many times do I have to multiply by 2 to reach N? 1, 2, 4, 8,..., N/4, N/2, N Call this number of multiplications "x". 2 x = N x = log 2 N Binary search is in the logarithmic complexity class. 62

63 Range algorithm What complexity class is this algorithm? Can it be improved? // returns the range of values in the given array; // the difference between elements furthest apart // example: range({17, 29, 11, 4, 20, 8) is 25 public static int range(int[] numbers) { int maxdiff = 0; // look at each pair of values for (int i = 0; i < numbers.length; i++) { for (int j = 0; j < numbers.length; j++) { int diff = Math.abs(numbers[j] numbers[i]); if (diff > maxdiff) { maxdiff = diff; return diff; 63

64 Range algorithm 2 The algorithm is O(N 2 ). A slightly better version: // returns the range of values in the given array; // the difference between elements furthest apart // example: range({17, 29, 11, 4, 20, 8) is 25 public static int range(int[] numbers) { int maxdiff = 0; // look at each pair of values for (int i = 0; i < numbers.length; i++) { for (int j = i + 1; j < numbers.length; j++) { int diff = Math.abs(numbers[j] numbers[i]); if (diff > maxdiff) { maxdiff = diff; return diff; 64

65 Range algorithm 3 This final version is O(N). It runs MUCH faster: // returns the range of values in the given array; // example: range({17, 29, 11, 4, 20, 8) is 25 public static int range(int[] numbers) { int max = numbers[0]; // find max/min values int min = max; for (int i = 1; i < numbers.length; i++) { if (numbers[i] < min) { min = numbers[i]; if (numbers[i] > max) { max = numbers[i]; return max - min; 65

66 Runtime of first 2 versions Version 1: Version 2: 66

67 Runtime of 3rd version Version 3: 67

68 Assignments for this week 1. Laboratory for Chapter 7 due today Monday 11/17 IMPORTANT: When you me your laboratory Word Document, be sure it is all in one file 2. Read pp (Chapter 13) for Wednesday 11/19 3. Be sure to complete Quiz 22 before leaving class tonight This is another program to write You must demonstrate the program to me before you leave lab 68

Building Java Programs Chapter 13

Building Java Programs Chapter 13 Building Java Programs Chapter 13 Searching and Sorting Copyright (c) Pearson 2013. All rights reserved. Sequential search sequential search: Locates a target value in an array/list by examining each element

More information

Building Java Programs

Building Java Programs Building Java Programs Chapter 13 binary search and complexity reading: 13.1-13.2 2 Wednesday Questions Are ListNodes used? Yes! In Java s LinkedList What does the Stack tostring represent? bottom [1,

More information

Searching and Sorting

Searching and Sorting Searching and Sorting Sequential search sequential search: Locates a target value in an array/list by examining each element from start to finish. How many elements will it need to examine? Example: Searching

More information

Sum this up for me. Let s write a method to calculate the sum from 1 to some n. Gauss also has a way of solving this. Which one is more efficient?

Sum this up for me. Let s write a method to calculate the sum from 1 to some n. Gauss also has a way of solving this. Which one is more efficient? Sum this up for me Let s write a method to calculate the sum from 1 to some n public static int sum1(int n) { int sum = 0; for (int i = 1; i

More information

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand COSC 236 Web Site You will always find the course material at: http://www.class-notes.us or http://www.class-notes.info or http://www.lecture-notes.tripod.com

More information

CSE 143 Lecture 2. reading:

CSE 143 Lecture 2. reading: CSE 143 Lecture 2 Implementing ArrayIntList reading: 15.1-15.3 slides adapted from Marty Stepp, Hélène Martin, Ethan Apter and Benson Limketkai http://www.cs.washington.edu/143/ Exercise Pretend for a

More information

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand COSC 236 Web Site You will always find the course material at: http://www.class-notes.us or http://www.class-notes.info or http://www.lecture-notes.tripod.com

More information

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand COSC 236 Web Site You will always find the course material at: http://www.class-notes.us or http://www.class-notes.info or http://www.lecture-notes.tripod.com

More information

CSE 143 Lecture 4. Implementing ArrayIntList; Binary Search. reading:

CSE 143 Lecture 4. Implementing ArrayIntList; Binary Search. reading: CSE 143 Lecture 4 Implementing ArrayIntList; Binary Search reading: 15.1-15.3 slides adapted from Marty Stepp and Hélène Martin http://www.cs.washington.edu/143/ Exercise Let's write a class that implements

More information

Building Java Programs

Building Java Programs Building Java Programs Chapter 7 Arrays reading: 7.1 2 Can we solve this problem? Consider the following program (input underlined): How many days' temperatures? 7 Day 1's high temp: 45 Day 2's high temp:

More information

Topic 21 arrays - part 1

Topic 21 arrays - part 1 Topic 21 arrays - part 1 "Should array indices start at 0 or 1? My compromise of 0.5 was rejected without, I thought, proper consideration. " - Stan Kelly-Bootle Copyright Pearson Education, 2010 Based

More information

arrays - part 1 My methods are really methods of working and thinking; this is why they have crept in everywhere anonymously. Emmy Noether, Ph.D.

arrays - part 1 My methods are really methods of working and thinking; this is why they have crept in everywhere anonymously. Emmy Noether, Ph.D. Topic 21 https://commons.wikimedia.org/w/index.php?curid=66702 arrays - part 1 My methods are really methods of working and thinking; this is why they have crept in everywhere anonymously. Emmy Noether,

More information

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand COSC 236 Web Site You will always find the course material at: http://www.class-notes.us or http://www.class-notes.info or http://www.lecture-notes.tripod.com

More information

AP Computer Science. Arrays. Copyright 2010 by Pearson Education

AP Computer Science. Arrays. Copyright 2010 by Pearson Education AP Computer Science Arrays Can we solve this problem? Consider the following program (input underlined): How many days' temperatures? 7 Day 1's high temp: 45 Day 2's high temp: 44 Day 3's high temp: 39

More information

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand COSC 236 Web Site You will always find the course material at: http://www.class-notes.us or http://www.class-notes.info or http://www.lecture-notes.tripod.com

More information

CSc 110, Spring 2017 Lecture 39: searching

CSc 110, Spring 2017 Lecture 39: searching CSc 110, Spring 2017 Lecture 39: searching 1 Sequential search sequential search: Locates a target value in a list (may not be sorted) by examining each element from start to finish. Also known as linear

More information

CSCI 136 Data Structures & Advanced Programming. Lecture 12 Fall 2018 Profs Bill & Jon

CSCI 136 Data Structures & Advanced Programming. Lecture 12 Fall 2018 Profs Bill & Jon CSCI 136 Data Structures & Advanced Programming Lecture 12 Fall 2018 Profs Bill & Jon Last Time Assertions SLL Improvements Tail pointers Circularly Linked Lists Doubly Linked Lists Practice with recursion

More information

Algorithmic Complexity

Algorithmic Complexity Algorithmic Complexity Algorithmic Complexity "Algorithmic Complexity", also called "Running Time" or "Order of Growth", refers to the number of steps a program takes as a function of the size of its inputs.

More information

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand COSC 236 Web Site You will always find the course material at: http://www.class-notes.us or http://www.class-notes.info or http://www.lecture-notes.tripod.com

More information

3/20/2015. Chapter 19 Sorting and Searching SELECTION SORT INSERTION SORT MERGE SORT THE QUICKSORT ALGORITHM MERGE SORT VS SELECTION SORT

3/20/2015. Chapter 19 Sorting and Searching SELECTION SORT INSERTION SORT MERGE SORT THE QUICKSORT ALGORITHM MERGE SORT VS SELECTION SORT Chapter 19 Sorting and Searching The Plan For Today APCS Account Chapter 18 Assignment Towers of Hanoi Chapter 19 19.6: Searching 19.7: Binary Search 19.8: Sorting Real Data Ch18/19 Work Time SELECTION

More information

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand COSC 236 Web Site You will always find the course material at: http://www.class-notes.us From this site you can click on the COSC-236

More information

Arrays. Weather Problem Array Declaration Accessing Elements Arrays and for Loops Array length field Quick Array Initialization Array Traversals

Arrays. Weather Problem Array Declaration Accessing Elements Arrays and for Loops Array length field Quick Array Initialization Array Traversals Arrays Weather Problem Array Declaration Accessing Elements Arrays and for Loops Array length field Quick Array Initialization Array Traversals Can we solve this problem? Consider the following program

More information

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand COSC 236 Web Site You will always find the course material at: http://www.class-notes.us or http://www.class-notes.info or http://www.lecture-notes.tripod.com

More information

Lecture 15: Algorithms. AP Computer Science Principles

Lecture 15: Algorithms. AP Computer Science Principles Lecture 15: Algorithms AP Computer Science Principles Algorithm algorithm: precise sequence of instructions to solve a computational problem. Search for a name in a phone s contact list. Sort emails by

More information

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand COSC 236 Web Site I have decided to keep this site for the whole semester I still hope to have blackboard up and running, but you

More information

Lecture 2: Implementing ADTs

Lecture 2: Implementing ADTs Lecture 2: Implementing ADTs Data Structures and Algorithms CSE 373 SP 18 - KASEY CHAMPION 1 Warm Up Discuss with your neighbors! From last lecture: - What is an ADT? - What is a data structure? From CSE

More information

CS 231 Data Structures and Algorithms Fall Algorithm Analysis Lecture 16 October 10, Prof. Zadia Codabux

CS 231 Data Structures and Algorithms Fall Algorithm Analysis Lecture 16 October 10, Prof. Zadia Codabux CS 231 Data Structures and Algorithms Fall 2018 Algorithm Analysis Lecture 16 October 10, 2018 Prof. Zadia Codabux 1 Agenda Algorithm Analysis 2 Administrative No quiz this week 3 Algorithm Analysis 4

More information

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand COSC 236 Web Site You will always find the course material at: http://www.class-notes.us or http://www.class-notes.info or http://www.lecture-notes.tripod.com

More information

Outline. runtime of programs algorithm efficiency Big-O notation List interface Array lists

Outline. runtime of programs algorithm efficiency Big-O notation List interface Array lists Outline runtime of programs algorithm efficiency Big-O notation List interface Array lists Runtime of Programs compare the following two program fragments: int result = 1; int result = 1; for (int i=2;

More information

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand COSC 236 Web Site You will always find the course material at: http://www.class-notes.us From this site you can click on the COSC-236

More information

Chapter 8 Search and Sort

Chapter 8 Search and Sort Chapter 8 Search and Sort Goals This chapter begins by showing two algorithms used with arrays: selection sort and binary search. After studying this chapter, you will be able to understand how binary

More information

CS 310: Order Notation (aka Big-O and friends)

CS 310: Order Notation (aka Big-O and friends) CS 310: Order Notation (aka Big-O and friends) Chris Kauffman Week 1-2 Logistics At Home Read Weiss Ch 1-4: Java Review Read Weiss Ch 5: Big-O Get your java environment set up Compile/Run code for Max

More information

Building Java Programs Chapter 7

Building Java Programs Chapter 7 Building Java Programs Chapter 7 Arrays Copyright (c) Pearson 2013. All rights reserved. Can we solve this problem? Consider the following program (input underlined): How many days' temperatures? 7 Day

More information

Building Java Programs

Building Java Programs Building Java Programs Comparable, Search reading: 10.2, 13.1 13.3 2 Binary search (13.1) binary search: Locates a target value in a sorted array/list by successively eliminating half of the array from

More information

CSE 143 Lecture 16 (B)

CSE 143 Lecture 16 (B) CSE 143 Lecture 16 (B) Sorting reading: 13.1, 13.3-13.4 slides created by Marty Stepp http://www.cs.washington.edu/143/ Sorting sorting: Rearranging the values in an array or collection into a specific

More information

Building Java Programs

Building Java Programs Building Java Programs Chapter 15 testing ArrayIntList; pre/post conditions and exceptions reading: 4.4 15.1-15.3 2 Searching methods Implement the following methods: indexof returns first index of element,

More information

"42 million of anything is a lot." -Doug Burger (commenting on the number of transistors in the Pentium IV processor)

42 million of anything is a lot. -Doug Burger (commenting on the number of transistors in the Pentium IV processor) Topic 20 Arrays part 2 "42 million of anything is a lot." -Doug Burger (commenting on the number of transistors in the Pentium IV processor) Based on slides for Building Java Programs by Reges/Stepp, found

More information

Lecture 1: Welcome! CSE 373: Data Structures and Algorithms CSE WI - KASEY CHAMPION 1

Lecture 1: Welcome! CSE 373: Data Structures and Algorithms CSE WI - KASEY CHAMPION 1 Lecture 1: Welcome! CSE 373: Data Structures and Algorithms 1 Agenda -Introductions -Syllabus -Dust off data structure cob webs -Meet the ADT -What is complexity? 2 Waitlist/ Overloads -There are no overloads

More information

CSE 143 Lecture 22. Sorting. reading: 13.1, slides adapted from Marty Stepp and Hélène Martin

CSE 143 Lecture 22. Sorting. reading: 13.1, slides adapted from Marty Stepp and Hélène Martin CSE 143 Lecture 22 Sorting reading: 13.1, 13.3-13.4 slides adapted from Marty Stepp and Hélène Martin http://www.cs.washington.edu/143/ Sorting sorting: Rearranging the values in an array or collection

More information

CSE 143. Lecture 13: Interfaces, Comparable reading: , 16.4, 10.2

CSE 143. Lecture 13: Interfaces, Comparable reading: , 16.4, 10.2 CSE 143 Lecture 13: Interfaces, Comparable reading: 9.5-9.6, 16.4, 10.2 Related classes Consider classes for shapes with common features: Circle (defined by radius r ): area = r 2, perimeter = 2 r Rectangle

More information

Building Java Programs. Interfaces and Comparable reading: , 10.2, 16.4

Building Java Programs. Interfaces and Comparable reading: , 10.2, 16.4 Building Java Programs Interfaces and Comparable reading: 9.5-9.6, 10.2, 16.4 2 Shapes Consider the task of writing classes to represent 2D shapes such as Circle, Rectangle, and Triangle. Certain operations

More information

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand COSC 236 Web Site You will always find the course material at: http://www.class-notes.us From this site you can click on the COSC-236

More information

Garfield AP CS. User Input, If/Else. Most slides from Building Java Programs. Thanks, Stuart Regesand Marty Stepp!

Garfield AP CS. User Input, If/Else. Most slides from Building Java Programs. Thanks, Stuart Regesand Marty Stepp! Garfield AP CS User Input, If/Else Most slides from Building Java Programs. Thanks, Stuart Regesand Marty Stepp! Warmup Write a method add10 that takes one integer parameter. Your method should return

More information

CSE 143 Lecture 1. Arrays (review) slides created by Marty Stepp

CSE 143 Lecture 1. Arrays (review) slides created by Marty Stepp CSE 143 Lecture 1 Arrays (review) slides created by Marty Stepp http://www.cs.washington.edu/143/ Arrays (7.1) array: An object that stores many values of the same type. element: One value in an array.

More information

CSE 143 Lecture 14. Sorting

CSE 143 Lecture 14. Sorting CSE 143 Lecture 14 Sorting slides created by Marty Stepp and Ethan Apter http://www.cs.washington.edu/143/ Sorting sorting: Rearranging the values in an array or collection into a specific order (usually

More information

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand COSC 236 Web Site You will always find the course material at: http://www.class-notes.us or http://www.class-notes.info or http://www.lecture-notes.tripod.com

More information

COMP-202 Unit 7: More Advanced OOP. CONTENTS: ArrayList HashSet (Optional) HashMap (Optional)

COMP-202 Unit 7: More Advanced OOP. CONTENTS: ArrayList HashSet (Optional) HashMap (Optional) COMP-202 Unit 7: More Advanced OOP CONTENTS: ArrayList HashSet (Optional) HashMap (Optional) Managing a big project Many times, you will need to use an Object type that someone else has created. For example,

More information

4/27/2012. Chapter Fourteen: Sorting and Searching. Chapter Goals

4/27/2012. Chapter Fourteen: Sorting and Searching. Chapter Goals Chapter Fourteen: Sorting and Searching Chapter Goals To study several sorting and searching algorithms To appreciate that algorithms for the same task can differ widely in performance To understand the

More information

Module 7: Arrays (Single Dimensional)

Module 7: Arrays (Single Dimensional) Module 7: Arrays (Single Dimensional) Objectives To describe why arrays are necessary in programming ( 7.1). To declare array reference variables and create arrays ( 7.2.1 7.2.2). To obtain array size

More information

LECTURE 08 SEARCHING AND SORTING ARRAYS

LECTURE 08 SEARCHING AND SORTING ARRAYS PowerPoint Slides adapted from *Starting Out with C++: From Control Structures through Objects, 7/E* by *Tony Gaddis* Copyright 2012 Pearson Education Inc. COMPUTER PROGRAMMING LECTURE 08 SEARCHING AND

More information

Searching, Sorting. Arizona State University 1

Searching, Sorting. Arizona State University 1 Searching, Sorting CSE100 Principles of Programming with C++, Fall 2018 (based off Chapter 9 slides by Pearson) Ryan Dougherty Arizona State University http://www.public.asu.edu/~redoughe/ Arizona State

More information

Topic 12 more if/else, cumulative algorithms, printf

Topic 12 more if/else, cumulative algorithms, printf Topic 12 more if/else, cumulative algorithms, printf "We flew down weekly to meet with IBM, but they thought the way to measure software was the amount of code we wrote, when really the better the software,

More information

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand COSC 236 Web Site You will always find the course material at: http://www.class-notes.us From this site you can click on the COSC-236

More information

Cosc 241 Programming and Problem Solving Lecture 9 (26/3/18) Collections and ADTs

Cosc 241 Programming and Problem Solving Lecture 9 (26/3/18) Collections and ADTs 1 Cosc 241 Programming and Problem Solving Lecture 9 (26/3/18) Collections and ADTs Michael Albert michael.albert@cs.otago.ac.nz Keywords: abstract data type, collection, generic class type, stack 2 Collections

More information

Recursion. Let s start by looking at some problems that are nicely solved using recursion. First, let s look at generating The Fibonacci series.

Recursion. Let s start by looking at some problems that are nicely solved using recursion. First, let s look at generating The Fibonacci series. Recursion The programs we have discussed so far have been primarily iterative and procedural. Code calls other methods in a hierarchical manner. For some problems, it is very useful to have the methods

More information

ECE 122. Engineering Problem Solving Using Java

ECE 122. Engineering Problem Solving Using Java ECE 122 Engineering Problem Solving Using Java Lecture 27 Linear and Binary Search Overview Problem: How can I efficiently locate data within a data structure Searching for data is a fundamental function

More information

1 Short Answer (5 Points Each)

1 Short Answer (5 Points Each) COSC 117 Exam #1 Solutions Fall 015 1 Short Answer (5 Points Each) 1. What is the difference between a compiler and an interpreter? Also, discuss Java s method. A compiler will take a program written in

More information

Chapter 4: Control Structures I

Chapter 4: Control Structures I Chapter 4: Control Structures I Java Programming: From Problem Analysis to Program Design, Second Edition Chapter Objectives Learn about control structures. Examine relational and logical operators. Explore

More information

Building Java Programs

Building Java Programs Building Java Programs Chapter 4 Lecture 4-1: if and if/else Statements reading: 4.2 self-check: #4-5, 7, 10, 11 exercises: #7 videos: Ch. 4 #2-4 The if/else statement Executes one block if a test is true,

More information

Programming II (CS300)

Programming II (CS300) 1 Programming II (CS300) Chapter 10 Recursion and Search MOUNA KACEM Recursion: General Overview 2 Recursion in Algorithms Recursion is the use of recursive algorithms to solve a problem A recursive algorithm

More information

1 class Lecture5 { 2 3 "Arrays" 4. Zheng-Liang Lu Java Programming 136 / 174

1 class Lecture5 { 2 3 Arrays 4. Zheng-Liang Lu Java Programming 136 / 174 1 class Lecture5 { 2 3 "Arrays" 4 5 } Zheng-Liang Lu Java Programming 136 / 174 Arrays An array stores a large collection of data which is of the same type. 2 // assume the size variable exists above 3

More information

CSE 373 APRIL 3 RD ALGORITHM ANALYSIS

CSE 373 APRIL 3 RD ALGORITHM ANALYSIS CSE 373 APRIL 3 RD ALGORITHM ANALYSIS ASSORTED MINUTIAE HW1P1 due tonight at midnight HW1P2 due Friday at midnight HW2 out tonight Second Java review session: Friday 10:30 ARC 147 TODAY S SCHEDULE Algorithm

More information

Building Java Programs

Building Java Programs Building Java Programs Chapter 4 Lecture 4-1: if and if/else Statements reading: 4.2 self-check: #4-5, 7, 10, 11 exercises: #7 videos: Ch. 4 #2-4 Loops with if/else if/else statements can be used with

More information

36. Collections. Java. Summer 2008 Instructor: Dr. Masoud Yaghini

36. Collections. Java. Summer 2008 Instructor: Dr. Masoud Yaghini 36. Collections Java Summer 2008 Instructor: Dr. Masoud Yaghini Outline Introduction Arrays Class Interface Collection and Class Collections ArrayList Class Generics LinkedList Class Collections Algorithms

More information

Opening Problem EXAMPLE. 1. Read one hundred numbers, 2. compute their average, and 3. find out how many numbers are above the average.

Opening Problem EXAMPLE. 1. Read one hundred numbers, 2. compute their average, and 3. find out how many numbers are above the average. Chapter 6 Arrays 1 Opening Problem EXAMPLE 1. Read one hundred numbers, 2. compute their average, and 3. find out how many numbers are above the average. 2 Introducing Arrays Array is a data structure

More information

COSC 243. Data Representation 3. Lecture 3 - Data Representation 3 1. COSC 243 (Computer Architecture)

COSC 243. Data Representation 3. Lecture 3 - Data Representation 3 1. COSC 243 (Computer Architecture) COSC 243 Data Representation 3 Lecture 3 - Data Representation 3 1 Data Representation Test Material Lectures 1, 2, and 3 Tutorials 1b, 2a, and 2b During Tutorial a Next Week 12 th and 13 th March If you

More information

7. Arrays, More Java Looping

7. Arrays, More Java Looping 7-1 7. Arrays, More Java Looping Review and Preview In the last class, we introduced the idea of looping repeating code blocks. In this class Java lesson, we look at another way to loop (the Java for loop)

More information

Computer Science 210 Data Structures Siena College Fall Topic Notes: Searching and Sorting

Computer Science 210 Data Structures Siena College Fall Topic Notes: Searching and Sorting Computer Science 10 Data Structures Siena College Fall 016 Topic Notes: Searching and Sorting Searching We all know what searching is looking for something. In a computer program, the search could be:

More information

CS 106 Introduction to Computer Science I

CS 106 Introduction to Computer Science I CS 106 Introduction to Computer Science I 06 / 11 / 2015 Instructor: Michael Eckmann Today s Topics Comments and/or Questions? Sorting Searching Michael Eckmann - Skidmore College - CS 106 - Summer 2015

More information

Introduction to Computing II (ITI 1121) Final Examination

Introduction to Computing II (ITI 1121) Final Examination Université d Ottawa Faculté de génie École de science informatique et de génie électrique University of Ottawa Faculty of Engineering School of Electrical Engineering and Computer Science Introduction

More information

Programming 2. Topic 8: Linked Lists, Basic Searching and Sorting

Programming 2. Topic 8: Linked Lists, Basic Searching and Sorting RMIT School of Computer Science and Information Technology Programming 2 Topic 8: Linked Lists, Basic Searching and Sorting Lecture Slides COPYRIGHT 2008 RMIT University. Original content by: Peter Tilmanis,

More information

Lecture 7: Searching and Sorting Algorithms

Lecture 7: Searching and Sorting Algorithms Reading materials Dale, Joyce, Weems:Dale, Joyce, Weems: 10.1-10.5 OpenDSA: 11 (Sorting) and 13 (Searching) Liang (10): 7 (for searching and quadratic sorts), 25 (comprehensive edition only) Contents 1

More information

Principles of Algorithm Analysis. Biostatistics 615/815

Principles of Algorithm Analysis. Biostatistics 615/815 Principles of Algorithm Analysis Biostatistics 615/815 Lecture 3 Snapshot of Incoming Class 25 Programming Languages 20 15 10 5 0 R C/C++ MatLab SAS Java Other Can you describe the QuickSort Algorithm?

More information

8.1. Chapter 8: Introduction to Search Algorithms. Linear Search. Linear Search. Linear Search - Example 8/23/2014. Introduction to Search Algorithms

8.1. Chapter 8: Introduction to Search Algorithms. Linear Search. Linear Search. Linear Search - Example 8/23/2014. Introduction to Search Algorithms Chapter 8: Searching and Sorting Arrays 8.1 Introduction to Search Algorithms Introduction to Search Algorithms Search: locate an item in a list of information Two algorithms we will examine: Linear search

More information

Lecture #8-10 Arrays

Lecture #8-10 Arrays Lecture #8-10 Arrays 1. Array data structure designed to store a fixed-size sequential collection of elements of the same type collection of variables of the same type 2. Array Declarations Creates a Storage

More information

Lecture. Algorithm Design and Recursion. Richard E Sarkis CSC 161: The Art of Programming

Lecture. Algorithm Design and Recursion. Richard E Sarkis CSC 161: The Art of Programming Lecture Algorithm Design and Recursion Richard E Sarkis CSC 161: The Art of Programming Class Administrivia Objectives To understand the basic techniques for analyzing the efficiency of algorithms To know

More information

Objectives of the lesson

Objectives of the lesson Learning Outcome 1) DEMONSTRATE KNOWLEDGE AND UNDERSTANDING OF THE PROCEDURAL APPROACH TO SOFTWARE DEVELOPMENT. Knowledge & Understanding 2) DEVELOP A PROBLEM BASED STRATEGY FOR CREATING AND APPLYING PROGRAMMED

More information

C/C++ Programming Lecture 18 Name:

C/C++ Programming Lecture 18 Name: . The following is the textbook's code for a linear search on an unsorted array. //***************************************************************** // The searchlist function performs a linear search

More information

Sorting & Searching (and a Tower)

Sorting & Searching (and a Tower) Sorting & Searching (and a Tower) Sorting Sorting is the process of arranging a list of items into a particular order There must be some value on which the order is based There are many algorithms for

More information

Searching and Sorting (Savitch, Chapter 7.4)

Searching and Sorting (Savitch, Chapter 7.4) Searching and Sorting (Savitch, Chapter 7.4) TOPICS Algorithms Complexity Binary Search Bubble Sort Insertion Sort Selection Sort What is an algorithm? A finite set of precise instruc6ons for performing

More information

What is an algorithm?

What is an algorithm? /0/ What is an algorithm? Searching and Sorting (Savitch, Chapter 7.) TOPICS Algorithms Complexity Binary Search Bubble Sort Insertion Sort Selection Sort A finite set of precise instrucons for performing

More information

CS/ENGRD 2110 Object-Oriented Programming and Data Structures Spring 2012 Thorsten Joachims. Lecture 10: Asymptotic Complexity and

CS/ENGRD 2110 Object-Oriented Programming and Data Structures Spring 2012 Thorsten Joachims. Lecture 10: Asymptotic Complexity and CS/ENGRD 2110 Object-Oriented Programming and Data Structures Spring 2012 Thorsten Joachims Lecture 10: Asymptotic Complexity and What Makes a Good Algorithm? Suppose you have two possible algorithms or

More information

Outline: Search and Recursion (Ch13)

Outline: Search and Recursion (Ch13) Search and Recursion Michael Mandel Lecture 12 Methods in Computational Linguistics I The City University of New York, Graduate Center https://github.com/ling78100/lectureexamples/blob/master/lecture12final.ipynb

More information

4.1 Performance. Running Time. The Challenge. Scientific Method. Reasons to Analyze Algorithms. Algorithmic Successes

4.1 Performance. Running Time. The Challenge. Scientific Method. Reasons to Analyze Algorithms. Algorithmic Successes Running Time 4.1 Performance As soon as an Analytic Engine exists, it will necessarily guide the future course of the science. Whenever any result is sought by its aid, the question will arise by what

More information

Measuring algorithm efficiency

Measuring algorithm efficiency CMPT 225 Measuring algorithm efficiency Timing Counting Cost functions Cases Best case Average case Worst case Searching Sorting O Notation O notation's mathematical basis O notation classes and notations

More information

Building Java Programs

Building Java Programs Building Java Programs Chapter 4 Lecture 4-1: Scanner; if/else reading: 3.3 3.4, 4.1, 4.5 2 Interactive Programs with Scanner reading: 3.3-3.4 Interactive programs We have written programs that print console

More information

Arrays: An array is a data structure that stores a sequence of values of the same type. The data type can be any of Java s primitive types:

Arrays: An array is a data structure that stores a sequence of values of the same type. The data type can be any of Java s primitive types: Arrays: An array is a data structure that stores a sequence of values of the same type. The data type can be any of Java s primitive types: int, short, byte, long, float, double, boolean, char The data

More information

Introduction to Computers and Programming. Today

Introduction to Computers and Programming. Today Introduction to Computers and Programming Prof. I. K. Lundqvist Lecture 10 April 8 2004 Today How to determine Big-O Compare data structures and algorithms Sorting algorithms 2 How to determine Big-O Partition

More information

Algorithm Efficiency & Sorting. Algorithm efficiency Big-O notation Searching algorithms Sorting algorithms

Algorithm Efficiency & Sorting. Algorithm efficiency Big-O notation Searching algorithms Sorting algorithms Algorithm Efficiency & Sorting Algorithm efficiency Big-O notation Searching algorithms Sorting algorithms Overview Writing programs to solve problem consists of a large number of decisions how to represent

More information

Searching in General

Searching in General Searching in General Searching 1. using linear search on arrays, lists or files 2. using binary search trees 3. using a hash table 4. using binary search in sorted arrays (interval halving method). Data

More information

Announcements. PS 4 is ready, due next Thursday, 9:00pm. Midterm Exam 1: 10/14 (Fri), 9:00am-10:53am

Announcements. PS 4 is ready, due next Thursday, 9:00pm. Midterm Exam 1: 10/14 (Fri), 9:00am-10:53am Announcements PS 4 is ready, due next Thursday, 9:00pm Midterm Exam 1: 10/14 (Fri), 9:00am-10:53am Room TBD Scope: Lecture 1 to Lecture 9 (Chapters 1 to 6 of text) You may bring a sheet of paper (A4, both

More information

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University CS5000: Foundations of Programming Mingon Kang, PhD Computer Science, Kennesaw State University Generating random numbers Obtain a random double value between 0.0 and 1.0, excluding 1.0 Math.random() Random

More information

CS 110 Practice Final Exam originally from Winter, Instructions: closed books, closed notes, open minds, 3 hour time limit.

CS 110 Practice Final Exam originally from Winter, Instructions: closed books, closed notes, open minds, 3 hour time limit. Name CS 110 Practice Final Exam originally from Winter, 2003 Instructions: closed books, closed notes, open minds, 3 hour time limit. There are 4 sections for a total of 49 points. Part I: Basic Concepts,

More information

Programming II (CS300)

Programming II (CS300) 1 Programming II (CS300) Chapter 9 (Part II) Recursion MOUNA KACEM Recursion: General Overview 2 Recursion in Algorithms Recursion is the use of recursive algorithms to solve a problem A recursive algorithm

More information

MPATE-GE 2618: C Programming for Music Technology. Unit 4.2

MPATE-GE 2618: C Programming for Music Technology. Unit 4.2 MPATE-GE 2618: C Programming for Music Technology Unit 4.2 Quiz 1 results (out of 25) Mean: 19.9, (standard deviation = 3.9) Equivalent to 79.1% (SD = 15.6) Median: 21.5 High score: 24 Low score: 13 Pointer

More information

Points off Total off Net Score. CS 314 Final Exam Spring 2016

Points off Total off Net Score. CS 314 Final Exam Spring 2016 Points off 1 2 3 4 5 6 Total off Net Score CS 314 Final Exam Spring 2016 Your Name Your UTEID Instructions: 1. There are 6 questions on this test. 100 points available. Scores will be scaled to 300 points.

More information

CSE 373. Sorting 1: Bogo Sort, Stooge Sort, Bubble Sort reading: Weiss Ch. 7. slides created by Marty Stepp

CSE 373. Sorting 1: Bogo Sort, Stooge Sort, Bubble Sort reading: Weiss Ch. 7. slides created by Marty Stepp CSE 373 Sorting 1: Bogo Sort, Stooge Sort, Bubble Sort reading: Weiss Ch. 7 slides created by Marty Stepp http://www.cs.washington.edu/373/ University of Washington, all rights reserved. 1 Sorting sorting:

More information

Topic 12 more if/else, cumulative algorithms, printf

Topic 12 more if/else, cumulative algorithms, printf Topic 12 more if/else, cumulative algorithms, printf "We flew down weekly to meet with IBM, but they thought the way to measure software was the amount of code we wrote, when really the better the software,

More information

Chapter 7 Single-Dimensional Arrays

Chapter 7 Single-Dimensional Arrays Chapter 7 Single-Dimensional Arrays 1 Arrays Array is a data structure that represents a collection of same-types data elements. A single-dimensional array is one that stores data elements in one row.

More information

Algorithms. Algorithms 1.4 ANALYSIS OF ALGORITHMS

Algorithms. Algorithms 1.4 ANALYSIS OF ALGORITHMS ROBERT SEDGEWICK KEVIN WAYNE Algorithms ROBERT SEDGEWICK KEVIN WAYNE 1.4 ANALYSIS OF ALGORITHMS Algorithms F O U R T H E D I T I O N http://algs4.cs.princeton.edu introduction observations mathematical

More information