Sorting integers in an array from largest to smallest. Negative ints not working

Recently I created a program that takes the ints inside of an array and puts them in another array from largest to smallest, but it doesn't work when you insert negatives. It prints a 0 rather than the negative integer.

import java.util.Arrays;

public class g2lArrays2 {

    public static void main(String[] args) {
        int[] nums = {15,31,7,2,7};
        int[] place = new int [nums.length];
        int[] definer = new int [nums.length];
        int a = 0,b = 0,c;
        place[0] = a;
        place[1] = 1;
        for (int i1 = 0; i1<place.length; i1++) {
        for (int i = 0; i<nums.length; i++) {
            if (nums[i] > a) {
                place[i1]=nums[i];
                a=place[i1];
                definer[i]=1;
            }
        }
        a = 0;
        for (c = definer.length-1; b==0 && c>-1; c--) {
            if (definer[c]==1) {
                b=c;
            }
        }
        nums[b]=0;
        b=0;
        for (int i2 = 0; i2<definer.length; i2++) {
            definer[i2]=0;
        }
        }
        System.out.println(Arrays.toString(place));
    }
}

It works otherwise, but like I've stated, just not with negative integers. So I'm curious about what I'm doing wrong and if there's a more efficient way to sort this program. All feedback is appreciated.

2 answers

  • answered 2017-12-06 01:22 GCP

    If you want to sort existing array in descending order without creating a new one:

    Integer[] arr = {1,2,3,4};
    Arrays.sort(arr, Collections.reverseOrder());
    

    arr will become [4,3,2,1]

    Or if you want to make a new one without cloning the existing one:

     Integer[] arr = {1,2,5,3,4};
     Arrays.sort(arr, Collections.reverseOrder());
     Integer[] newArray = new Integer[arr.length];
    
     for (int i = arr.length - 1, j = 0; i >= 0; i--, j++){
          newArray[j] = arr[i];
     }
    
     System.out.println(Arrays.toString(arr));
    

  • answered 2017-12-06 03:04 BarriaKarl

    Okay i think FairPlay answer is totally the best way to sort a array. That said you asked why your code was having problems and that is what i will try to explain here.

    First you are printing the array named place and so i will only comment about the places where the code involves that specific array.

    this is the parts in your code that affect the place array:

    public static void main(String[] args) {
        int[] nums = {15,31,7,2,7};
        int[] place = new int [nums.length];
        int[] definer = new int [nums.length];
        int a = 0,b = 0,c;
        place[0] = a;
        place[1] = 1;
        for (int i1 = 0; i1<place.length; i1++) {
        for (int i = 0; i<nums.length; i++) {
            if (nums[i] > a) {
                place[i1]=nums[i];
                a=place[i1];
                definer[i]=1;
    

    Okay first lets clear this. First things to pay attention is correct identation and create lines breaks whenever you think it is good. Second, when creating a variable that will only be used in a loop or inside a for name them i, j or k and repeat them if possible (this is something i do so it is more of a tip than a rule).

    Here is the clean code:

    public static void main(String[] args) {
    
        int[] nums = {15,31,7,2,7};
        int[] place = new int [nums.length];
        int[] definer = new int [nums.length];
    
        int a = 0,
        int b = 0;
    
        place[0] = a;
        place[1] = 1;
    
        for (int i = 0; i < place.length; i++) {
    
            for (int j = 0; j < nums.length; j++) {
    
                if (nums[j] > a) {
    
                    //first a = 0 just so i remember
                    //int[] nums = {15,31,7,2,7};
    
                    place[i] = nums[j];
                    a = place[i];
                    definer[j] = 1;
    

    Got it! The problem is exactly in the if above. the problem is in the nums[j] > a. Clearly a negative number wont pass the check ever and that is the problem.

    EDIT: Okay i just realized i misread where the first for ends (bad identation) but the problem is the same. You will have problems with negative numbers because your a and b are set to 0 every loop. The 0 that appear in the place of negative numbers are because you added b = 0 in the nums array.

    To solve that you should just use array.sort() as FairPlay said in his answer. it is not worth the trouble trying to fix this.