> Trying to write a program that prints out 100 numbers and time how long it takes to do it.?

Trying to write a program that prints out 100 numbers and time how long it takes to do it.?

Posted at: 2014-12-18 
I'm not sure how you are calculating your time in that program but there are several approaches to this problem.

The first issue is that you're setting every value to 0, but saying N[a] = N[0] = 0. Then c = N[a] = 0. Then N[b] = c = 0.

For random numbers, either increasing or anything, you can use a loop to initialize the elements of the array to the loop counter + some other value. Another is to assign each index of the array to a (int)Math.random()

To count time you can use Javas built in " java.lang.System " and use the methods currentTimeMillis() or nanoTime() depending on your needs, and use the two of them one before the method and the other after and calculate the different in those 2 times to get the time to sort.

For sorting, There are many algorithms you can use, if you can use Javas built in sort() that uses quicksort which is significantly faster than bubblesort or selection sort. You can program your own unique method as well and compare times.

The program is mostly written out, except it is only printing out only zeros, but I want it to print out numbers I either enter in or random numbers, and then I want it to time how long it takes to put those numbers in order. This is the program I already have written out:

import java.util.Scanner;

public class numbers

{

public static void main (String[] args)

{

Scanner in = new Scanner(System.in);

int []N= new int [1000];

int a, b, c;

for(a=0; a<=998; a++)

{

for(b=a+1; b<=999; b++)

{

if(N[a] < N[b])

{

c=N[a];

N[a]=N[b];

N[b]=c;

}

}

}

for(a=0; a<=999; a++)

{

System.out.println(N[a]);

}



}

}