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.
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 ratings0% 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.
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) {