Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sort array from smallest to largest using java

I'm supposed to create an array and sort the numbers from smallest to largest. Here is what I have so far:

public class bubbleSort {

public static void sort (int [] arrayName){
    int temp;
    for (int i = 0; i < arrayName.length-1; i++)
    {
        if(arrayName[i] > arrayName[i+1])
        {
            temp=arrayName[i];
            arrayName[i]=arrayName[i+1];
            arrayName[i+1]=temp;
            i=-1;
        }
    }
}

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

    System.out.println(sort(arrayName)); 
}
}

I am getting an error on the last line where I'm trying to print it out. What am I doing wrong?

like image 324
Bryan Avatar asked Jan 19 '12 19:01

Bryan


People also ask

How do you sort an array from smallest to largest?

Selection sort performs the following steps to sort an array from smallest to largest: Starting at array index 0, search the entire array to find the smallest value. Swap the smallest value found in the array with the value at index 0. Repeat steps 1 & 2 starting from the next index.

How do you arrange an array in ascending order in Java?

We can sort arrays in ascending order using the sort() method which can be accessed from the Arrays class. The sort() method takes in the array to be sorted as a parameter. To sort an array in descending order, we used the reverseOrder() method provided by the Collections class.

Can you sort an array in Java?

You can also sort an array in Java with user-defined methods using the comparator interface and for a loop. All you have to do is define the logic in the method such that it will sort the array. Please look at the below example, where you will have to sort an array without using the Arrays. sort() method in Java.


3 Answers

Your sort(int[] array) method returns nothing. It is void, therefore you cannot print its return.

like image 160
Alex Avatar answered Oct 04 '22 13:10

Alex


You need to iterate over the array and print out each value. You cannot just println(<array>). Instead, try:

// sort the array
sort(arrayName);
for( int sortedValue : arrayName )
  System.out.println( sortedValue );

That will iterate over each element in the array and print it out.

You can also use commons-lang's ArrayUtils.toString() method to do this all automatically for you, but I am assuming that since this is a homework assignment, you cannot just use external libraries to do your work for you.

like image 30
Eric B. Avatar answered Oct 04 '22 13:10

Eric B.


Maybe you can use lambdaj (download here,website), this library is very powerfull for managing collections (..list,arrays), the following code is very simple and works perfectly:

import static ch.lambdaj.Lambda.on;
import static ch.lambdaj.Lambda.DESCENDING;
import static ch.lambdaj.Lambda.sort;
import java.util.Arrays;
import java.util.List;

public class Test {
    public static void main(String[] args) {
        List<Integer> numberList =  Arrays.asList(4,8,2,3,4,1,13,2,5);

        List<Integer> sortedList = sort(numberList, on(Integer.class));
        System.out.println(sortedList); //shows ascending list

        sortedList = sort(numberList, on(Integer.class), DESCENDING);
        System.out.println(sortedList); //shows descending list
    }
}

This code shows:

[1, 2, 2, 3, 4, 4, 5, 8, 13]
[13, 8, 5, 4, 4, 3, 2, 2, 1]

In one line you can sort a list, this is a simple example but with this library you can resolve more.

sort(numberList, on(Integer.class));

You must add lambdaj-2.4.jar to your project. I hope this will be useful.

Note: This will help you assuming you can have alternatives to your code.

like image 23
Gaston Flores Avatar answered Oct 04 '22 14:10

Gaston Flores