To search an array for a particular value
true, or the index
false or -1 (why -1?)
The algorithm (i.e., the approach) used by selection sort corresponds closely to the technique you might use if you were asked to sort a deck of cards, a pile of folders, a list of names:
Selection sort basically does the same thing, but we take into consideration that we're working with an array:
+----------------------------------------------------------------------------+ | | | unsorted | | | +----------------------------------------------------------------------------+
After one iteration through the outer loop:
+----------------------------------------------------------------------------+ | | | |smallest| unsorted | | | | +----------------------------------------------------------------------------+Halfway through the outer loop:
+----------------------------------------------------------------------------+ | | | | sorted | unsorted | | | | +----------------------------------------------------------------------------+At end:
+----------------------------------------------------------------------------+ | | | sorted | | | +----------------------------------------------------------------------------+
A very high-level statement of the approach:
Go through the array from beginning to end Find the smallest element in the array from the current position to the end, and place it in that position
In a bit more detail:
Go through the array from beginning to end
Go through the array from the current position to the end
If an element is less than the value in the current position swap them
Let's start getting down to code-level
for (currentPosition = 0; currentPosition < size; currentPosition++)
for (j = currentPosition+1; j < size; j++)
if (arr[j] < arr[currentPosition]
swap(arr[j], arr[currentPosition])
Here it is turned into a method:
void selectionSort(int [] arr, int size) {
for (currentPosition = 0; currentPosition < size; currentPosition++)
for (j = currentPosition+1; j < size; j++)
if (arr[j] < arr[currentPosition]
swap(arr, j, currentPosition)
}
void selectionSort(int [] arr, int size) {
for (currentPosition = 0; currentPosition < size; currentPosition++)
int pos = currentPosition;
for (j = currentPosition+1; j < size; j++)
if (arr[j] < arr[currentPosition]
pos = j;
swap(arr, pos, currentPosition)
}
void selectionSort(int [] arr, int size) {
for (currentPosition = 0; currentPosition < size; currentPosition++)
int smallestPosition = currentPosition;
for (j = currentPosition+1; j < size; j++)
if (arr[j] < arr[currentPosition]
smallestPosition = j;
if (smallestPosition != currentPosition) swap(arr, pos, currentPosition)
}
and here it is with the inner loop (a single pass through the array) turned into a method:
void selectionSort(int [] arr, int size) {
for (currentPosition = 0; currentPosition < size; currentPosition++)
int smallestPosition = findSmallestPosition(arr, size);
if (smallestPosition != currentPosition) swap(arr, pos, currentPosition)
}
int findSmallestPosition(int [] arr, int size) {
int smallestPosition = currentPosition;
for (j = currentPosition+1; j < size; j++)
if (arr[j] < arr[currentPosition]
smallestPosition = j;
return smallestPosition;
}
In bubble sort:
Pass through the array until a pass is completed with no swaps performed Compare adjacent elements and swap if they're out of order
In a bit more detail:
Go through the array from the beginning to end
If an element at position i is less than the element at position i+1 swap them
If a swap was performed repeat
Let's start getting down to code-level
do {
bool swapped = false;
for (i = 0; i < n-1; i++)
if (arr[i] > arr[i+1] {
swap(arr, i, i+1)
swapped = true;
}
} while (swapped)
Notice the pattern of how swapped is used:
for (int i = 0; i < n; i++) for (int j = 0; j < n-1; j++) if (arr[j] > arr[j+1]) swap(arr, j, j+1);
n-2) (not doing so would
result in an ArrayIndexOutOfBoundsException)
for (int last = n-1; last > 0; last--) for (int i = 0; i < last; i++) if (arr[i] > arr[i+1]) swap(arr, i, i+1);
boolean swapped = true;
for (int last = n-1; last > 0 && swapped; last--) {
swapped = false;
for (int i = 0; i < last; i++)
if (arr[i] > arr[i+1]) {
swap(arr, i, i+1);
swapped = true;
}
}
swapped will be set in the inner loop (the loop performing the comparison pass)
if any swap is performed
swapped to true
do/while
lo and hi respectively.
lo and hi have crossed, i.e., lo is greater than hi, i.e., the subarray is empty, report failure
lo … hi; call it mid
lo … mid-1
mid+1 … hi
public static int find(int [] arr, int val) {
int lo = 0, hi = arr.length-1;
while (lo <= hi) {
int mid = (hi+lo)/2;
if (val == arr[mid])
return mid;
else if (val < arr[mid])
hi = mid - 1;
else
lo = mid + 1;
}
return -1;
}