This is an example. Write the name of your array in the word list.

bool arrange(int length){

  for(int i=0;i<length;i++)
  {
	if(list[i]>list[i+1])
		swap(list[i],list[i+1]);

	

    }

  for(int i=0;i<length;i++)cout<<"my new list contains "<<list [i]<<endl;


return true;
}

alexchen
1. We don't just give people the code for their homework - that teaches them nothing, and
2. I guess you hadn't realised that this is a Java forum?

import ij.ImagePlus;
import ij.plugin.filter.PlugInFilter;
import ij.process.ImageProcessor;
import ij.process.ByteProcessor;

public class Create_New_Image implements PlugInFilter {
	String title = null;

	public int setup(String arg, ImagePlus im) {
		title = im.getTitle();
		return DOES_8G + NO_CHANGES;
	}

	public void run(ImageProcessor ip) {
		int[] hist = ip.getHistogram();
		int w = 256;	// width of the histogram image
		int h = hist.sort();


		// create the histogram image:
		ImageProcessor histIp = new ByteProcessor(w,h);
		histIp.setValue(255);	// white = 255
		histIp.fill();	// clear this image



		for (int v = 0; v < h; v++) {
			for (int u = 0; u < w; u++) {
				ip.getPixel(u, v);
				histIp.putPixel(u, v, 0);

			}
		}




		//display the histogram image:
		String hTitle = "Histogram of " + title;
		ImagePlus histIm = new ImagePlus(hTitle, histIp);
		histIm.show();
			// histIm.updateAndDraw();
	}

}	// end of class Create_New_Image

This is my codes. I'm actually writing a plugin for ImageJ. I need to get the largest number in my array hist (line 15),and equate it to my variable h (line 17).

Edited 5 Years Ago by Sunshineserene: n/a

Just use one pass of the bubble sort algorithm as hinted at in the code in previous post. The larget value in the array will be in the last position of the array after one pass, the second largest in second last position after two passes etc..

Start with a variable = largest negative int.
Loop thru array and if any element is > that value, replace the value with the array element.
At the end of the loop the variable will contain the largest value in the array.

put in a max value and set the first number entered in the max and then compare the numbers in the array to the max value and put in an if statement:

if (max < array[i]){
max = array[i]
}

masijade's suggestion of arrays.Sort() is the easiest. largest number will be at array[array.length - 1].

public void run(ImageProcessor ip) {
		int[] hist = ip.getHistogram();
		int w = 256;	// width of the histogram image
		int h = Arrays.sort(hist);

This is what I got so far, however I got error saying: incompatible types. Required int, but found void. I think it's cos of the ip. How can I get the largest number instead?

Because Arrays.sort doesn't return anything. It sorts the array. After calling that simply take the last element of the array.

As said a few posts ago ...

Object[] bogusArray = { /* some Objects */ };
Object someElement = bogusArray[bogusArray.length - 1];
public void run(ImageProcessor ip) {
		int[] hist = ip.getHistogram();
		int w = 256;	// width of the histogram image
		int h = hist[hist.length-1];

Yes I did that. But there were lots of exception errors. ):

public void run(ImageProcessor ip) {
		int[] hist = ip.getHistogram();
		int w = 256;	// width of the histogram image
		int h1 = Arrays.sort(hist);
		int h = hist[hist.length-1];

Is this correct? Now the only error is the incompatible types for line 4.

Don't forget that sorting your actual histogram data will screw up your histogram - you need to sort a copy of the array.
Personally think that sorting a copy an array just to get the largest value is overkill compared to simply doing one pass thru the original and saving the largest value found, IMHO.

Well don't assign the result of Arrays.sort to anything.

And, yes, it is overkill, but, unless the size of the array is huge, or the step needs to be done a lot in a short period of time, it is easier to just use that then having to start creating your own "utility" methods for doing all these sorts of things (or "reinventing the wheel" everytime you need to do something like it).

Also, I usually have to do this "highest" sort of thing in simulation programs where the "highest" is not what I am searching for, but rather a niveau % index (i.e. say the niveau percent is 97% and the array has 100 elements, then I am searching for the 3rd highest value) where the niveau is paramaterised, as is the size of the array. And for this sort of thing a sort comes in really handy.

Edit: In this case, however, considering it is a histogram, I would also use a loop and the Math.max() method.

int h = Integer.MIN_VALUE;
for (int i : ip.getHistogram()) { h = Math.max(h, i); }

Edited 5 Years Ago by masijade: n/a

Well don't assign the result of Arrays.sort to anything.

A "senior moment"? Arrays.sort sorts the array you pass to it and has return type null. Hence my concern that it changes the actual source data.

As for nth highest, median etc, I fully agree with you.

Comments
I guess I deserved that. ;-)

Well don't assign the result of Arrays.sort to anything.

A "senior moment"? Arrays.sort sorts the array you pass to it and has return type null. Hence my concern that it changes the actual source data.

Nope, that was in answer to the post before yours concerning the compiler error message the OP was getting. ;-)

The rest of the post was for you though. I probably should have marked those as "@OP" and "@James". ;-)

Hi all, do you guys have any idea how to construct a histogram? The problem I'm facing now is to draw the histogram values as black bars.

public void run(ImageProcessor ip) {
		//int[] hist = ip.getHistogram();
		int w = 256;	// width of the histogram image
		//int h1 = Arrays.sort(hist);
		//int h = hist[hist.length-1];

		int h = Integer.MIN_VALUE;

		for (int i : ip.getHistogram())	{
			h = Math.max(h, i);
		}



		// create the histogram image:
		ImageProcessor histIp = new ByteProcessor(w,h);
		histIp.setValue(255);	// white = 255
		histIp.fill();	// clear this image



		for (int v = h-1; v < h-1; v--) {
			for (int u = w-1; u < w-1; u--) {
				histIp.getPixel(u, v);
				histIp.putPixel(u, v, 0);

			}
		}

This is what I have so far. I don't think my nested for loop is correct. =/

This article has been dead for over six months. Start a new discussion instead.