Joseph Haugh
University of New Mexico
void bubbleSort(int[] xs) {
// loop over the array
// loop backward until we get to the next slot
// compare adjacent elements
// move the smaller element toward the front if necessary
// repeat, skipping the slot with the new minimum
}
void bubbleSort(int[] xs) {
// loop over the array
for (int i = 1; i < xs.length; i++) {
// loop backward until we get to the next slot
for (int j = xs.length - 1; j >= i; j--) {
// compare adjacent elements
// move the smaller element toward the front if necessary
}
// repeat, skipping the slot with the new minimum
}
}
void bubbleSort(int[] xs) {
// loop over the array
for (int i = 1; i < xs.length; i++) {
// loop backward until we get to the next slot
for (int j = xs.length - 1; j >= i; j--) {
// compare adjacent elements
// move the smaller element toward the front if necessary
if (xs[j-1] > xs[j]) {
int temp = xs[j-1];
xs[j-1] = xs[j];
xs[j] = temp;
}
}
// repeat, skipping the slot with the new minimum
}
}
void main() {
int[] xs = { 4, 6, 1, 3, 8, 3, 1, 2 };
IO.println("xs = " + Arrays.toString(xs));
bubbleSort(xs);
IO.println("sorted xs = " + Arrays.toString(xs));
}
bubbleSort is not terribly efficientint[] randomIntArray(Random rand, int len) {
int[] xs = new int[len];
for (int i = 0; i < len; i++) {
xs[i] = rand.nextInt(0, 100);
}
return xs;
}
void bubbleSort(int[] xs) {
// loop over the array
for (int i = 1; i < xs.length; i++) {
// loop backward until we get to the next slot
for (int j = xs.length - 1; j >= i; j--) {
// compare adjacent elements
// move the smaller element toward the front if necessary
if (xs[j-1] > xs[j]) {
int temp = xs[j-1];
xs[j-1] = xs[j];
xs[j] = temp;
}
}
// repeat, skipping the slot with the new minimum
}
}
int[] randomIntArray(Random rand, int len) {
int[] xs = new int[len];
for (int i = 0; i < len; i++) {
xs[i] = rand.nextInt(0, 100);
}
return xs;
}
String showDuration(String name, long duration) {
return name + " ran for " + TimeUnit.NANOSECONDS.toMillis(duration) + " ms";
}
void main() {
Random rand = new Random();
// Small example
int[] xs = randomIntArray(rand, 10);
// Let's time it
long startTime = System.nanoTime();
bubbleSort(xs);
long endTime = System.nanoTime();
IO.println(showDuration("Small example", endTime - startTime));
// Medium example
xs = randomIntArray(rand, 10000);
startTime = System.nanoTime();
bubbleSort(xs);
endTime = System.nanoTime();
IO.println(showDuration("Medium example", endTime - startTime));
// Large example
xs = randomIntArray(rand, 10000000);
startTime = System.nanoTime();
bubbleSort(xs);
endTime = System.nanoTime();
IO.println(showDuration("Large example", endTime - startTime));
}
Output:
Small example ran for 0 ms
Medium example ran for 146 ms
Large example ran for 146000000 ms
Same code but with the built-in sort
void bubbleSort(int[] xs) {
// loop over the array
for (int i = 1; i < xs.length; i++) {
// loop backward until we get to the next slot
for (int j = xs.length - 1; j >= i; j--) {
// compare adjacent elements
// move the smaller element toward the front if necessary
if (xs[j-1] > xs[j]) {
int temp = xs[j-1];
xs[j-1] = xs[j];
xs[j] = temp;
}
}
// repeat, skipping the slot with the new minimum
}
}
int linearSearch(int x, int[] xs) {
// loop over the array element by element
for (int i = 0; i < xs.length; i++) {
// check if each element is equal to x
if (xs[i] == x) {
// if it is return the current index
return i;
}
// else keep looping
}
// if not found return -1
return -1;
}
int[] randomIntArray(Random rand, int len) {
int[] xs = new int[len];
for (int i = 0; i < len; i++) {
xs[i] = rand.nextInt(0, 100);
}
return xs;
}
String showDuration(String name, long duration) {
return name + " ran for " + TimeUnit.NANOSECONDS.toMillis(duration) + " ms";
}
void main() {
Random rand = new Random();
// Small example
int[] xs = randomIntArray(rand, 10);
// Let's time it
long startTime = System.nanoTime();
Arrays.sort(xs);
long endTime = System.nanoTime();
IO.println(showDuration("Small example", endTime - startTime));
// Medium example
xs = randomIntArray(rand, 10000);
startTime = System.nanoTime();
Arrays.sort(xs);
endTime = System.nanoTime();
IO.println(showDuration("Medium example", endTime - startTime));
// Large example
xs = randomIntArray(rand, 10000000);
startTime = System.nanoTime();
Arrays.sort(xs);
endTime = System.nanoTime();
IO.println(showDuration("Large example", endTime - startTime));
}
Output:
Small example ran for 2 ms
Medium example ran for 6 ms
Large example ran for 419 ms
int linearSearch(int x, int[] xs) {
// loop over the array element by element
// check if each element is equal to x
// if it is return the current index
// else keep looping
// if not found return -1
}
int linearSearch(int x, int[] xs) {
// loop over the array element by element
for (int i = 0; i < xs.length; i++) {
// check if each element is equal to x
// if it is return the current index
// else keep looping
}
// if not found return -1
return -1;
}
int linearSearch(int x, int[] xs) {
// loop over the array element by element
for (int i = 0; i < xs.length; i++) {
// check if each element is equal to x
if (xs[i] == x) {
// if it is return the current index
return i;
}
// else keep looping
}
// if not found return -1
return -1;
}
void main() {
int[] xs = { 2, 4, 5, 6, 1, 2, 9, 3, 2 };
IO.println(linearSearch(2, xs)); // 0
IO.println(linearSearch(9, xs)); // 6
IO.println(linearSearch(10, xs)); // -1
}
int binarySearch(int x, int[] xs) {
// keep track of beginning and end of section of array
// loop until we find the number or search everywhere
// find the midpoint
// compare it to x
// if it is less, then search first half
// if it is more, then search second half
// if it is equal, then we found it!
// repeat until we find the number
// if not found return -1
}
int binarySearch(int x, int[] xs) {
// keep track of beginning and end of section of array
int beg = 0;
int end = xs.length - 1;
// loop until we find the number or search everywhere
while (beg <= end) {
// find the midpoint
int mid = (beg + end) / 2;
int midValue = xs[mid];
// compare it to x
// if it is less, then search first half
// if it is more, then search second half
// if it is equal, then we found it!
// repeat until we find the number
}
// if not found return -1
return -1;
}
int binarySearch(int x, int[] xs) {
// keep track of beginning and end of section of array
int beg = 0;
int end = xs.length - 1;
// loop until we find the number or search everywhere
while (beg <= end) {
// find the midpoint
int mid = (beg + end) / 2;
int midValue = xs[mid];
// compare it to x
if (x < mid) {
// if it is less, then search first half
end = mid - 1;
} else if (x > mid) {
// if it is more, then search second half
beg = mid + 1;
} else {
// if it is equal, then we found it!
return mid;
}
// repeat until we find the number
}
// if not found return -1
return -1;
}
void main() {
int[] xs = { 2, 4, 5, 6, 1, 2, 9, 3, 2 };
IO.println(linearSearch(2, xs)); // 0
IO.println(linearSearch(9, xs)); // 6
IO.println(linearSearch(10, xs)); // -1
}
int[] randomSortedIntArray(Random rand, int len) {
int[] xs = new int[len];
for (int i = 0; i < len; i++) {
xs[i] = rand.nextInt(Integer.MIN_VALUE, Integer.MAX_VALUE - 1);
}
Arrays.sort(xs);
xs[len - 1] = Integer.MAX_VALUE;
return xs;
}
int linearSearch(int x, int[] xs) {
// loop over the array element by element
for (int i = 0; i < xs.length; i++) {
// check if each element is equal to x
if (xs[i] == x) {
// if it is return the current index
return i;
}
// else keep looping
}
// if not found return -1
return -1;
}
int[] randomSortedIntArray(Random rand, int len) {
int[] xs = new int[len];
for (int i = 0; i < len; i++) {
xs[i] = rand.nextInt(Integer.MIN_VALUE, Integer.MAX_VALUE - 1);
}
Arrays.sort(xs);
xs[len - 1] = Integer.MAX_VALUE;
return xs;
}
String showDuration(String name, long duration) {
return name + " ran for " + TimeUnit.NANOSECONDS.toMillis(duration) + " ms";
}
void main() {
Random rand = new Random();
// Small example
int[] xs = randomSortedIntArray(rand, 100);
// Let's time it
long startTime = System.nanoTime();
linearSearch(Integer.MAX_VALUE, xs);
long endTime = System.nanoTime();
IO.println(showDuration("Small example", endTime - startTime));
// Medium example
xs = randomSortedIntArray(rand, 100000);
startTime = System.nanoTime();
linearSearch(Integer.MAX_VALUE, xs);
endTime = System.nanoTime();
IO.println(showDuration("Medium example", endTime - startTime));
// Large example
xs = randomSortedIntArray(rand, 100000000);
startTime = System.nanoTime();
linearSearch(Integer.MAX_VALUE, xs);
endTime = System.nanoTime();
IO.println(showDuration("Large example", endTime - startTime));
}
Output:
Small example ran for 0 ms
Medium example ran for 1 ms
Large example ran for 39 ms
int binarySearch(int x, int[] xs) {
// keep track of beginning and end of section of array
int beg = 0;
int end = xs.length - 1;
// loop until we find the number or search everywhere
while (beg <= end) {
// find the midpoint
int mid = (beg + end) / 2;
int midValue = xs[mid];
// compare it to x
if (x < mid) {
// if it is less, then search first half
end = mid - 1;
} else if (x > mid) {
// if it is more, then search second half
beg = mid + 1;
} else {
// if it is equal, then we found it!
return mid;
}
// repeat until we find the number
}
// if not found return -1
return -1;
}
int[] randomSortedIntArray(Random rand, int len) {
int[] xs = new int[len];
for (int i = 0; i < len; i++) {
xs[i] = rand.nextInt(Integer.MIN_VALUE, Integer.MAX_VALUE - 1);
}
Arrays.sort(xs);
xs[len - 1] = Integer.MAX_VALUE;
return xs;
}
String showDuration(String name, long duration) {
return name + " ran for " + TimeUnit.NANOSECONDS.toMillis(duration) + " ms";
}
void main() {
Random rand = new Random();
// Small example
int[] xs = randomSortedIntArray(rand, 100);
// Let's time it
long startTime = System.nanoTime();
binarySearch(Integer.MAX_VALUE, xs);
long endTime = System.nanoTime();
IO.println(showDuration("Small example", endTime - startTime));
// Medium example
xs = randomSortedIntArray(rand, 100000);
startTime = System.nanoTime();
binarySearch(Integer.MAX_VALUE, xs);
endTime = System.nanoTime();
IO.println(showDuration("Medium example", endTime - startTime));
// Large example
xs = randomSortedIntArray(rand, 100000000);
startTime = System.nanoTime();
binarySearch(Integer.MAX_VALUE, xs);
endTime = System.nanoTime();
IO.println(showDuration("Large example", endTime - startTime));
}
Output:
Small example ran for 0 ms
Medium example ran for 0 ms
Large example ran for 0 ms