Skip to content

Instantly share code, notes, and snippets.

@CEOehis
Created June 25, 2020 14:29
Show Gist options
  • Save CEOehis/a5cc4764f9a2bd966d40df9564e7aa61 to your computer and use it in GitHub Desktop.
Save CEOehis/a5cc4764f9a2bd966d40df9564e7aa61 to your computer and use it in GitHub Desktop.

Revisions

  1. CEOehis created this gist Jun 25, 2020.
    99 changes: 99 additions & 0 deletions BenchMarker.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,99 @@
    import java.util.Arrays;

    /**
    *
    *
    * Simple class for benchmarking some sorting algorithms
    */
    public class BenchMarker {
    final static int ARRAY_SIZE = 100_000;

    /**
    * Sorts an unsorted list of integers by using insertion sort
    * @param list an array of integers.
    */
    static void insertionSort(int[] list) {
    int itemsSorted;
    for(itemsSorted = 1; itemsSorted < list.length; itemsSorted++) {
    int temp = list[itemsSorted];
    int loc = itemsSorted - 1;
    while(loc >= 0 && list[loc] > temp) {
    list[loc + 1] = list[loc];
    loc = loc - 1;
    }
    list[loc + 1] = temp;
    }
    }

    /**
    * Sorts an unsorted list of integers by using selection sort
    * @param list an array of integers.
    */
    static void selectionSort(int [] list) {
    for(int first = 0; first < list.length; first++) {

    int smallestLoc = first;

    for(int j = first + 1; j < list.length; j++) {
    if(list[j] < list[smallestLoc]) {
    smallestLoc = j;
    }
    }

    int temp = list[smallestLoc];
    list[smallestLoc] = list[first];
    list[first] = temp;
    }
    }

    public static void main(String[] args) {
    int[] firstIntList = new int[ARRAY_SIZE];
    int[] secondIntList = new int[ARRAY_SIZE];
    int[] thirdIntList = new int[ARRAY_SIZE];

    long startTime, runTime;

    System.out.println("Creating sample arrays of " + ARRAY_SIZE + " items");

    for(int i = 0; i < ARRAY_SIZE; i++) {
    int item = (int)(Integer.MAX_VALUE * Math.random());
    firstIntList[i] = item;
    secondIntList[i] = item;
    thirdIntList[i] = item;
    }
    System.out.println("======================= DONE ===========================");


    // Sort using selectionSort sort
    System.out.println("Sorting using selection sort");

    startTime = System.currentTimeMillis();
    selectionSort(firstIntList);
    runTime = System.currentTimeMillis() - startTime;

    System.out.println("Selection sort completed in: " + runTime + " Milliseconds");
    System.out.println("======================= DONE ===========================");


    // Sort using insertion sort
    System.out.println("Sorting using insertion sort");

    startTime = System.currentTimeMillis();
    insertionSort(secondIntList);
    runTime = System.currentTimeMillis() - startTime;

    System.out.println("Insertion sort completed in: " + runTime + " Milliseconds");
    System.out.println("======================= DONE ===========================");


    // Sort using Arrays.sort()
    System.out.println("Sorting using in-built Arrays.sort()");

    startTime = System.currentTimeMillis();
    Arrays.sort(thirdIntList);
    runTime = System.currentTimeMillis() - startTime;

    System.out.println("Arrays.sort() sort completed in: " + runTime + " Milliseconds");
    System.out.println("======================= DONE ===========================");
    }
    }