01-21-2014

posted Jan 21, 2014, 8:44 AM by Samuel Konstantinovich   [ updated Jan 22, 2014, 5:09 AM ]
EDIT: Your results should be something along these lines...

For Array Size 100000

The builtin Sort Should be the fastest 
0.027 seconds on my really fast machine

selection Sort (should be approximately 2-4 times slower than insertion)
3.401 seconds on my really fast machine

insertion Sort (should be FASTER than selection) * My results in class were wrong, I had a bug in my sort! *
1.835 seconds on my really fast machine

bubble Sort (should be approximately 4-6 times slower than slection)
15.427 seconds on my really fast machine

radix Sort (Optimally** should be 4-8 times longer time than builtin...)
0.084 seconds on my really fast machine

**If you did MUCH better than selection/insertion with radix, that is fine.





Your goal is to run this bash script on your code, and generate some test results. 


Here is the batch script:
#!/bin/bash
echo Array Sizes:
types=(builtin selection insertion bubble radix)
for i in `seq 1 10`;
do
    let x=$i*10000
    echo $x
done

for n in `seq 0 4`;
do
    echo ${types[$n]} Sort
    for i in `seq 1 10`;
    do
        let x=$i*10000
        java -Xmx1g Tester ${types[$n]} 10000000 $x
    done
done



Here is your java wrapper class, you must add your sort functions into the appropriate methods.

import java.util.*;
public class Tester{
    public static void main(String[]args){
        try{
            String type = args[0];
            int max = Integer.parseInt(args[1]);
            int size = Integer.parseInt(args[2]);
            int[]L = makeRandomArray(size,max);

            long startTime = System.currentTimeMillis();
            sort(L,type,size+1);
            long stopTime = System.currentTimeMillis();
            long elapsedTime = stopTime - startTime;

            if(isSorted(L)){
                System.out.println(elapsedTime/1000.0);
            }else{
                System.out.println("Failed to sort");
            }
        }catch(Exception e){
            System.out.println("usage: Test <TYPE> <MAX_VALUE> <ARRAY_SIZE>\n");
            e.printStackTrace();
        }
    }

    public static int[] makeRandomArray(int size, int max){
        int[]ary = new int[size];
        for(int i=0;i<size;i++){
            ary[i]=(int)(max*Math.random());
        }
        return ary;
    }

    public static void sort(int[]L,String type, int max){
        if(type.equals("bubble")){
            Bubble(L);
        }else if(type.equals("radix")){
            radixsort(L);
        }else if(type.equals("insertion")){
            Insertion(L);
        }else if(type.equals("selection")){
            Selection(L);
        }else{
            Arrays.sort(L);
        }
    }

    public static boolean isSorted(int[]L){
        if(L==null || L.length<2){
            return true;
        }
        for(int i=0; i<L.length-1;i++)
            if(L[i]>L[i+1]){
                return false;
            }
        return true;
    }
    public static void Selection(int[]L){
    /*put in your code*/
    }

    public static void Insertion(int[]L){
    /*put in your code*/
    }

    public static void Bubble(int[]L){
    /*put in your code*/
    }

    public static void radixsort(int[]d){
    /*put in your code*/
    }

}



Comments