-1

I have working code for sort methods with time analysis code in the end, but can't figure out where to put the random number or array generator code for the sort methods. I get errors each time I try, hopefully someone can show me how it looks when the random method is called instead of the default assign. Also, is the timing part done right? It doesn't seem like it should take that long, when I run I get something like 69391841667800 nanoseconds.

public class InsertSort {
private int[] arr;
public InsertSort(int[] array) {
arr = array;
}
private boolean more(int value1, int value2)
{
return value1 > value2;
}
public void sort()
{
int size = arr.length;
int temp,j;
for(int i=1; i<size; i++)
{temp=arr[i];
for(j=i; j>0 && more(arr[j-1], temp); j--)
{
arr[j]=arr[j-1];
}
arr[j]=temp;
}
}
public static void main(String[] args)
{
int[] array = {9,1,8,2,7,3,6,4,5};
InsertSort bs = new InsertSort(array);
bs.sort();
for(int i=0;i<array.length ;i++)
{
System.out.print(array[i] + " ");
}
long endTime = System.nanoTime(); //Current system Time at end

long startTime = 0;
long duration = (endTime - startTime); //divide by 1000000 to get milliseconds.

System.out.print(duration);
}
}

This is my code for random number generator

import java.util.ArrayList;

public class Random {

public static void main(String[] args)
{
    System.out.println(generateRandomList(5, 1, 10));
}

public static ArrayList<Integer> generateRandomList( int size, int min, int max) {
    ArrayList<Integer> list;
    list = new ArrayList<>();
    for(int i=0;i<size;i++) {
        int n = (int)(Math.random() * (max-min))+min;
        list.add(n);
    }
    return list;
}

}
JavaNub
  • 13
  • 4
  • 1
    Please [edit] the post and format the first code block properly. – Turing85 May 16 '21 at 18:44
  • 2
    What errors do you get? We can't help you if you don't show what the issue is – Kevin Hooke May 16 '21 at 18:46
  • Turing not sure what you mean, I used the insert code feature here. – JavaNub May 16 '21 at 18:48
  • Kevin, basically I am not sure how to insert the random method into the insert method. I am supposed to test for timing of sorting methods for random number and array generators. When I try, I get errors such as - Multiple markers at this line - Syntax error, insert ")" to complete MethodDeclaration - Syntax error, insert "Identifier (" to complete MethodHeaderName - Syntax error, insert "SimpleName" to complete QualifiedName - Syntax error on token ".", @ expected after this token at System.out.print(duration); – JavaNub May 16 '21 at 18:55
  • Question [re-asked](https://stackoverflow.com/q/67767398/522444) and without formatting improvement – Hovercraft Full Of Eels May 31 '21 at 03:16

1 Answers1

0

You need to replace

int[] array = {9,1,8,2,7,3,6,4,5};

in your first main method with call to generateRandomList like this

int[] array = Random.generateRandomList(5, 1, 10);

If you are not using IDE you may need tonadd you Random class to import section of your main class.

And measuring execution time for java code is very non-trivial. Please check the following link for more information https://www.baeldung.com/java-microbenchmark-harness

Ivan
  • 8,508
  • 2
  • 19
  • 30
  • I tried by the I get the error - Type mismatch: cannot convert from ArrayList to int[] – JavaNub May 16 '21 at 19:52
  • @JavaNub you need to change `generateRandomList` to return array of numbers or change `InsertSort` to accept list instead of array – Ivan May 16 '21 at 20:12
  • I tried changing generateRandomList to this but still get same error (sorry I cannot make it code format in comments) - import java.util.ArrayList; public class Random { public static void main(String[] args) { System.out.println(generateRandomList(5, 1, 10)); } public static ArrayList generateRandomList( int size, int min, int max) { ArrayList array; array = new ArrayList<>(); for(int i=0;i – JavaNub May 16 '21 at 20:28
  • To change InsertSort to accept list instead of array, would I just change every "array" to "list" in that class? – JavaNub May 16 '21 at 20:30
  • Yes, and usually lists are easier to deal with then arrays – Ivan May 17 '21 at 00:01
  • I tried changing every "array" to "list" in the InsertSort class , and presuming I have to change from arraylist to list in the Random class, but then when I do so for the Random class I get the error "Cannot instantiate the type List", and still get the same error in the timing code "Type mismatch: cannot convert from ArrayList to int[]" – JavaNub May 30 '21 at 06:53