Java Programming: From Problem Analysis to Program Design, 5e
5.
Search (continued) Java Programming: From Problem Analysis to Program Design, 5e
If the search item is the second item in the list, the sequential search makes two key comparisons (also called item comparisons ) to determine whether the search item is in the list
If the search item is the 900th item in the list, the sequential search makes 900 key comparisons to determine whether the search item is in the list
If the search item is not in the list, the sequential search makes 1000 key comparisons
6.
Search (continued) Java Programming: From Problem Analysis to Program Design, 5e
If searchItem is always at the end of the list, it will take many comparisons to find searchItem
If searchItem is not in the list, then we will compare searchItem with every element in the list
A sequential search is therefore not efficient for large lists; in fact, it can be proved that, on average, the number of comparisons made by a sequential search is equal to half the size of the list
List is sorted by selecting list element and moving it to its proper position
Algorithm finds position of smallest element and moves it to top of unsorted portion of list
Repeats process above until entire list is sorted
Java Programming: From Problem Analysis to Program Design, 5e
8.
Selection Sort (continued) Java Programming: From Problem Analysis to Program Design, 5e
9.
Selection Sort (continued) Java Programming: From Problem Analysis to Program Design, 5e
10.
Selection Sort (continued) Java Programming: From Problem Analysis to Program Design, 5e
11.
Selection Sort (continued) Java Programming: From Problem Analysis to Program Design, 5e public static void selectionSort( int [] list, int listLength) { int index; int smallestIndex; int minIndex; int temp; for (index = 0; index < listLength – 1; index++) { smallestIndex = index; for (minIndex = index + 1; minIndex < listLength; minIndex++) if (list[minIndex] < list[smallestIndex]) smallestIndex = minIndex; temp = list[smallestIndex]; list[smallestIndex] = list[index]; list[index] = temp; } }
12.
Java Programming: From Problem Analysis to Program Design, 5e
It is known that for a list of length n , selection sort makes exactly n ( n – 1) / 2 key comparisons and exactly 3( n – 1) item assignments
Therefore, if n = 1000, then to sort the list, selection sort makes 500,000 key comparisons and about 3000 item assignments
Selection Sort (continued)
13.
Insertion Sort Java Programming: From Problem Analysis to Program Design, 5e
The insertion sort algorithm sorts the list by moving each element to its proper place
14.
Insertion Sort (continued) Java Programming: From Problem Analysis to Program Design, 5e
15.
Insertion Sort (continued) Java Programming: From Problem Analysis to Program Design, 5e
16.
Insertion Sort (continued) Java Programming: From Problem Analysis to Program Design, 5e
17.
Insertion Sort (continued) Java Programming: From Problem Analysis to Program Design, 5e public static void insertionSort( int [] list, int listLength) { int firstOutOfOrder, location; int temp; for (firstOutOfOrder = 1; firstOutOfOrder < listLength; firstOutOfOrder++) if (list[firstOutOfOrder] < list[firstOutOfOrder - 1]) { temp = list[firstOutOfOrder]; location = firstOutOfOrder; do { list[location] = list[location - 1]; location--; } while(location > 0 && list[location - 1] > temp); list[location] = temp; } } //end insertionSort
18.
Insertion Sort (continued) Java Programming: From Problem Analysis to Program Design, 5e
It is known that for a list of length n , on average, the insertion sort makes ( n 2 + 3 n – 4) / 4 key comparisons and about n ( n – 1) / 4 item assignments
Therefore, if n = 1000, then to sort the list, the insertion sort makes about 250,000 key comparisons and about 250,000 item assignments
Search item is compared with middle element of list
If search item < middle element of list, search is restricted to first half of the list
If search item > middle element of list, search second half of the list
If search item = middle element, search is complete
Java Programming: From Problem Analysis to Program Design, 5e
20.
Binary Search Algorithm (continued) Java Programming: From Problem Analysis to Program Design, 5e
Determine whether 75 is in the list
21.
Binary Search Algorithm (continued) Java Programming: From Problem Analysis to Program Design, 5e
22.
Java Programming: From Problem Analysis to Program Design, 5e public static int binarySearch( int [] list, int listLength, int searchItem) { int first = 0; int last = listLength - 1; int mid; boolean found = false ; while (first <= last && !found) { mid = (first + last) / 2; if (list[mid] == searchItem) found = true ; else if (list[mid] > searchItem) last = mid - 1; else first = mid + 1; } if (found) return mid; else return –1; } //end binarySearch
23.
Binary Search Algorithm (continued) Java Programming: From Problem Analysis to Program Design, 5e
24.
Binary Search Algorithm (continued) Java Programming: From Problem Analysis to Program Design, 5e
25.
Performance of the Binary Search Java Programming: From Problem Analysis to Program Design, 5e
Suppose that L is a list of size 1048576
Because 1048576 = 220, it follows that the while loop in binary search will have at most 21 iterations to determine whether an element is in L
Every iteration of the while loop makes two key (that is, item) comparisons
To determine whether an element is in L , binary search makes at most 42 item comparisons
On the other hand, on average, a sequential search will make 524,288 key (item) comparisons to determine whether an element is in L
In general, if L is a sorted list of size n , to determine whether an element is in L , the binary search makes at most 2log 2 n + 2 key (item) comparisons
Java Programming: From Problem Analysis to Program Design, 5e
Be the first to comment