Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sort only the positive value and remain the negative value with it's index as it is of an array

I need to sort the array in ascending order only for the positive value. For the negative value the index position will remain the same as it is.

If the array is : int[] inputArray = {-1, 150, 190, 170, -1, -1, 160, 180}.

The output should be like this - int[] outputArray = {-1, 150, 160, 170, -1, -1, 180, 190}.

But in my case the output is this - int[] outputArray = {-1, 150, 170, 190, -1, -1, 160, 180}.

Here is my code below :

public static void main(String[] args) {
    int[] inputArray = {-1, 150, 190, 170, -1, -1, 160, 180};
    int[] outputArray = sortByHeight(inputArray);

    for (int item : outputArray) {
        System.out.print(item + ", ");
    }
}

public static int[] sortByHeight(int[] inputArray) {
    for (int i=0; i<inputArray.length; i++) {
        for (int j = 0; j<inputArray.length - 1; j++) {
            int temp = inputArray[j];
            if (temp >= 0) {
                if (inputArray[j] > inputArray[j+1] && inputArray[j+1] >= 0) {
                    inputArray[j] = inputArray[j+1];
                    inputArray[j+1] = temp;
                }
            }
        }
    }
    return inputArray;
}
like image 771
Sumon Bappi Avatar asked Jul 10 '17 19:07

Sumon Bappi


People also ask

How do you sort only positive numbers in an array?

Using brute force approach to sort only positive numbers in the array. We will first filter out the positive numbers from the given array and sort them in required order. After that we will use a temp array to store the sorted array and extra variable to track the elements of the filtered and sorted positive array.

How do you sort an array with negative and positive numbers in Javascript?

In order to sort numbers, you'll need to write a function that returns a negative number if a is less than b , returns a positive number if b is less than a , and returns 0 if the numbers are the same. This can easily be accomplished by subtracting the numbers.

How do you store negative numbers in an array?

No, you cannot use a negative integer as size, the size of an array represents the number of elements in it, –ve number of elements in an array makes no sense.


2 Answers

In the second loop, for every inputArray[j] you need to find next element which is greater than 0 before comparing.

 public static void main(String[] args) {
        int[] inputArray = {-1, 150, 190, 170, -1, -1, 160, 180};
        int[] outputArray = sortByHeight(inputArray);

        for (int item : outputArray) {
            System.out.print(item + ", ");
        }
    }

    public static int[] sortByHeight(int[] inputArray) {
        for (int i=0; i<inputArray.length; i++) {
            for (int j = 0; j<inputArray.length - 1; j++) {
                int temp = inputArray[j];
                if (temp >= 0) {
                    int k = j+1;
                    while(inputArray[k] < 0)
                       k++;
                    if (inputArray[j] > inputArray[k] && inputArray[k] >= 0) {
                        inputArray[j] = inputArray[k];
                        inputArray[k] = temp;
                    }
                }
            }
        }
        return inputArray;
    }
like image 81
Dij Avatar answered Oct 15 '22 01:10

Dij


Try to:

  1. Extract only the positive values
  2. Sort them using Collections.sort (or Array.sort)
  3. Go through the original array and replace the positive values by the ordered ones
like image 33
Jean Logeart Avatar answered Oct 15 '22 01:10

Jean Logeart