Sort array from smallest to largest with java

I have to create an array and sort the numbers from smallest to largest. Here is what I still have:

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 get an error in the last line where I try to print it. What am I doing wrong?

+5
source share
9 answers

Your method sort(int[] array)returns nothing. This is not valid, so you cannot print its return.

+15
source

You need to iterate over the array and print each value. You cannot just println (<array>). Try instead:

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

This will iterate over each element of the array and print it.

commons-lang ArrayUtils.toString(), , , , , .

+3

, lambdaj ( , website), (..list, ), :

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

:

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

, , .

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

lambdaj-2.4.jar . , .

. , .

+2

, API Java Arrays.sort

+1

- .

public static void , . :

public static int sort (int[] arrayname)
0
public static int[ ] arraySortUp( int[ ] intArray )
{
        int toSwap, indexOfSmallest = 0;
        int i, j, smallest;

        for( i = 0; i < intArray.length; i ++ )
        {               

            smallest = Integer.MAX_VALUE;

            for( j = i; j < intArray.length; j ++ )
            {
                if( intArray[ j ] < smallest )
                {
                    smallest = intArray[ j ];
                    indexOfSmallest = j;
                }                   
            }

            toSwap = intArray[ i ];
            intArray[ i ] = smallest;
            intArray[ indexOfSmallest ] = toSwap;
        }

        return intArray;
}       
0

" " ( ):

    public static void main(String[] args) throws IOException {

    int[] array = {1,4,2,8,4,7,5 /*put in the numbers you want to sort*/};

    Arrays.sort(array); /*You will need to import this function*/

    for (int i = 0; i < array.length; i++) {
       System.out.println(array[i]);
    }

    }

!

0

java ...... , → (ArraySorting.java)..... !!!!!

import java.io.*;
import java.util.Arrays;
import java.util.Scanner;
public class ArraySorting 
{
 public static void main(String args[])
 {
    int temp=0;   
    Scanner user_input=new Scanner(System.in);
    System.out.println("enter Size elements...");
    int Size=user_input.nextInt();

    int[] a=new int[Size];
    System.out.println("Enter element Of an Array...");
    for(int j=0;j<Size;j++)
    {
        a[j]=user_input.nextInt();
    }     
    for(int index=0;index<a.length;index++)
    {
        for(int j=index+1;j<a.length;j++)
        {
             if(a[index] > a[j] ) 
             {
                 temp = a[index];
                 a[index] = a[j];
                 a[j] = temp;
             }
        }
    }
    System.out.print("Output is:- ");
    for(int i=0;i<a.length;i++)
    {
        System.out.println(a[i]);
    }

}

}

0

java Extentation.i-e (ArraySorting.java), ....

import java.io.*;
import java.util.Arrays;
import java.util.Scanner;
public class ArraySorting 
{


public static void main(String args[])
{
       Scanner user_input=new Scanner(System.in);

       System.out.println("enter Size elements...");
       int Size=user_input.nextInt();

       int[] a=new int[Size];
       System.out.println("Enter element Of an Array...");
        for(int j=0;j<Size;j++)
        {
            a[j]=user_input.nextInt();
        }

        Arrays.sort(a);       
        for(int index=0;index<a.length;index++)
        {
            System.out.println(a[index]);
        }

}

}

-2

All Articles