Chapter 5 · Real programs

Practice: searching and sorting

You are reading the whole lesson on one page. Back to one step at a time

Step 1 of 21 · Concept

Look at each one in turn

This lesson is practice: the classic searches and sorts, written by hand.

Linear search looks at each element in turn, until it finds the one it wants. It returns that index, or -1 when there is none. -1 can never be an index.

public class Search {
    static int indexOf(int[] values, int target) {
        for (int i = 0; i < values.length; i++) {
            if (values[i] == target) {
                return i;
            }
        }
        return -1;
    }

    public static void main(String[] args) {
        int[] values = {7, 3, 9, 4, 9};
        System.out.println(indexOf(values, 9));
        System.out.println(indexOf(values, 5));
    }
}
public class Binary {
    static int binarySearch(int[] sorted, int target) {
        int low = 0;
        int high = sorted.length - 1;
        return -1;
    }

    public static void main(String[] args) {
        int[] a = {2, 5, 8, 12, 16, 23, 38, 56, 72, 91};
        System.out.println("-> " + binarySearch(a, 23));
        System.out.println("-> " + binarySearch(a, 2));
        System.out.println("-> " + binarySearch(a, 91));
        System.out.println("-> " + binarySearch(a, 7));
    }
}
import java.util.Arrays;

public class Bubble {
    static void bubbleSort(int[] a) {
        for (int pass = 0; pass < a.length - 1; pass++) {
            for (int i = 0; i < a.length - 1 - pass; i++) {
                if (a[i] > a[i + 1]) {
                    int tmp = a[i];
                    a[i] = a[i + 1];
                    a[i + 1] = tmp;
                }
            }
            System.out.println(Arrays.toString(a));
        }
    }

    public static void main(String[] args) {
        int[] a = {5, 1, 4, 2, 8};
        bubbleSort(a);
    }
}
import java.util.Arrays;

public class Selection {
    static void selectionSort(int[] a) {
    }

    public static void main(String[] args) {
        int[] a = {29, 10, 14, 37, 13};
        selectionSort(a);
    }
}
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

public class Count {
    static Map<String, Integer> count(String[] words) {
        Map<String, Integer> counts = new HashMap<>();
        return counts;
    }

    static String mostCommon(Map<String, Integer> counts) {
        return "";
    }

    public static void main(String[] args) {
        String text = "the cat and the dog and the bird";
        String[] words = text.split(" ");
        Map<String, Integer> counts = count(words);
        System.out.println(new TreeMap<>(counts));
        String top = mostCommon(counts);
        System.out.println("Most common: " + top);
    }
}
public class Reverse {
    static String reverseWords(String sentence) {
        return sentence;
    }

    public static void main(String[] args) {
        String[] tests = {
            "Java runs in the browser", "one", "a b c"
        };
        for (String t : tests) {
            System.out.println(reverseWords(t));
        }
    }
}
public class Palindrome {
    static boolean isPalindrome(String word) {
        return false;
    }

    public static void main(String[] args) {
        String[] words = {
            "level", "Racecar", "java", "abba", "a"
        };
        for (String w : words) {
            System.out.println(w + ": " + isPalindrome(w));
        }
    }
}
import java.util.Arrays;

public class Dictionary {
    // sorts the words alphabetically, by hand
    static void sort(String[] words) {
    }

    // a binary search: word's index, or -1
    static int find(String[] sorted, String word) {
        return -1;
    }

    public static void main(String[] args) {
        String[] words = {
            "pear", "apple", "fig", "kiwi", "banana", "cherry"
        };
        sort(words);
        System.out.println(Arrays.toString(words));
        System.out.println("kiwi: " + find(words, "kiwi"));
        System.out.println("mango: " + find(words, "mango"));
        System.out.println("apple: " + find(words, "apple"));
    }
}
import java.util.Arrays;

public class BubbleStop {
    static int bubbleSort(int[] a) {
        int passes = 0;
        for (int pass = 0; pass < a.length - 1; pass++) {
            for (int i = 0; i < a.length - 1 - pass; i++) {
                if (a[i] > a[i + 1]) {
                    int tmp = a[i];
                    a[i] = a[i + 1];
                    a[i + 1] = tmp;
                }
            }
            passes++;
            System.out.println(Arrays.toString(a));
        }
        return passes;
    }

    public static void main(String[] args) {
        int[] a = {5, 1, 4, 2, 8};
        System.out.println("Passes: " + bubbleSort(a));
    }
}
Terminal
Step 1 of 9Linear search

Keyboard shortcuts

The step

ContinueEnter
Previous / next stepAlt+[ · Alt+]
Move along the steps bar← → Home End, then Enter
From a quiz's title to its answers↓
Skip to the stepTab, then Enter (first on the page)
Course outlineAlt+O
These shortcuts?

The workspace

Run the stepCtrl+Enter (⌘↵ on a Mac)
Lesson → editor → consoleF6 (Shift+F6 back)
Output ↔ TerminalCtrl+`
Focus the editorCtrl+. (⌘. on a Mac)
Hide or show the lessonAlt+L
Restore, close a messageEsc (outside the editor and the terminal)

The editor

Leave the editorF6 (Shift+F6 back); or Ctrl+M (⌃⇧M on a Mac), then Tab moves the focus (not in the vim mode)
Open a file by nameCtrl+P (⌘P on a Mac)
All commandsCtrl+Shift+P (⌘⇧P on a Mac) or F1
Go to a lineCtrl+G (⌃G on a Mac)
Show or hide the filesCtrl+B (⌘B on a Mac)
Previous / next file tabCtrl+Shift+[ · Ctrl+Shift+] (⌘⇧[ · ⌘⇧] on a Mac)
The file tabs← → Home End on a tab; Delete closes it
Save the files nowCtrl+S (⌘S on a Mac)
Go to a symbol in the fileCtrl+Shift+O (⌘⇧O on a Mac)
Go to the definitionF12
Format the fileCtrl+Shift+F (⌘⇧F on a Mac), or Shift+Alt+F
Go back / forward after a jumpAlt+← · Alt+→ (⌃- · ⌃⇧- on a Mac)