Basic Java Sorting Algorithm (Selection/Insertion) help

n my Computer Science I class in high school, we have to make an array with 100 int values with a range of 1-1000:

Code Java:

public static void main(String[] args)
int list []=new int [100];
for(int i=0; i<list.length; i++)
{
list[i]=(Math.random()*1000+1)//
}

but our teacher left us on our own to find out how to do Sorting Algorithms. He wants us to use the Selection or Insertion sorting algorithms to sort the array.

If you could help me, that would be awesome

Re: Basic Java Sorting Algorithm (Selection/Insertion) help

See Sorting algorithm - Wikipedia, the free encyclopedia which has links to the sorting algorithms you mentioned, each page of which has pseudo-code you can work from and, more importantly, descriptions to understand the algorithm.

Re: Basic Java Sorting Algorithm (Selection/Insertion) help

Code Java:

public static void main(String[] args) {
int list[]=new int[100];
for (int i=0; i<list.length; i++)
{
list[i]=((int)(Math.random()*1000)+1);
}
for (int i=0; i<list.length-1; i++)
{
for (int j=i+1; j<list.length; j++)
{
if (list[ i ] > list[ j ])
{
int temp = list[ i ];
list[ i ] = list[ j ];
list[ j ] = temp;
}
}
}

Here is my code, all I get are symbols for output, what am I doing wrong?

Re: Basic Java Sorting Algorithm (Selection/Insertion) help

Quote:

all I get are symbols for output,

You will have to define what this means...what symbols? does it compile? Exceptions?

Re: Basic Java Sorting Algorithm (Selection/Insertion) help

As you have mentioned that you need basic sorting technique,you could use Bubble Sort.Check out http://c-madeeasy.blogspot.com/2011/07/various-sorting-techniques

Re: Basic Java Sorting Algorithm (Selection/Insertion) help

If your professor asked to figure it out on your own, your professor is probably going to ask you to write some pseudo code on an exam. The right way to tackle this algorithm is to watch a youtube video of someone demonstrating the algorithm so you understand it. Once you understand the algorithms it shouldnt take you more than 30 minutes to implement a working program.