Benchmarking is still useful when comparing two implementations in very specific contexts. But those measurements don't let you compare algorithms, nor are they useful in different contexts (hardware, language, etc.).
for (int k = 0; k < n; k++) {
//statement 1
//statement 2
//...
//statement 5
}
//...25 more statements...
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
//statement 1
//statement 2
}
}
Time to run = T(n) = n*5 + 25 + n*n*2 = 2n2 + 5n + 25
(assuming each statement takes 1 "tick" to run)
T(n) = O(f(n)) as n -> ∞
iff there exist constants n0 >= 0 and c > 0 such that
for all n > n0, T(n) <= cf(n).
Here, I defined Big-O for T(n) in terms of f(n). In other contexts, you'll see the big-O of any function f(n) defined in terms of g(n). Same thing. Just replace f(n) with g(n) above, and then T(n) with f(n). You end up with:
f(n) = O(g(n)) as n->∞ iff ...(etc)...
which is the definition format you'll often see in other sources.
(Note we could have selected f(n) = 8n2 + n OR f(n) = n4 here instead. Valid, but convention (especially in this class) is to choose the simplest and lowest-order f(n) that you can.)
T(n) = O(f(n)) as n -> ∞
iff there exist constants n0 and c such that
for all n > n0, T(n) <= cf(n).
Summary: Big-O tells us the algorithm won't grow any faster (get any worse) than a given f(n), no matter how many more processed elements are added.
/** Returns index of target found in x array, or -1 if not found */
public static int search(int[] x, int target) {
for (int i = 0; i < x.length; i++) {
if (x[i] == target) {
return i;
}
}
return -1; //looped through whole array and didn't find target
}
/** Prints the given array forward and then backwards. */
public static void backAndForth(int[] nums) {
//forward
for (int n : nums) {
System.out.print(n);
System.out.print(" ");
}
System.out.println();
//backwards
for (int i = nums.length - 1; i >= 0; i--) {
System.out.print(nums[i]);
System.out.print(" ");
}
System.out.println();
}
/** Prints a box of #s of width * height. */
public static void drawBox(int width, int height) {
for (int row = 0; row < height; row++) {
for (int col = 0; col < width; col++) {
System.out.print('#');
}
System.out.println();
}
}
/** Prints 5 boxes of the given size. */
public static void drawBoxes(int size) {
for (int i = 0; i < 5; i++) {
drawBox(size, size);
System.out.println();
}
}
/** Prints the given number of boxes. */
public static void drawBoxes(int many) {
for (int i = 0; i < many; i++) {
drawBox(10, 10);
System.out.println();
}
}
/** Returns whether there are any repeated values in the given array. */
public static boolean findDuplicates(int[] nums) {
for (int i = 0; i < nums.length; i++) {
for (int j = i + 1; j < nums.length; j++) {
if (nums[i] == nums[j]) {
return true;
}
}
}
return false;
}
int[] nums = {5, 6, 90};
int[] ages = new int[10];
int size = nums.length;
nums[1] = 60;
for (int i = 0; i < nums.length; i++) { //use .length, not 3 here
nums[i]++;
}
for (int n : nums) {
System.out.println(n);
}
Again, review: should already know this.
char[][] ticTacToe = new char[3][3];
tictactoe[1][1] = 'X';
char[] middleRow = ticTacToe[1];
System.out.println(middleRow[1]);
int[][] matrix = new int[4][]; //sub-arrays can be different size if created later
for (int i = 0; i < matrix.length; i++) {
matrix[i] = new int[i + 1];
}
matrix[1][1] = 9;
public class Hello {
public static void main(String[] args) {
//...
}
}
java Hello
java Hello one two (args: ["one", "two"])
java Hello 3 (args: ["3"])
java Hello (args: [])
import java.util.Scanner;
import java.io.File;
import java.io.IOException;
public class PrintFile {
/** Prints to the screen the contents of the file named input.txt. */
public static void main(String[] args) {
try {
Scanner file = new Scanner(new File("input.txt"));
while (file.hasNextLine()) {
String line = file.nextLine();
System.out.println(line);
}
file.close();
}catch (IOException e) {
System.out.println("Could not read from file: " + e.getMessage());
}
}
}
hasNext(), hasNextInt(), etc.
new Scanner(new File(...)). (Don't forget the new File part)
import java.io.IOException;
import java.io.FileReader;
import java.io.BufferedReader;
public class Hello {
/** Prints to the screen the contents of the file named input.txt. */
public static void main(String[] args) {
try {
BufferedReader file = new BufferedReader(new FileReader("input.txt"));
String line = file.readLine();
while (line != null) {
System.out.println(line);
line = file.readLine();
}
}catch (IOException e) {
System.out.println("Could not read from file: " + e.getMessage());
}
}
}
String line = file.readLine(); //initializer
while (line != null) { //condition
System.out.println(line);
line = file.readLine(); //"increment"
}
Those are the components of a for loop...
for (String line = file.readLine(); line != null; line = file.readLine()) {
System.out.println(line);
}
Could have done something similar with a Scanner too (tho not quite as gracefully):
for (String line = null; file.hasNextLine(); line = file.nextLine()) {
if (line != null) {
System.out.println(line);
}
}