## Java sorting array positive ascending to negative ascending

alternate positive and negative numbers in array java

sort an array without changing position of negative numbers in java

sort array with negative numbers javascript

rearrange positive and negative numbers using inbuilt sort function

c++ program to rearrange positive and negative numbers in array

I can't solve the problem , where I need output from array A like {1,2,3,4,-1,-2,-3,-4} from random numbers in array, then write it to another array B. So far my experimental code doesn't work as I'd

public static void main(String[] args) { int a[] = {5,4,3,2,1,-3,-2,-30}; int length = a.length - 1; for (int i = 0 ; i < length ; i++) { for (int j = 0 ; j < length-i ; j++) { if (a[j] < a[j+1]) { int swap = a[j]; a[j] = a[j+1]; a[j+1] = swap; } } } for (int x : a) { System.out.print(x+" "); } }

Output is 5 4 3 2 1 -2 -3 -30 , but I need 1,2,3,4,5,-2,-3,-30

Update:

public static void main(String[] args) { int a[] = {5,4,3,2,1,-3,-2,-30,-1,-15,8}; int length = a.length - 1; for (int i = 0 ; i < length ; i++) { for (int j = 0 ; j < length-i ; j++) { if (a[j] < a[j+1]) { int swap = a[j]; a[j] = a[j+1]; a[j+1] = swap; } else { if (a[j] > a[j+1] && a[j+1] > 0) { int swap = a[j]; a[j] = a[j+1]; a[j+1] = swap; } } } } for (int x : a) { System.out.print(x+" "); } }

I got closer to my target but 8 1 2 3 4 5 -1 -2 -3 -15 -30 , that number 8 ruins it all

Add an if-else to differentiate the positive and negative case.

if (a[j] < 0) { if (a[j] < a[j+1]) { int swap = a[j]; a[j] = a[j+1]; a[j+1] = swap; } } else { if (a[j] > a[j+1] && a[j+1] > 0) { int swap = a[j]; a[j] = a[j+1]; a[j+1] = swap; } }

**Rearrange positive and negative numbers with constant extra space ,** Given an array of positive and negative numbers, arrange them such that all negative We can modify insertion sort to solve this problem. import java.io.*;. In this java tutorial, we are sorting an array in ascending order using temporary variable and nested for loop. We are using Scanner class to get the input from user. Java Example: Program to Sort an Array in Ascending Order. In this program, user is asked to enter the number of elements that he wish to enter.

If I understand you correctly you want to sort after two things. Positive numbers from low to high and negative numbers from high to low.

You could first sort from high to low and in a second run over the array skip all positives and then sort from high to low.

Does this help? I could write some code, but I believe that's something you want to learn right now :)

**Sort an array without changing position of negative numbers ,** Given an array arr[] of N integers, the task is to sort the array without changing the position of Java implementation of the approach sort function · Find ratio of zeroes, positive numbers and negative numbers in the Array Sort all even numbers in ascending order and then sort all odd numbers in descending order Java program to sort an array of integers in ascending order : In this Java programming tutorial, we will learn how to sort an array of integers in ascending order. Our program will first take the inputs from the user and create one integer array. Then it will sort the numbers of the array and print it out again to the user.

Algo:

Traverse the Array and Store positives in one and Negatives in another. O(i)

Sort the positives array in ascending order. O(mLog(m))

Sort the negatives indescending order. O(nLog(n))

Create a final array of the size of the input.

Add all the positive array sorted values. Then add the negative array sorted values. O(i)

Total : O(i) + O(mLog(m)) + O(nLog(n)) + O(i) = O(mLog(m)) if m > n

**Java program for bubble sort in Ascending & descending order,** In this tutorial we are gonna see how to do sorting in ascending & descending order nextInt(); int array[] = new int[num]; System.out.println("Enter " + num + Java Program to Sort the Array in an Ascending Order. This is a Java Program to Sort the Array in an Ascending Order. Enter size of array and then enter all the elements of that array. Now with the help of for loop and temp variable we sort the array in ascending order.

I have used library functions here. But if you want you can the write the functions using the same idea.

public class PostivieAsendingNegativeDesending implements Comparator<Integer> { public static void main(String args[]) { int fullList[] = {5, 4, 3, 2, 1, -3, -2, -30}; ArrayList<Integer> subList = new ArrayList<>(); ArrayList<Integer> subList2 = new ArrayList<>(); for (int i = 0; i < fullList.length; i++) { if (fullList[i] < 0) { subList2.add((fullList[i])); } else { subList.add(fullList[i]); } } Collections.sort(subList); Collections.sort(subList2, new PostivieAsendingNegativeDesending()); subList.addAll(subList2); for (int i = 0; i < subList.size(); i++) { System.out.print(subList.get(i) + " "); } System.out.println(""); } @Override public int compare(Integer n1, Integer n2) { return n2 - n1; } }

**Sorting,** Suppose we need to sort an array of positive integers {3,11,2,9,1,5}. Then, go through the input array and place integer 3 into a second array at index 3, integer 11 at index 11 and so on. See implementation details in in MergeSort.java. The returned value is negative, zero or positive depending on whether this object Java Program to Sort Array in Ascending Order using Temp Variable. This Java program allows the user to enter the size and the One Dimensional Array elements. Next, it will sort the array element in ascending order using For Loop.

This will do the trick which uses only basic loops

public static void main(String[] args) { int a[] = { 5, 4, 3, 2, 1, -3, -2, -30 }; int length = a.length - 1; int pos = 0, neg = 0; // find total count of positive and negative numbers for (int i = 0; i <= length; i++) { if (a[i] < 0) neg++; else pos++; } // initialize the arrays based on 'pos' and 'neg' int posArr[] = new int[pos]; int negArr[] = new int[neg]; // store pos and neg values in the arrays int countPos = 0, countNeg = 0; for (int i = 0; i <= length; i++) { if (a[i] < 0) { negArr[countNeg] = a[i]; countNeg++; } else { posArr[countPos] = a[i]; countPos++; } } // sort positive numbers for (int i = 0; i < posArr.length - 1; i++) { for (int j = 0; j < posArr.length - 1 - i; j++) { if (posArr[j] > posArr[j + 1]) { int swap = posArr[j]; posArr[j] = posArr[j + 1]; posArr[j + 1] = swap; } } } // sort negative numbers for (int i = 0; i < negArr.length - 1; i++) { for (int j = 0; j < negArr.length - 1 - i; j++) { if (negArr[j] < negArr[j + 1]) { int swap = negArr[j]; negArr[j] = negArr[j + 1]; negArr[j + 1] = swap; } } } // 1. print out posArr[] and then negArr[] // or // 2. merge them into another array and print }

Logic is explained below :

Find total count of positive and negative numbers.

Create and store the positive and negative values in the respective arrays.

Sort positive array in ascending order.

Sort negative array in descending order.

Print out positive array followed by the negative array OR merge them into another and print.

**Ordered Array,** Comparison, Zero, Negative, or Positive. " 9999" . The elements of an ordered array are arranged in ascending (or descending) order. In general, an ordered sort() method is a java.util.Arrays class method.. Syntax: public static void sort(int[] arr, int from_Index, int to_Index) arr - the array to be sorted from_Index - the index of the first element, inclusive, to be sorted to_Index - the index of the last element, exclusive, to be sorted This method doesn't return any value.

**C# Sharp Exercises: Sort elements of array in ascending order ,** C# Sharp programming, exercises, solution: Write a program in C# Sharp to sort elements of array in ascending order. Sort all even numbers in ascending order and then sort all odd numbers in descending order Given an array of integers (both odd and even), sort them in such a way that the first part of the array contains odd numbers sorted in descending order, rest portion contains even numbers sorted in ascending order.

**How to sort an array numerically in JavaScript,** To sort the array numerically in ascending order, That comparison item, thus returning a positive value if the second item is bigger, a negative In this tutorial, it shows the use of java.lang.Comparable and java.util.Comparator to sort a Java object based on its property value.. 1. Sort an Array. To sort an Array, use the Arrays.sort().

**Squares of a Sorted Array,** We can use two pointers to read the positive and negative parts of the array - one pointer j in the positive direction, and another i in the negative Integer[] Array - before sorting : 1975 2003 1979 1992 1983 1999 1987 Integer[] Array - after sorting in ascending order : 1975 1979 1983 1987 1992 1999 2003 String[] Array - before sorting : Clive Kapil Allan Imran Arjuna Steve Ricky String[] Array - after sorting in ascending order : Allan Arjuna Clive Imran Kapil Ricky Steve

##### Comments

- you might want to split the input into two lists of negative and positive integers, then sort them and then merge accordingly.
- You'll probably just want to write a custom comparator (stackoverflow.com/questions/5245093/…) that sorts first by sign, and then in ascending order by absolute value.
- I am not experienced enough to use comparator...
- I get output- 4 3 2 1 5 -2 -3 -30
- Copy the code and insert it into the internal for. In your updated code you miss the if condition
- Thank you for your help, I managed to make my code work based on your code idea! :)
- Did it actually help?
- Yes,it did help! :)
- Thank you for your tip! :)