1

I'm writing a program that times the runtimes of different sorting algorithms by creating a random array of integers and sorting it with five different algorithms. I need to make a deep copy of my original array so that when I run the next sort algorithm it's not sorting the already sorted array (thus affecting my test results).

Here is my code:

import java.io.*;
import java.util.*;

class SortingTest
{   
   static int runtime=0;
   static int start=0;
   static int stop=0;

    /**@param start the time when the algorithm started sorting
    @param stop the time when the algorithm finished sorting
    @return runtime the total time it took for the sorting algorithm to sort
    a function that calculates the runtime of the sorting algorithm*/
    public static int findruntime(int start, int stop)
    {
        return runtime=stop-start;
    }

    /**@param A the array containing the numbers to be sorted
    @param n the length of the array
    a function that runs bubble sort and prints the run time*/
    public static void runbubble(Integer [] A, int n)
    {
        start=(int)System.currentTimeMillis();
        Sorts.bubble(A);
        stop=(int)System.currentTimeMillis();

        if (n<=100)
        {
            for(int i=0; i<A.length; i++)
            {
                System.out.print(A[i]+" ");
            }
            System.out.println();
        }

        findruntime(start,stop);
        System.out.println("The running time for Bubble Sort is "+ runtime+ " milliseconds.");
    }

    /**@param A the array containing the numbers to be sorted
    @param n the length of the array
    a function that runs selection sort and prints the run time*/
    public static void runselection(Integer [] A, int n)
    {
        start=(int)System.currentTimeMillis();
        Sorts.select(A);
        stop=(int)System.currentTimeMillis();

        if (n<=100)
        {
            for(int i=0; i<A.length; i++)
            {
                System.out.print(A[i]+" ");
            }
            System.out.println();
        }

        findruntime(start,stop);
        System.out.println("The running time for Selection Sort is "+ runtime+" milliseconds.");
    }

    /**@param A the array containing the numbers to be sorted
    @param n the length of the array
    a function that runs insertion sort and prints the run time*/
    public static void runinsertion(Integer [] A, int n)
    {
        start=(int)System.currentTimeMillis();
        Sorts.insertion(A);
        stop=(int)System.currentTimeMillis();

        if (n<=100)
        {
            for(int i=0; i<A.length; i++)
            {
                System.out.print(A[i]+" ");
            }
            System.out.println();
        }

        findruntime(start,stop);
        System.out.println("The running time for Insertion Sort is "+ runtime+" milliseconds.");
     }

     /**@param A the array containing the numbers to be sorted
    @param n the length of the array
    a function that runs quick sort and prints the run time*/
    public static void runquick(Integer [] A, int n)
    {
        start=(int)System.currentTimeMillis();
        Sorts.quick(A);
        stop=(int)System.currentTimeMillis();

        if (n<=100)
        {
            for(int i=0; i<A.length; i++)
            {
                System.out.print(A[i]+" ");
            }
            System.out.println();
        }

        findruntime(start,stop);
        System.out.println("The running time for Quick Sort is "+ runtime+" milliseconds.");
    }

    /**@param A the array containing the numbers to be sorted
    @param n the length of the array
    a function that runs shell sort and prints the run time*/
    public static void runshell(Integer [] A, int n)
    {
        start=(int)System.currentTimeMillis();
        Sorts.shell(A);
        stop=(int)System.currentTimeMillis();

        if (n<=100)
        {
            for(int i=0; i<A.length; i++)
            {
                System.out.print(A[i]+" ");
            }
            System.out.println();
        }

        findruntime(start,stop);
        System.out.println("The running time for Shell Sort is "+ runtime+" milliseconds.");
    }

    public static void main (String [] args)
        throws IOException
    {
        System.out.println("Welcome to the Sorting Algorithms Speed Test!");
        System.out.print("Please enter a value for n:");

        Scanner cin = new Scanner(System.in);
        int n= cin.nextInt();

        //create array of size n
        Integer[] list1= new Integer[n];

        //generate and fill in the array
        for(int i=0; i<list1.length; i++)
        {
            int number= (int)(1+n*Math.random());
            list1[i]=number;
        }

        //print if n<=100
        if (n<=100)
        {
            for(int i=0; i<list1.length; i++)
            {
                System.out.print(list1[i]+" ");
            }
            System.out.println();
        }
    // MUST FIGURE OUT A WAY SO THAT THE ORIGINAL ARRAY LIST DOES NOT GET SORTED AS WELL

        //using bubble sort
        /*runbubble(list1,n);


         //TESTING ONLY
         System.out.println("This is to make sure that the original array was never tampered with..."); 
        for(int i=0; i<list1.length; i++)
        {
            System.out.print(list1[i]+" ");
        }
        System.out.println();

    //using selection sort
    runselection(list1,n);

    //TESTING ONLY
    System.out.println("This is to make sure that the original array was never tampered with..."); 
    for(int i=0; i<list1.length; i++)
        {
            System.out.print(list1[i]+" ");
        }
        System.out.println();

    //using insertion sort  
    runinsertion(list1,n);

    //TESTING ONLY
    System.out.println("This is to make sure that the original array was never tampered with..."); 
    for(int i=0; i<list1.length; i++)
        {
            System.out.print(list1[i]+" ");
        }
        System.out.println();

    //using quick sort
    runquick(list1,n);

    //TESTING ONLY
    System.out.println("This is to make sure that the original array was never tampered with..."); 
    for(int i=0; i<list1.length; i++)
        {
            System.out.print(list1[i]+" ");
        }
        System.out.println();*/

    //using shell sort
    runshell(list1,n);

    //TESTING ONLY
    System.out.println("This is to make sure that the original array was never tampered with..."); 
    for(int i=0; i<list1.length; i++)
        {
            System.out.print(list1[i]+" ");
        }
        System.out.println();
}

}

Megumi Ai
  • 89
  • 2
  • 9

1 Answers1

3

You should use System.arrayCopy()

So when you declare your Integer Array

//create array of size n
Integer[] list1 = new Integer[n];
Integer[] copyList1 = new Integer[n];

So you're creating 2 arrays then once you've populated your list1 with values you can call

System.arraycopy(list1 ,0 ,copyList1 ,0 ,list1.length);

which will copy all the values from list1 into copyList1, obviously you can give things better names.

Andrew Aitken
  • 671
  • 3
  • 14