|
|
The linear search algorithm for arrays:
/* ---------------------------------------------------- The linear search algorithm to find key in the array list ---------------------------------------------------- */ public static int linearSearch(int[] list, int key) { for ( int i = 0; i < list.length; i++ ) if ( list[i] == key ) return i; // key was not found in list[] return -1; } public static void main(String[] args) { int[] myList = {1, 4, 4, 2, 5, -3, 6, 2}; int i = linearSearch(myList, 4); // Returns 1 int j = linearSearch(myList, -4); // Returns -1 int k = linearSearch(myList, -3); // Returns 5 } |
Examine each array element sequentially:
/* ---------------------------------------------------- The linear search algorithm to find key in the array list ---------------------------------------------------- */ public static int linearSearch(int[] list, int key) { for ( int i = 0; i < list.length; i++ ) if ( list[i] == key ) return i; // key was not found in list[] return -1; } public static void main(String[] args) { int[] myList = {1, 4, 4, 2, 5, -3, 6, 2}; int i = linearSearch(myList, 4); // Returns 1 int j = linearSearch(myList, -4); // Returns -1 int k = linearSearch(myList, -3); // Returns 5 } |
Look for the key value in each array element:
/* ----------------------------------------------------
The linear search algorithm to find key
in the array list
---------------------------------------------------- */
public static int linearSearch(int[] list, int key)
{
for ( int i = 0; i < list.length; i++ )
if ( list[i] == key )
return i;
// key was not found in list[]
return -1;
}
public static void main(String[] args)
{
int[] myList = {1, 4, 4, 2, 5, -3, 6, 2};
int i = linearSearch(myList, 4); // Returns 1
int j = linearSearch(myList, -4); // Returns -1
int k = linearSearch(myList, -3); // Returns 5
}
|
Return the index of the array element if match found:
/* ----------------------------------------------------
The linear search algorithm to find key
in the array list
---------------------------------------------------- */
public static int linearSearch(int[] list, int key)
{
for ( int i = 0; i < list.length; i++ )
if ( list[i] == key )
return i;
// key was not found in list[]
return -1;
}
public static void main(String[] args)
{
int[] myList = {1, 4, 4, 2, 5, -3, 6, 2};
int i = linearSearch(myList, 4); // Returns 1
int j = linearSearch(myList, -4); // Returns -1
int k = linearSearch(myList, -3); // Returns 5
}
|
When all elements have been searched, then the key is not found in the array. We return -1.
/* ---------------------------------------------------- The linear search algorithm to find key in the array list ---------------------------------------------------- */ public static int linearSearch(int[] list, int key) { for ( int i = 0; i < list.length; i++ ) if ( list[i] == key ) return i; // key was not found in list[] return -1; } public static void main(String[] args) { int[] myList = {1, 4, 4, 2, 5, -3, 6, 2}; int i = linearSearch(myList, 4); // Returns 1 int j = linearSearch(myList, -4); // Returns -1 int k = linearSearch(myList, -3); // Returns 5 } |
DEMO: demo/07-search-array/01-lin-search/Demo.java
|
|
The binary search algorithm for arrays:
+---+---+---+---+---+---+---+---+---+---+---+---+---+ list[]: | | | | | | | | | | | | | | +---+---+---+---+---+---+---+---+---+---+---+---+---+ ^ ^ | | low high public static int binarySearch(int[] list, int key) { int low = 0; int high = list.length - 1; while (low <= high) { int mid = (low + high) / 2; if (key == list[mid]) return mid; else if (key < list[mid]) high = mid - 1; else low = mid + 1; } return -1; // Not found } |
(1) Find the middle element
+---+---+---+---+---+---+---+---+---+---+---+---+---+
list[]: | | | | | | | | | | | | | |
+---+---+---+---+---+---+---+---+---+---+---+---+---+
^ ^ ^
| | |
low mid high
public static int binarySearch(int[] list, int key)
{
int low = 0;
int high = list.length - 1;
while (low <= high)
{
int mid = (low + high) / 2;
if (key == list[mid])
return mid;
else if (key < list[mid])
high = mid - 1;
else
low = mid + 1;
}
return -1; // Not found
}
|
(2) If list[mid] == key, we found the search value key:
+---+---+---+---+---+---+---+---+---+---+---+---+---+ list[]: | | | | | | |key| | | | | | | +---+---+---+---+---+---+---+---+---+---+---+---+---+ ^ ^ ^ | | | low mid high public static int binarySearch(int[] list, int key) { int low = 0; int high = list.length - 1; while (low <= high) { int mid = (low + high) / 2; if (list[mid] == key) return mid; else if (list[mid] > key) high = mid - 1; else low = mid + 1; } return -1; // Not found } |
(3) If list[mid] > key, we continue the search in the lower half of the array:
+---+---+---+---+---+---+---+---+---+---+---+---+---+ list[]: | | | | | | | > | | | | | | | +---+---+---+---+---+---+---+---+---+---+---+---+---+ ^ ^ ^ | | | low high mid |
(4) Otherwise ( list[mid] < key), we continue the search in the upper half of the array:
+---+---+---+---+---+---+---+---+---+---+---+---+---+ list[]: | | | | | | | < | | | | | | | +---+---+---+---+---+---+---+---+---+---+---+---+---+ ^ ^ ^ | | | |
(5) We must repeat the steps for the remaining array elements: When do we stop ?
+---+---+---+---+---+---+---+---+---+---+---+---+---+ list[]: | | | | | | | | | | | | | | +---+---+---+---+---+---+---+---+---+---+---+---+---+ ^ ^ | | |
(6) We stop when low > high:
+---+---+---+---+---+---+---+---+---+---+---+---+---+ list[]: | | | | | | | | | | | | | | +---+---+---+---+---+---+---+---+---+---+---+---+---+ ^ ^ | | <--- empty array high low public static int binarySearch(int[] list, int key) { int low = 0; int high = list.length - 1; while ( low <= high ) { int mid = (low + high) / 2; if (list[mid] == key) return mid; else if (list[mid] > key) high = mid - 1; else low = mid + 1; } return -1; // Not found } |
(7) we return -1 when key is not found:
+---+---+---+---+---+---+---+---+---+---+---+---+---+ list[]: | | | | | | | | | | | | | | +---+---+---+---+---+---+---+---+---+---+---+---+---+ ^ ^ | | <--- empty array high low public static int binarySearch(int[] list, int key) { int low = 0; int high = list.length - 1; while ( low <= high ) { int mid = (low + high) / 2; if (list[mid] == key) return mid; else if (list[mid] > key) high = mid - 1; else low = mid + 1; } return -1; // Not found } |
public static void main(String[] args) { int[] myList = {1, 5, 9, 17, 19, 78, 99, 143, 450, 876, 999}; int r; r = binarySearch(myList, 143); System.out.println("r = " + r); } public static int binarySearch(int[] list, int key) { int low = 0; int high = list.length - 1; while ( low <= high ) { int mid = (low + high) / 2; if (list[mid] == key) return mid; else if (list[mid] > key) high = mid - 1; else low = mid + 1; } return -1; // Not found } |
DEMO: demo/07-search-array/02-bin-search/Demo.java
|
|
|
|
|
|
|
|