Sort only positive value and remain negative value with its index, since it has an array

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

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 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; } 
+8
java sorting arrays
source share
6 answers

In the second loop for each inputArray[j] you need to find the next element that 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; } 
+6
source share

Try:

  • Retrieve Only Positive Values
  • Sort them using Collections.sort (or Array.sort )
  • Go through the original array and replace the positive values ​​with the ordered ones.
+7
source share

You can try to sort yourself or extract only positive values ​​and sort them, but here is an alternative version that leaves the input array unmodified (since otherwise returning a new array from the method would be superfluous).

The code simply copies and sorts the input array first, then combines the negative values ​​from the input array with the positive values ​​from the sorted array. Since negative values ​​were sorted first, there is no way to overwrite the sorted values ​​in a copy.

The code also does not insert values, as would be necessary to build List<Integer> positive values.

 private static int[] sortByHeight(int[] inputArray) { int[] arr = inputArray.clone(); Arrays.sort(arr); int i = 0; while (i < arr.length && arr[i] < 0) i++; for (int j = 0; j < arr.length; j++) arr[j] = (inputArray[j] < 0 ? inputArray[j] : arr[i++]); return arr; } 

Test

 int[] inputArray = {-1, 150, 190, 170, -2, -1, 160, 180}; int[] outputArray = sortByHeight(inputArray); System.out.println(Arrays.toString(outputArray)); 

Exit

 [-1, 150, 160, 170, -2, -1, 180, 190] 

Reusing arr for both a sorted array of all values ​​and an array of results works because a positive value will only be copied or will remain where it is.

To illustrate:

 -1, 150, 190, 170, -2, -1, 160, 180 // Input array ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ -1, 150, 160, 170, -2, -1, 180, 190 // Result array ↑ ↑ └─────────┐ ↑ ↑ ↑ └─────────┐ ↑ ↑ ↑ └─────────┐ ↑ ↑ ↑ ↑ -2, -1, -1, 150, 160, 170, 180, 190 // Array after sorting 
+5
source share
 List<Integer> positives = new ArrayList<Integer>(); for (int i : inputArray) if (i > 0) positives.add(i); Collections.sort(positives); int x = 0; for (int y = 0; y < inputArray.length; y++) if (inputArray[y] > 0) inputArray[y] = positives.get(x++); 

This extracts all positive values ​​from the int input array, sorts them and replaces the positive values ​​one by one.

+3
source share

Turning what @ jean-logeart said in code:

 public static int[] sortByHeight(int[] inputArray) { // Extract only the positive values ArrayList<Integer> positiveValues = new ArrayList<>(); for (int value: inputArray) { if (value > 0) positiveValues.add(value); } // Sort them using Collections.sort (or Array.sort) Collections.sort(positiveValues); // Go through the original array and replace the positive values by the ordered ones int positiveIndex = 0; for (int index = 0; index < inputArray.length; index++) { if (inputArray[index] > 0) { inputArray[index] = positiveValues.get(positiveIndex); positiveIndex++; } } return inputArray; } 

Running with main () posed in the question calls:

 -1, 150, 160, 170, -1, -1, 180, 190, 
+3
source share

How about an in-place replacement like this:

  int[] inputArray = { -1, 150, 190, 170, -1, -1, 160, 180 }; Iterator<Integer> sorted = Arrays.stream(inputArray) .filter(x -> x > 0) .sorted() .boxed() .collect(Collectors.toList()) .iterator(); IntStream.range(0, inputArray.length) .forEach(x -> { if (inputArray[x] > 0) { inputArray[x] = sorted.next(); } }); 
+2
source share

All Articles