Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

# Thread: Basic Java Sorting Algorithm (Selection/Insertion) help

1. ## 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:

```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

2. ## 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.

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

```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?

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

all I get are symbols for output,
You will have to define what this means...what symbols? does it compile? Exceptions?

5. ## 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

6. ## 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.