6

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?

2
  • 1
    what is the error you are getting? Commented Jan 19, 2012 at 19:23
  • 2
    Your sort 'function' isn't returning anything Commented Jan 19, 2012 at 19:25

9 Answers 9

15

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

Sign up to request clarification or add additional context in comments.

4 Comments

I suggest running through arrayName with a for-loop and printing out each index.
Ah, so I need to change it to int instead of void. How do I get it to return all numbers of the array as oppose to just the value in temp or some other individual value?
The way you have it is fine, your variable arrayName is now sorted. You just need to rifle through its values now, and print them each :)
Just call sort(array) on its own line, and then print out the array elements one by one.
3

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.

Comments

2

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.

Comments

1

For learning purpose writing your own sort function is fine but for production code always use java API Arrays.sort

Comments

0

You need to change your sort method--it is returning nothing.

public static void is for a method where you are not returning anything. Try this:

public static int sort (int[] arrayname)

Comments

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

Comments

0

This is the "cleaner" way to do this (I think):

    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]);
    }

    }

Hope this help!

Comments

0

Array Sorting without using built in functions in java ......just make new File unsing this name -> (ArraySorting.java) ..... Run the Project and Enjoy it !!!!!

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]);
    }

}

}

Comments

-2

Create File with java Extentation.i-e(ArraySorting.java) then Paste the Code....

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]);
        }

}

}

4 Comments

I'm only guessing, but I think the OP was trying to write their own sorting algorithm rather than using an off-the-shelf solution.
This is a simple Java Coding Dude :/ ....try to this Link....stackoverflow.com/questions/8931977/…. thankx---
Quite. I would imagine it's somebody that's learning how to program, and when it becomes 'simple' to them then they should use library methods as you have, but jumping to the answers section is never a good way to learn
ok i understant.. Arrays.sort(a); is a library of java for sorting.....

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.