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 *
```

Similar Questions

I want to write a method (returning a booleanvalue) nameditisSorted that takes two arguments; data: an integerarray n: the number of elements in the array and recursiv

I have an array of myObjects called arrayToFilter. One (element?) of myObject is an array of bezierpaths. I am comparing the bezierpath at a particular index (thispath) to a second path, and making fi

Given two ImagePlus or BufferedImages (I don't care) how can I match the histogram of the first to the second one? By matching I mean: Matching the cumulative distribution function (CDF) of one image

What is an easy way in Java to sort the array of elements maintaining information about indexes where the elements were stored in the original array? Is there a built-in function? There are few ways I

I've been following this tutorial: http://www.youtube.com/watch?v=cu-I2um024k and I'm trying to create a histogram using an array of data points. My code works fine when the data points are all positi

I have sorted array with numbers as keys, I need a reasonably fast alg to pick a key number which is holding value closest or identical (if exists) to given variable. If given value is higher than max

I have a sorted unique array and want to efficiently insert an element into it that is not in the array like this: a = [1,2,4,5,6] new_elm = 3 insert_at = a.bsearch_index {|x| x > new_elm } # =>

given an np.array of shape (n_days, n_lat, n_lon), I'd like to compute a histogram with fixed bins for each lat-lon cell (ie the distribution of daily values). A simple solution to the problem is to

I am working with a large dataset that I need to build a histogram of. I feel like my method of just going through the entire list and marking in a second array the frequency is a slow approach. Any s

First let me say that I am new to C so my approach is basic. I am attempting to check a sorted array for a point where it was rotated. For example (1 2 4 5 9) becomes (5 9 1 2 4). I am attempting to

Then I perform allValues I have array of strings, but they sorted alphabetically. How can I get array of objects sorted by their placement order ? Second question: how can I sort this objects for key

Possible Duplicate: Fastest strategy to form and sort an array of positive integers What would be the fastest way to get a sorted array from an unsorted iterable of integers ? Currently I do it by i

hey i tried to made a histogram that shows frames substraction, the code is running but i got gray window without result. the message on the command window is: Compiler did not align stack variables.

I need to estimate if the array list is sorted (don't sort). When Strings are sorted, they are in alphabetical order. I try to use compareTo() method to determine which string comes first And return

I have a non-associative array where the data that comes in is not sorted (I'm receiving the data from an outside system and cannot force it to come into the array in sorted order.) Is there any way t

I have the histogram for an image which i have calculated. I want to display this as an image so that I can actually see the histogram. I think my problem is to do with scaling although i am slightly

I am trying to locate the point of rotation in a sorted array through a modified binary search. Consider this array int values[9]={7, 8, 9, 1, 2, 3, 4, 5, 6}; The point of rotation here is at index =

I have an array that is already sorted in partitions of 4: 2, 23, 45, 55, 1, 4, 23, 74545, 75, 234, 323, 9090, 2, 43, 6342, 323452 What would be the most efficient way to sort this array? Note: the a

Find floor and ceil of a number X from an array which is already sorted. e.g. a[] = {3, 7, 9, 10, 15} if X=2, floor = N/A, ceil = 3 if X=3, floor = 3, ceil = 3 if X=4, floor = 3, ceil = 7 if X=16, f

Is there an optimized function in any numerical library (MKL, Boost, GSL,..etc) that searches a sorted array of floating point numbers for the closest match to a given float? Another function which wi

I already read this post but the answer didn't satisfied me Check if Array is sorted in Log(N). Imagine I have a serious big array over 1,000,000 double numbers (positive and/or negative) and I want

I know how to put an array in order, but in this case I just want to see if it is in order. An array of strings would be the easiest, I imagine, and answers on that front are appreciated, but an answe

I have a sorted array X[k]. Now I want to find I have tried this int ans=0; for(int i=1;i<=k;i++) { for(int j=i+1;j<=k;j++) { ans+=abs(X[i]-X[j]); } } I'm getting the correct answer by usin

I know how to create a histogram (just use with boxes) in gnuplot if my .dat file already has properly binned data. Is there a way to take a list of numbers and have gnuplot provide a histogram base

Hi I have an array of distances a= np.array([20.5 ,5.3 ,60.7 ,3.0 ], 'double') and I need the indices of the sorted array (for example [3, 1, 0, 2], for a.sort()). There is a function in numpy to do t

Here's the problem I've been trying to tackle for a few days. I need to write a program that gets a sorted array. The program will put 999 where two adjacent blocks have the same value, and then put a

I have to partially sort an array in descending order in which some numbers could be already sorted. Is their any function which could do it efficiently or any efficient algorithm.

Considering there is an array returned from a function which is of very large size. What will be the fastest approach to test this ? A simplest approach will be: /// <summary> /// Determines i

I am trying to make a pixel histogram for an image. I have an array called image that looks like this array([[[ 0, 0, 18, ..., 3, 0, 11], [ 5, 18, 0, ..., 16, 16, 11], [13, 0, 5, ..., 13, 19, 14], ..

I want to display some dates in the X axis of a histogram chart, but i don't understand how i can do it with this code i can create a simple histogram with couples of x-y values, but they can olny be

I am writing an easy program the just returns true if an array is sorted else false and I keep getting an exception in eclipse and I just can't figure out why. I was wondering if someone could take a

Possible Duplicate: Why is sorting modifing the original in this case? jsFiddle - I have 2 arrays. I want to set the second variable to the sorted version of the first. var _myArray = [N,T,A,

I'm working on a problem in C, and I have a quick question about it. The problem is as follows: I'm given some sorted array of integers, say, a[i] = { 1, 2, 3, 3, 3 }. Now, I am supposed to run a prog

I want to draw a JPEG coefficient histogram //coeff[] is the coefficients array int hist[]=new int[25]; for(int i=0;i<coeff.length;i++) hist[coeff[i]]++; now I want To draw hist array like bar cha

is there a way to sort an array and store the result in a linked list with the original array positions referenced by the list? so 0 bbb 1 aaa 2 ccc would become a linked list 1 aaa 0 bbb 2 ccc Than

Why $val is Array(1), but not the numeric value? I expected that $selected as $k => $val should return each line from the array $selected. Thus, $k must be a numeric key (it is) and $val must be co

I just need a little help here. I am doing an assignment where I need an Efficient Way To sort a 2-D integer Array of which row and column elements are already sorted in Ascending Order.(Preferable La

Question> Given two sorted array A and B, return an array C containing elements common to A and B. The array C cannot contain duplicates. Here is my solution, but my hunch is that it is wrong. But

There is a very expensive computation I must make frequently. The computation takes a small array of numbers (with about 20 entries) that sums to 1 (i.e. the histogram) and outputs something that I ca

Let's say we have this sorted array 0 1 1 1 1 2 2 2 2 2 3 10 10 10 I would like to find efficiently the positions where an element changes. For example in our array the positions are the following:

Since hist() of the base R does not report percentages (and the freq=FALSE) does not help either, I turned to lattice. histogram(rnorm(10000)) Please help me with the following: How can I get rid o

I'm plotting a histogram in gnuplot and the bars end up covering the tics in the x axis, like so: Is there any way to prevent this?

Via the starPrint method I need to make the frequency of each number populated in the array display in a histogram as such: 1=3*** 2=4**** 3=7******* and so on. It needs the number of stars populated

This is an interview question: given an integer x and a sorted array a[] of N distinct integers, design a linear-time algorithm to determine if there exists two distinct indices i and j such that a[i]

This question already has an answer here: Need an algorithm for a special array (linear field) 3 answers I have tried the following code to find out the minimum element in a cyclic sorted array

Suppose I run a 7/11, and the following 100x3 cell array sorted by the first column, time, is my sale records. 12:32:01 customer1 12 12:32:02 customer2 13 12:32:04 customer6 4 12:32:06 customer8 6 12:

In bash I'have a sorted integer array like: array[0]=1 array[1]=2 array[2]=3 array[3]=4 array[4]=7 array[5]=9 array[6]=10 array[7]=13 array[8]=15 array[9]=16 And I want to obtain output like: 1-4,7,

I'm using data of the form: [num1,num2,..., numk] (an array of integers). I would like to plot a histogram of a particular form, which I will use an example to describe. Suppose data = [0,5,7,2,3]. I

So I have a little problem. I have a data set in scipy that is already in the histogram format, so I have the center of the bins and the number of events per bin. How can I now plot is as a histogram.

What would be the quickest way to insert values into the correct position in a numpy sorted array. For example I would like to insert ever value of binto a: a = [1,1,2,4,7,7,11,13,13,13,15,20,25,26,27