13. Sorting

  • Feel free to use your laptop

  • You are strongly encourage to work with others

    • When you get stuck, ask those sitting around you for help

    • Get used to working together in the labs

    • Peer teaching and peer learning has been empirically shown to be very effective

Note

This lab is making use of integer arrays for sorting, but feel free to use IndexedBags. If an IndexedBag is used, the method signatures will need to be updated. For example,

public static <T extends Comparable<T>> void bubbleSort(IndexedBag<T> toSort)

13.1. Starting Point

  • This lab focuses on the sorting algorithms discussed in the first Sorting Topic

  • The below code can be used for setting up an array to be sorted

  • This code can also be used for timing the different sorting algorithms

public static void main(String[] args){

    int[] toSort = createUnsortedArray(10000, 10);
    printArray(toSort);

    double startTime = System.nanoTime();
    //bubbleSort(toSort);
    //insertionSort(toSort);
    //selectionSort(toSort);
    double endTime = System.nanoTime();

    printArray(toSort);
    System.out.println("runtime: " + (endTime - startTime)/1000000 + " ms");
}

public static int[] createUnsortedArray(int n, int maxValue){
    int[]  toSort= new int[n];
    for(int i = 0; i < n; ++i){
        toSort[i] = (int)(Math.random() * maxValue);
    }
    return toSort;
}

public static void printArray(int[] toPrint){
    for(int value: toPrint){
        System.out.print(value + ", ");
    }
    System.out.println();
}

13.2. Bubble Sort

  1. Implement the bubble sort algorithm

  2. Make sure it works and run it a few times to see what the runtimes are

    • Change the size of the array and the max value and see how it impacts runtimes

    • For the purposes of this lab, do not worry about writing unit tests

public static void bubbleSort(int[] toSort){
    // Fill me in
}

13.3. Insertion Sort

  1. Implement the insertion sort algorithm

  2. Make sure it works and run it a few times to see what the runtimes are

    • Change the size of the array and the max value and see how it impacts runtimes

    • For the purposes of this lab, do not worry about writing unit tests

public static void insertionSort(int[] toSort){
    // Fill me in
}

13.4. Selection Sort

  1. Implement the selection sort algorithm

  2. Make sure it works and run it a few times to see what the runtimes are

    • Change the size of the array and the max value and see how it impacts runtimes

    • For the purposes of this lab, do not worry about writing unit tests

public static void selectionSort(int[] toSort){
    // Fill me in
}

13.5. Comparing Sorts

  1. Run each of the sorts a few times and take note of the runtimes

    • Play around with the size of the arrays and the max value

    • Compare the runtimes of each algorithm to what was expected based on their computational complexities

  2. Try adding a counter variable into the inner loops of each of the sorts to see how many times the loops ran

13.6. Kattis Problems

  1. https://open.kattis.com/problems/quadrant

  2. https://open.kattis.com/problems/judgingmoose

  3. https://open.kattis.com/problems/timeloop

  4. https://open.kattis.com/problems/oddities

  5. https://open.kattis.com/problems/fizzbuzz

  6. https://open.kattis.com/problems/twostones

  7. https://open.kattis.com/problems/spavanac

  8. https://open.kattis.com/problems/cetvrta

  9. https://open.kattis.com/problems/bus

  10. https://open.kattis.com/problems/timeloop

  11. https://open.kattis.com/problems/oddities

  12. https://open.kattis.com/problems/fizzbuzz

  13. https://open.kattis.com/problems/sibice

  14. https://open.kattis.com/problems/datum

  15. https://open.kattis.com/problems/dicecup

  16. https://open.kattis.com/problems/autori

  17. https://open.kattis.com/problems/apaxiaaans

  18. https://open.kattis.com/problems/hissingmicrophone

  19. https://open.kattis.com/problems/trik

  20. https://open.kattis.com/problems/pot

  21. https://open.kattis.com/problems/filip

  22. https://open.kattis.com/problems/reversebinary

  23. https://open.kattis.com/problems/sevenwonders

  24. https://open.kattis.com/problems/zamka

  25. https://open.kattis.com/problems/bijele

  26. https://open.kattis.com/problems/cold

  27. https://open.kattis.com/problems/nastyhacks

  28. https://open.kattis.com/problems/grassseed

  29. https://open.kattis.com/problems/pet

  30. https://open.kattis.com/problems/batterup

  31. https://open.kattis.com/problems/aboveaverage

  32. https://open.kattis.com/problems/icpcawards

  33. https://open.kattis.com/problems/quickbrownfox

  34. https://open.kattis.com/problems/nodup

  35. https://open.kattis.com/problems/conundrum

  36. https://open.kattis.com/problems/bela

  37. https://open.kattis.com/problems/kornislav