Sei sulla pagina 1di 47

Algorithms: Searching & Sorting

Unit 10

How would you search through a shuffled deck for the Queen of Hearts?

How do you look up a number in a phone book?

Algorithms
A sequence of steps to solve a problem

Chocolate Chip Cookie Recipe Driving directions

Finding the min/max


Searching & Sorting

Search A Collection For


The location of a key value

The existence of a key value

Linear Search
Also known as sequential search Search through list from beginning til target value is found or end is reached No specific order of values is required

Max comparisons: N (size of list)

Linear Search
public static int linearSearch(int[] numbers, int key) { for (int x = 0; x < numbers.length; x++) { if (numbers[x] == key) return x; } Exit loop AND method return -1; once match found } If still in method after all elements checked, no match

Binary Search
While target value not found and not done Find middle value of remaining list area
Match? Search is done Target < middle? Continue left of middle Target > middle? Continue right of middle

Keep going til low & public static int binarySearch(int[] highnumbers, cross over int key) { int low = 0; int high = numbers.length - 1; while (low <= high) Find the middle { int middle = (low + high) / 2; Done! if (numbers[middle] == key) return middle; else if (key < numbers[middle]) high = middle - 1; else Go left or right low = middle + 1; } return -1; }

Binary Search

Binary Search
Requires that the list be in order

To calculate max comparisons for list of size N:


Find lowest power of 2 that N is less than That power is the maximum comparisons

Binary Search
Examples of calculating max comparisons:
Max

N = 55

32 (25) <= 55 < 64 (26)

N = 512 512 (29) <= 512 < 1024 (210)


512 is a power of 2, go up one more! Max

Search Comparison
Execution Time

Linear

Binary

Size

Sorting
Arrange elements of a list in order

Promotes more efficient searching


Sorting Algorithms we will study Selection Sort Bubble Sort (not on AP exam)

Insertion Sort

Merge Sort (next unit)

Sort by Increasing Size

Selection Sort
For each location Search list from this location to the end for smallest (or largest if descending) value

Swap into this location

Selection Sort
58 79 58 14 14 23 94 90 66 75
min i min i i i i i i

14 23 79 58 79 23 94 90 66 75 14 23 58 79 94 90 66 75
min i i i i i min min i min i i i i i

Selection Sort
14 23 58 66 79 94 90 79 66 75
14 23 58 66 75 94 90 79 75 94

14 23 58 66 75 79 90 90 79 94
14 23 58 66 75 79 90 94

Selection Sort
Makes N-1 passes (N is size of list)

Makes same number of comparisons even if list is already in order or in reverse order!

N-1 Passes public static void selectionSort(int[] numbers) { int length = numbers.length; for (int start = 0; start < length-1; start++) Find smallest { one remaining int minIndex = start; for (int i = start + 1; i < length; i++) { if (numbers[i] < numbers[minIndex]) minIndex = i; } int temp = numbers[start]; numbers[start] = numbers[minIndex]; numbers[minIndex] = temp; Swap into place } }

Sort Alphabetically
Boston Wings Meat Loaf Styx

Meat Boston Styx Loaf

Wings

Insertion Sort
For each element starting at 2nd element
Keep searching for insertion point while value is smaller than elements in front of it

Insert element at insertion point

Insertion Sort
79
Key

58 79 14 23 94 90 66 75
pos - 1

14
Key

58 58 14 79 79 14 23 94 90 66 75
pos -pos 1 -1

23
Key

14 23 58 58 79 79 23 94 90 66 75
pos -pos 1 -1 pos - 1

Insertion Sort
94
Key

14 23 58 79 94 90 66 75
14 23 58 79 90 94 94 90 66 75 14 23 58 66 79 79 90 90 94 94 66 75 14 23 58 66 75 79 79 90 90 94 75 94

90
Key

66
Key

75
Key

Insertion Sort
Makes N-1 passes (N is size of list) Makes only N-1 comparisons if already in order Max work load if in reverse order

public static void insertionSort(int[] numbers) N-1 Passes { int len = numbers.length; for (int index = 1; index < len; index++) { Search for int key = numbers[index]; insertion point int position = index; while (position > 0 && key < numbers[position-1]) { numbers[position] = numbers[position-1]; position--; } No iterations numbers[position] = key; if key > value } in front of it }

Sort by Increasing Size


Is this pair in increasing order? If not, swap them.

End of Pass 1

Sort by Increasing Size


Is this pair in increasing order? If not, swap them.

End of Pass 2

Sort by Increasing Size


Is this pair in increasing order? If not, swap them.

End of Pass 3

Sort by Increasing Size


Is this pair in increasing order? If not, swap them.

End Max of 4 Passes

Bubble Sort
For each location in the list (except last) and while swaps are being made

Traverse list
Compare element with neighbor If elements are out of order, swap them Next largest value has sunk into place

Bubble Sort
58 14 79 23 14 79 79 23 90 94 66 90 75 94 66 94 94 75

14 58 23 14 58 58 23 79 66 90 75 66 90 90 75 94

14 23 58 66 79 79 66 75 75 79 90 94

Bubble Sort
14 23 58 66 75 79 90 94
No swaps? Were done! Did we make any swaps?

Bubble Sort
At most N-1 passes (N is size of list)

Makes 1 pass if list is already in order

public static void bubbleSort(int[] numbers) Stop when no { swaps made int bottom = numbers.length - 1; or bottom == 0 boolean swapped = true; while (swapped) { swapped = false; for (int i = 0; i < bottom; i++) Swap out of { if (numbers[i] > numbers[i+1]) order neighbors { int temp = numbers[i]; numbers[i] = numbers[i+1]; numbers[i+1] = temp; swapped = true; } Next largest } now in place bottom--; } }

Practice Interactively
On myPISD, go to APCS1 Class Resources
Choose Algorithm Animation Websites

Sorting In-order Arrays


Sort
Selection Insertion Bubble

# of Comparisons Total Passes Per Pass Comparisons n1 n/2 (n 1) (n / 2) n1 1 n1 1 n1 n1

Insert/Delete an Item
ArrayList provides ability to insert into an ordered list and delete from a list with ease Java array does not! Insert into ordered list assuming sufficient space is always successful Delete from list not always successful

Inserting 40 into Sorted Array


40 < 50 Slide 50 over
40 > 35 Insert 40 40 < 65 Slide 65 over 40 < 80 Slide 80 over

20 35 40 50 50 65 65 80 80 0
Move logical end of array

End of End of Array Array

Inserting Into Sorted Java Array


Working backwards from next available index until insertion point found If item to insert is less than element to left, shift element to current location Otherwise, insertion point found Store insertion item at insertion point

Increase number of elements counter

Insertion Solution
public void insert(int value) { int index = numInArray; while (index > 0 && value < numbers[index-1]) { numbers[index] = numbers[index-1]; index--; } numbers[index] = value; numInArray++; Insert the item }
Shift items to right until insertion point found

Deleting 40 From Array


Slide 50 over to take its place Slide 80 over

Find the 40

Slide 65 over

Move logical end of array

80 80 20 35 50 40 65 50 65

End of End of Array Array

Deleting From Java Array


Search for item to delete If found, traverse array from match location to end

Shift each element to left


Reduce number of elements count

Deletion Solution
public boolean delete(int value) Search for value { to delete int index = 0; boolean found = false; while (!found && index < numInArray) { if (numbers[index] == value) found = true; else index++; Slide over remaining } elements to take its place if (found) { for (int k = index; k < numInArray-1; k++) numbers[k] = numbers[k+1]; // shift to left numInArray--; } return found; }

Deletion Solution Search for value


to delete public boolean delete(int value) { int index = Arrays.binarySearch(numbers, 0, numInArray, value); if (index >= 0) { while (index < numInArray - 1) { numbers[index] = numbers[index + 1]; index++; } numInArray--; If not found, method returns return true; [ -(insertion index) 1] } else { return false; } }

Searching/Sorting with Arrays of Objects


Comparing is different Primitives: ==, >, >=, <, <= Objects: equals, compareTo

Searching/Sorting with Arrays of Objects


Compare entire object if Comparable interface implemented (compareTo)
if (list[x].compareTo(list[y]) < 0)

Can also compare object fields


if (list[x].value() < list[y].value())

Object or any Helpful Utility Libraries

java.util.Arrays

primitive type (int, double, etc.)

String toString( ____ [ ] arr)


int binarySearch( ____ [ ] arr, ___ key)

void sort( ____ [ ] arr)


java.util.Collections
int binarySearch(List<Comparable<T>> list, T key)
void sort(List<Comparable<T>> list)

Potrebbero piacerti anche