# histogram of sorted array

I had to make a histogram of a given array, the frequency of the numbers in the array should be marked with *.

My program works, the problem is that the array isn't sorted correctly if there is a negative number in the array.

```    public class Histogram {

//number in the counter is shown as *
private static String convertToStars(int num){
StringBuilder builder = new StringBuilder();
for(int j = 0; j < num; j++){
builder.append('*');
}
return builder.toString();
}

public static void outputHistogram(Integer[] array) {

//if array is empty
if(array.length == 0){
System.out.println("Keine Elemente vorhanden.");
return;
}

//array is cloned, so it is possible to delete same numbers
Integer[] copy = array.clone();
Arrays.sort(copy);
System.out.println(Arrays.toString(copy) + "\n");

for(int i = 0; i < copy.length; i++){
int counter = 1;

for(int j = 0; j < copy.length; j++){

if(i != j && array[i] == array[j]){
counter++;
copy[j] = null;
}

}

if(copy[i] != null){
System.out.println("\t" + array[i] + "\t" + convertToStars(counter));
}
}
}

public static void main(String[] args) {
Integer[] array = {2, 4, 23, 23, 23, 2, -8, 56, 4, 2};
Histogram h = new Histogram();
System.out.println("Histogramm des Arrays: " );
h.outputHistogram(array);
}
```

}

The negative ones should be sorted before the positive numbers. It wasn't possible to upload an image. Anyway, thanks for helping.

You seem to have interchanged the uses of array and copy within your nested loops.

You keep the inplaced sorted copy for printing purpose and manipulate your original array and the controlling variable, else you lose the information of in placed sorted array.

```for (int j = 0; j < copy.length; j++) {

if (i != j && copy[i] == copy[j]) {
counter++;
array[j] = null;
}

}

if (array[i] != null) {
System.out.println("\t" + copy[i] + "\t"
+ convertToStars(counter));
}
```

Gives the correct output,

```Histogramm des Arrays:
[-8, 2, 2, 2, 4, 4, 23, 23, 23, 56]

-8  *
2   ***
4   **
23  ***
56  *
```