CS REG - Unit 7 - PART TWO
CS REG - Unit 7 - PART TWO
• Sequential or Linear search typically starts at the first element in an array or ArrayList and looks
through all the items one by one until it either finds the desired value and then it returns the index it
found the value at or if it searches the entire array or list without finding the value it returns -1.
• Binary search can only be used on data that has been sorted or stored in order. It checks the middle
of the data to see if that middle value is less than, equal, or greater than the desired value and then
based on the results of that it narrows the search. It cuts the search space in half each time.
Sequential search
• sequential search: Locates a target value in an array/list by examining each element from
start to finish. If found, return index of first occurrence. Otherwise, return -1.
i
Search in ArrayList of Double
public int sequentialSearch1(ArrayList<Double> arl, double
target, double delta){
for(int i = 0; i < arl.size(); i++){
if(Math.abs(target – arl.get(i)) < delta)
return i;
}
// target not in array
return -1;
}
Lab: Search in an ArrayList of Books for a String
public class Book{
private String description;
// other instance variables and constructor are skipped
}
Lab: Search in an ArrayList of Books for a String
Sequential search: int in Array
Implement sequential search using arrays which returns the index of the target or -1 if it is not
found.
public int sequentialSearch(int[] array, int target){
for(int i = 0; i < array.length; i++){
if(array[i] == target)
return i;
}
// target not in array
return -1;
}
Enhanced for loop for searching
As long as we are only *checking* (not modifying) for existence, a for-each loop will work fine.
}
Can we make searching faster?
• We can’t make assumptions about the data we are searching, so we
need to check EVERY index (in order) until we either find the data, or
reach the end of the structure.
index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
value -4 2 7 10 15 20 22 25 30 36 42 50 56 68 85 92 103
selection sort: look for the smallest element, swap with first element. Look for the second
smallest, swap with second element, etc…
merge sort: recursively divide the array in half and sort it. Merge sort will be discussed in
Unit 10.
Selection sort
selection sort: Orders a list of values by repeatedly putting the smallest or largest
unplaced value into its final position.
The algorithm:
• Look through the list to find the smallest value.
• Swap it so that it is at index 0.
index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
value 22 18 12 -4 27 30 36 50 7 68 91 56 2 85 42 98 25
index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
value -4 2 12 22 27 30 36 50 7 68 91 56 18 85 42 98 25
index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
value -4 2 7 22 27 30 36 50 12 68 91 56 18 85 42 98 25
1) For an array of n elements, the array is sorted in n – 1 passes.
2) After the kth pass, the first k elements are in their final sorted positions.
public static void selectionSort(int[] elements){
- No, the Selection Sort algorithm needs to know the index of the items it is
working with.
内循环寻找*最小值*
找到了*更小的*,更新minIndex
找到最小值,
把现在位置上的数字和minIndex上的互换
执行完外循环1次,确保了第0个是*最小值*;返回执行第二次外循环 – 假设第1个最小
How many times do we need to swap
- changing the original array?
index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
value -4 12 18 22 27 30 36 50 7 68 91 56 2 85 42 98 25
sorted sub-array (indexes 0-7)
Insertion Sort Algorithm
64 54 58 87 55 54 58 64 87 55
54 less than 64 55 less than 87
Insert 54 before 64 55 less than 64
55 less than 58
54 64 58 87 55 55 greater than 54
58 less than 64 Insert 55 before 58
58 greater than 54
Insert 58 before 64 54 55 58 64 87
54 58 64 87 55
87 greater than 64
Go to next element
public void insertionSort(ArrayList<Integer> list){
for(int i = 1; i < list.size(); i++){
elements[possibleIndex] = temp;
}
} This implementation uses arrays. This code has appeared
on MC questions on the AP exam. Understand it!
How many times did we move possibleIndex forward?
4, 12, 4, 7, 19, 6
4, 12, 4, 7, 19, 6
possibleIndex -- : change possibleIndex at 4 to [1], 4 is at the
correct place, end while loop
4, 4, 12, 7, 19, 6
4, 12, 4, 7, 19, 6
possibleIndex -- : change possibleIndex at 4 to [1], 4 is at the
correct place, end while loop
4, 4, 12, 7, 19, 6
possibleIndex -- : change possibleIndex at 7 to [2], 7 is at the
correct place, end while loop
4, 4, 7, 12, 19, 6
4, 12, 4, 7, 19, 6
possibleIndex -- : change possibleIndex at 4 to [1], 4 is at the
correct place, end while loop
4, 4, 12, 7, 19, 6
possibleIndex -- : change possibleIndex at 7 to [2], 7 is at the
correct place, end while loop
4, 4, 7, 12, 19, 6
4, 12, 4, 7, 19, 6
possibleIndex -- : change possibleIndex at 4 to [1], 4 is at the
correct place, end while loop
4, 4, 12, 7, 19, 6
possibleIndex -- : change possibleIndex at 7 to [2], 7 is at the
correct place, end while loop
4, 4, 7, 12, 19, 6
4, 12, 4, 7, 19, 6
possibleIndex -- : change possibleIndex at 4 to [1], 4 is at the
correct place, end while loop
4, 4, 12, 7, 19, 6
possibleIndex -- : change possibleIndex at 7 to [2], 7 is at the
correct place, end while loop
4, 4, 7, 12, 19, 6
possibleIndex -- : change possibleIndex at 6 (4, 4, 7, 12, 6, 19),
possibleIndex -- : change possibleIndex at 6 (4, 4, 7, 6, 12, 19),
possibleIndex -- : change possibleIndex at 6, 6 is at the correct
place (4, 4, 6, 7, 12, 19), end while loop
4, 4, 6, 7, 12, 19
Questions on insertion sort…
• Why use a while loop as the inner loop?
• As soon as the condition is failed a while loop will not execute again, if a
for loop was chosen a much more complex middle section of the for loop
header is required.
Note: For selection sort, there is no worst or best case. Finding the smallest at each iteration
requires traversing the array to the end.
The Complexity of An Algorithm
The complexity f(n) is defined in terms of the input size n. For example, sorting an array
should take more resources for larger arrays.
int x = 0;
for(int i = 0; i < n; i++){
x++;
}
int x = 0;
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
x++;
}
}
Answer: n^2
Example: Selection and Insertion are both quadratic in complexity.
Complexity of Searching Algorithms
Assume the following algorithms are performed on an array of size n.
Binary Search: This is a bit harder. Since each comparison eliminates half of the array, it takes
approximately 𝑙𝑜𝑔! 𝑛 iterations. For example, if an array has length 32, it takes about5 comparisons
since 2" = 32. We will cover this in U10.
Complexity of Sorting Algorithms
Assume the following algorithms are performed on an array of size n.