csci 51 introduction to programming march 12, 2009

30
CSCI 51 Introduction to Programming March 12, 2009

Upload: horace-harrell

Post on 13-Dec-2015

219 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: CSCI 51 Introduction to Programming March 12, 2009

CSCI 51Introduction to Programming

March 12, 2009

Page 2: CSCI 51 Introduction to Programming March 12, 2009

Finding Pairs

0 1 2 3 4

How do you pick which ones are pairs?

Remember, a computer can only compare two things at a time.

Page 3: CSCI 51 Introduction to Programming March 12, 2009

Finding Pairs

0 1 2 3 4ind1 ind2

Does Die at ind1 equal Die at ind2?

0 1-4

1 2-4

2 3-4

3 4

int len = dice.getNumDice();for (int ind1 = 0; ind1<len-1; ind1++) { // save face value of Die at ind1 for (int ind2 = ind1+1; ind<len; ind2++) {

// save face value of Die at ind2// compare face values -- if match -> print

}}

length of array? 5

Page 4: CSCI 51 Introduction to Programming March 12, 2009

Equality

Die die1 = dice.getDie(ind1);Die die2 = dice.getDie(ind2);

if (die1.getFace() == die2.getFace())

if (die1.equals(die2))

public boolean equals (Die otherDie){ if (getFace() == otherDie.getFace()) {

return true; } return false;}

in Die class

Page 5: CSCI 51 Introduction to Programming March 12, 2009

New Stuff

• Searching arrays for a particular value– reference: Ch 11

• Sorting arrays– makes searching for a particular

value easier (and quicker)– reference: Ch 12

Page 6: CSCI 51 Introduction to Programming March 12, 2009

Searching Arrays

• Find one particular element in an array of many elements

• Find several particular elements in an array of many elements

• Complexity (How Long To Search?)– find a parking space - linear– look up a word in a dictionary - complex

• 500K+ words in OED

– search - very complex• over 3 trillion web pages

Page 7: CSCI 51 Introduction to Programming March 12, 2009

Linear Searching

• Given a test value and a list of values– list can be ordered or unordered– loop through the list

• repeatedly ask: Is this a match?• quit when the answer is yes (use break stmt)

– if you finish all items, there is no match

• Inefficient– worst time to search is ~ length– average time to search is ~ length/2

• Relatively easy to program

Page 8: CSCI 51 Introduction to Programming March 12, 2009

// Linear search of unordered list of integers

// unordered listint[] list = {17, 14, 9, 23, 18, 11, 62, 47, 33, 88};

// look for this value in the listint searchFor = 33;

// Loop thru list until we find matchint foundAt = -1; // where found (default)for (int index = 0; index < list.length; index++){ if (list[index] == searchFor) { foundAt = index; break; // jump out of the loop }}

// foundAt is now index of item “searchFor”// or -1 if not found

Page 9: CSCI 51 Introduction to Programming March 12, 2009

// Linear search of unordered list of Strings

// unordered listString[] list = {“Bart”, “Homer”, “Marge”, “Lisa”, “Maggie”, “Millhouse”};

// look for this value in the listString searchFor = “Maggie”;

// Loop thru list until we find matchint foundAt = -1; // where found (default)for (int index = 0; index < list.length; index++){ if (list[index].equals(searchFor)) { foundAt = index; break; // jump out of the loop }}

// foundAt is now index of item ``searchFor’’// or -1 if not found

Page 10: CSCI 51 Introduction to Programming March 12, 2009

Binary Search

• Requires ordered (sorted) list• Set searchRange to the entire list• Repeat:

– pick a “test value” in the middle of searchRange

– if test value == value searching for• Stop!

– if test value > value searching for• searchRange = lower half of searchRange

– if test value < value searching for• searchRange = upper half of searchRange

Page 11: CSCI 51 Introduction to Programming March 12, 2009

Example

2 4 5 12 16 19 22 26 29 32 37 41 46 50

Looking for 46 Trial 1

2

3

2 4 5 12 16 19 22 26 29 32 37 41 46 50

2 4 5 12 16 19 22 26 29 32 37 41 46 50

Page 12: CSCI 51 Introduction to Programming March 12, 2009

Notes on Binary Searches• List must be ordered (sorted)

– can maintain a list in ordered fashion

• Much more efficient than linear– in example, took 3 iterations instead of 13– time ~ log2(listLength) – linear

• worst case ~ listLength• average ~ listLength/2

– for 100K words: 17 iterations versus 50,000

• More complex to program

Page 13: CSCI 51 Introduction to Programming March 12, 2009

SearchingThings To Know

• Be able to recognize and write a linear search

• Understand its pros and cons

• Know the concepts of a Binary Search

Page 14: CSCI 51 Introduction to Programming March 12, 2009

Questions

2 10 17 45 49 55 68 85 92 98

How many comparisons are needed to determine if the following items are in the list of 10 items?

linear search binary searchnumber15

49

98

2

10 3

5 1

10 4

1 3

(49, 10, 17)

(49, 85, 92, 98)(49, 10, 2)

(3, if know list sorted)

Page 15: CSCI 51 Introduction to Programming March 12, 2009

Sorting

• Put elements of an array in some order– alphabetize names– order grades lowest to highest

• Three simple sorting algorithms– selection sort– insertion sort– bubble

Page 16: CSCI 51 Introduction to Programming March 12, 2009

Selection Sort

• Sorts by putting values directly into their final, sorted position

• For each position in the list, the selection sort finds the value that belongs in that position and puts it there

Page 17: CSCI 51 Introduction to Programming March 12, 2009

Selection SortGeneral Algorithm• Scan the list to find the smallest value• Exchange (swap) that value with the

value in the first position in the list• Scan rest of list for the next smallest

value• Exchange that value with the value in

the second position in the list• And so on, until you get to the end of

the list

Page 18: CSCI 51 Introduction to Programming March 12, 2009

Selection SortAt Work

98 68 83 7493

68 98 83 7493

68 74 83 9893

68 74 83 9893

68 74 83 93 98 SORTED!

Page 19: CSCI 51 Introduction to Programming March 12, 2009

Selection Sort

• Sorts in ascending order

• Can be changed to sort in descending order– look for max instead of min

Page 20: CSCI 51 Introduction to Programming March 12, 2009

Insertion Sort

• Like we’d actually sort things

• Insert each new item into an already sorted list

• Each unsorted element is inserted at the appropriate spot in the sorted subset until the list is ordered

Page 21: CSCI 51 Introduction to Programming March 12, 2009

Insertion SortGeneral Algorithm• Sort the first two values (swap, if

necessary)• Repeat:

– insert list’s next value into the appropriate position relative to the first ones (which are already sorted)

• Each time insertion made, number of values in the sorted subset increases by one

• Other values in array shift to make room for inserted elements

Page 22: CSCI 51 Introduction to Programming March 12, 2009

Insertion SortAt Work

98 68 83 7493

68 98 83 7493

68 83 98 7493

68 74 83 98 93

68 74 83 9398

SORTED!

Page 23: CSCI 51 Introduction to Programming March 12, 2009

Insertion Sort

• Outer loop controls the index in the array of the next value to be inserted

• Inner loop compares the current insert value with values stored at lower indexes

• Each iteration of the outer loop adds one more value to the sorted subset of the list, until the entire list is sorted

Page 24: CSCI 51 Introduction to Programming March 12, 2009

Bubble Sort

• "bubble"– largest values bubble to the end– smallest values sink to the beginning

• Idea– go through the list and swap neighboring

items if needed

• Pros– easy to understand and code

• Cons– horribly inefficient (listLength2)

Page 25: CSCI 51 Introduction to Programming March 12, 2009

Bubble SortAt Work

98 68 83 749368 98 83 749368 83 98 749368 83 74 9893

68 74 83 93 98 SORTED!

68 83 74 9398

Page 26: CSCI 51 Introduction to Programming March 12, 2009

Sort Implementations

• All three use double (nested) loops

• Selection and insertion– an outer loops scans all elements– an inner loop scans and switches/inserts as

needed

• Bubble– an outer loop repeats until no swaps are

needed– an inner loops scans and swaps as needed

Page 27: CSCI 51 Introduction to Programming March 12, 2009

SortingThings To Know

• Be able to recognize and follow an insertion sort, selection sort, and bubble sort

• Understand their pros and cons• Know that many other sorts exist

with varying efficiency and programming difficulty

Sorting animations (in Java of course!)http://www.cs.hope.edu/~alganim/animator/Animator.html

Page 28: CSCI 51 Introduction to Programming March 12, 2009

Question

Given the operation of the following sort, identify the type of sort (selection, insertion, or bubble)

34 21 97 15 8721 34 97 15 8721 34 15 97 8721 34 15 87 9721 15 34 87 9715 21 34 87 97

original

pass 1

pass 2

pass 3

pass 4pass 5SORTED

Page 29: CSCI 51 Introduction to Programming March 12, 2009

Question

Given the operation of the following sort, identify the type of sort (selection, insertion, or bubble)

34 21 97 15 8721 34 97 15 8721 34 97 15 8715 21 34 97 8715 21 34 87 97

original

pass 1

pass 2

pass 3

pass 4SORTED

Page 30: CSCI 51 Introduction to Programming March 12, 2009

SortingThings Other Than Numbers

• characters– same as integers (compare with < and >)

• Strings– use the built-in compareTo method

• Other Objects– we write a compareTo method– use the compareTo method