-1

Here is the exercise: Create a class called SortingTest. In it, create a method that accepts an array of int values as a parameter, and prints out the elements sorted (smallest element first) to the terminal.

Part of this questions is also taking about using HashSets which are supposed to be used. How??

Here is what I have got so far

import java.util.Arrays;
import java.util.HashSet;

public class SortingTest {

  public void sortArrays(int numbers[]) {
    Arrays.sort(numbers);
    System.out.println(numbers);

  }
}

I think I am just not understanding the concept of what it is asking me to do.

wy94
  • 1
  • 8
  • 3
    Presumably, since it's a practice exercise, they want you to implement the sorting yourself. Also, that's [not how you print an array](http://stackoverflow.com/questions/409784/whats-the-simplest-way-to-print-a-java-array) – resueman Dec 08 '15 at 19:49
  • any hints as to how I would go about doing it then?? – wy94 Dec 08 '15 at 19:50
  • You could start out [here](https://en.wikipedia.org/wiki/Sorting_algorithm), pick an algorithm, and write the code that implements the algorithm described. An easy one (although slow) would be [bubble sort](https://en.wikipedia.org/wiki/Bubble_sort) – resueman Dec 08 '15 at 19:52
  • The bubble sort is the most straightforward. After you finish sorting it just print out the new array with `System.out.println(Arrays.toString(numbers));` – Jack Ryan Dec 08 '15 at 19:53

5 Answers5

1

Here is a little sample that should help you through it... Many ways to go about this but this is pretty simple using the built in java class.

import java.util.Arrays;

public class SortArray {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int integerArray[] = {1, 5, 3, 7, 8, 2};

        Arrays.sort(integerArray);

        for(int number : integerArray) {
            System.out.print(number + ", ");
        }

    }
}
S. Keney
  • 23
  • 6
  • All you would need to do is take the above code and apply it to a separate method that you will call in the main method. – S. Keney Dec 08 '15 at 20:07
0

First read how many types algorithms are there for sorting and try to implement them by your self.

Here is the link to Bubble Sort in Java.

Nimble Fungus
  • 513
  • 3
  • 22
0

If you do have to manually implement the sorting algo, then look up dual pivot quick sort which is what I believe Arrays.sort uses under the hood.

https://en.wikipedia.org/wiki/Quicksort

If you just have to dump out using Arrays.sort, you're almost there, but you'll need to loop to print out the values and actually call the method, which you should probably make static:

sorted = Arrays.sort(numbers);

for (int number : sorted) {
    System.out.println(number);
}
Greg
  • 11
  • 3
0

Seem like no one is talking about your HashSet question HashSet will remove duplicates in your array. So you will want to use HashSet if you do not want any duplicate number. For example if you have a sorted array like so

[2,3,5,5,9,11,14,15,20,20] 
sorted => [2,3,5,9,11,14,15,20]

If you have to create your own sorting algorithm then I suggest you try to implement one yourself before even searching for one. Basic idea of sorting to arrange numbers in increasing order or if you want decreasing order.
And like others said, to print an array you can use

Arrays.toString(myarray);

or loop through the elements and print values

for(int i = 0; i < myarray.length(); i++){
    System.out.print(myarray[i]+" ");
}
Fafore Tunde
  • 319
  • 2
  • 8
-1

You need to go through each element of the array with the help of a loop (like for) and in the loop you print each number.

for(int index = 0; index < numbers.length(); index++)
{
  System.out.println(numbers[index]);
}
t3s0
  • 272
  • 1
  • 2
  • 11
  • You can just print out the numbers with Java's built-in `toString()` method, it can also accept integer arrays as a parameter. – Jack Ryan Dec 08 '15 at 19:51
  • 1
    @JackRyan When talking about "Java's built-in `toString()` method", most people would assume you're talking about `Object.toString()` which naturally accepts no arguments whatsoever. – Kayaman Dec 08 '15 at 20:04
  • @Kayaman is right. So I don't really understand the reason for the -1. – t3s0 Dec 08 '15 at 20:12
  • I'm just saying why would you write the code to loop over a integer array to print its elements when you can type `System.out.println(Arrays.toString(numbers))`, especially because he is already using the Arrays class in his function. – Jack Ryan Dec 08 '15 at 20:25
  • @JackRyan Because he's doing an exercise where the idea is to learn the basics, such as loops, arrays and all sorts of things that become less relevant with experience. – Kayaman Dec 08 '15 at 20:34
  • It is always a good idea to test code snippets before you post them, then you would see, that your snippet won't work – Tom Dec 08 '15 at 21:54