-1

I will in this method descending order number integer

package sortarray;

public class start {

    public static void main(String[] args) {

        int [] numb={10,12,8,6,2};

        sortarray(numb);
    }

    public static void sortarray(int [] input){

        int max=input[0];

        int [] sortmax=input;//i don't know how this array sortmax initialized first

        for (int i=0;i<input.length;i++)
            if(max<input[i]){
                max=input[i];

                sortmax[i]=max;//this array is not work 
            }

        for (int j=0;j<sortmax.length;j++)
            System.out.print(" "+sortmax[j]);
    }           

}

but into this method, (sortmax) is not work why?

Markus
  • 3,225
  • 6
  • 35
  • 47
farzad
  • 1
  • 1

2 Answers2

1

I understand you want to keep some kind of history of max'es?

sortmax is just referencing to your input array, everything you do on sortmax you do on input. You need to do this:

int[] sortmax = new int[input.length];

instead of int[] sortmax = input;.

Shadov
  • 5,421
  • 2
  • 19
  • 38
0

You can always use the Arrays.sort() for this, but if the goal is to learn sorting, a brute force (slow for large vectors) sorter could look like this:

public void sort(int[] array){
   int[] tempArray = new int[array.length]
   int max;
   int index;
   for(int k = 0; k < array.length; k++){
     for(int i = 0; i < array.length; i++){
       if(max < array[i]){
         max = array[i];
         index = i;
        }
     }
     tempArray[k] = array[index];
     array[index] = Integer.MIN_VALUE;
   }
   array = tempArray;
}
Ozgar
  • 305
  • 2
  • 14