0% found this document useful (0 votes)
242 views2 pages

Gmail - Sort All Even Numbers in Ascending Order and Then Sort All Odd Numbers in Descending Order

The document describes a Java program that sorts an integer array in a two-way manner: first by sorting even numbers in ascending order, then sorting odd numbers in descending order. It uses two pointers from the left and right sides to iterate through the array, swapping even and odd numbers. The odd numbers are then sorted in descending order using Collections.reverseOrder(), and the even numbers are sorted in ascending order. The program is tested on a sample input array.

Uploaded by

anis ansari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
242 views2 pages

Gmail - Sort All Even Numbers in Ascending Order and Then Sort All Odd Numbers in Descending Order

The document describes a Java program that sorts an integer array in a two-way manner: first by sorting even numbers in ascending order, then sorting odd numbers in descending order. It uses two pointers from the left and right sides to iterate through the array, swapping even and odd numbers. The odd numbers are then sorted in descending order using Collections.reverseOrder(), and the even numbers are sorted in ascending order. The program is tested on a sample input array.

Uploaded by

anis ansari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Sort all even numbers in ascending order and then sort all odd numbers in descending order

1 message

Prof. AmanClasses ansari <[email protected]> Tue, Nov 24, 2020 at 22:41


To: anislinek15 <[email protected]>

// Java program sort array in even and odd manner.


// The odd numbers are to be sorted in descending
// order and the even numbers in ascending order
  
import java.util.Arrays;
import java.util.Collections;
  
public class GFG {
    // To do two way sort. First sort even numbers in
    // ascending order, then odd numbers in descending
    // order.
    static void twoWaySort(Integer arr[], int n)
    {
        // Current indexes from left and right
        int l = 0, r = n - 1;
  
        // Count of odd numbers
        int k = 0;
  
        while (l < r) 
        {
          
            // Find first even number from left side.
            while (arr[l] % 2 != 0) 
            {
                l++;
                k++;
            }
  
            // Find first odd number from right side.
            while (arr[r] % 2 == 0 && l < r)
                r--;
  
            // Swap even number present on left and odd
            // number right.
            if (l < r) 
            {
              
                // swap arr[l] arr[r]
                int temp = arr[l];
                arr[l] = arr[r];
                arr[r] = temp;
            }
        }
  
        // Sort odd number in descending order
        Arrays.sort(arr, 0, k, Collections.
                                  reverseOrder());
  
        // Sort even number in ascending order
        Arrays.sort(arr, k, n);
    }
  
    // Driver Method
    public static void main(String[] args)
    {
        Integer arr[] = { 1, 3, 2, 7, 5, 4 };
  
        twoWaySort(arr, arr.length);
  
        System.out.println(Arrays.toString(arr));
    }
}
------------------------
Output: 

7 5 3 1 2 4

You might also like