给定一个整数数组(奇数和偶数),请对它们进行排序,以使数组的第一部分包含按降序排列的奇数,其余部分包含按升序排列的偶数。
例子:
Input : arr[] = {1, 2, 3, 5, 4, 7, 10}
Output : arr[] = {7, 5, 3, 1, 2, 4, 10}
Input : arr[] = {0, 4, 5, 3, 7, 2, 1}
Output : arr[] = {7, 5, 3, 1, 0, 2, 4}
询问:微软
方法1(使用分区)
- 对输入数组进行分区,以使所有奇数元素向左移动,所有偶数元素向右移动。此步骤需要O(n)。
- 对阵列进行分区后,请分别对左右部分进行排序。此步骤需要O(n Log n)。
以下是上述想法的实现。
C++
// C++ 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
#include
using namespace std;
// To do two way sort. First sort even numbers in
// ascending order, then odd numbers in descending
// order.
void twoWaySort(int 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]);
}
// Sort odd number in descending order
sort(arr, arr + k, greater());
// Sort even number in ascending order
sort(arr + k, arr + n);
}
// Driver code
int main()
{
int arr[] = { 1, 3, 2, 7, 5, 4 };
int n = sizeof(arr) / sizeof(int);
twoWaySort(arr, n);
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
return 0;
}
Java
// 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));
}
}
Python
# Python program to sort array
# in even and odd manner
# The odd numbers are to be
# sorted in descending order
# and the even numbers in
# ascending order
# To do two way sort. First
# sort even numbers in ascending
# order, then odd numbers in
# descending order.
def two_way_sort(arr, arr_len):
# Current indexes l->left
# and r->right
l, r = 0, arr_len - 1
# Count of number of
# odd numbers, used in
# slicing the array later.
k = 0
# Run till left(l) < right(r)
while(l < r):
# While left(l) is odd, if yes
# increment the left(l) plus
# odd count(k) if not break the
# while for even number found
# here to be swaped
while(arr[l] % 2 != 0):
l += 1
k += 1
# While right(r) is even,
# if yes decrement right(r)
# if not break the while for
# odd number found here to
# be swaped
while(arr[r] % 2 == 0 and l < r):
r -= 1
# Swap the left(l) and right(r),
# which is even and odd numbers
# encountered in above loops
if(l < r):
arr[l], arr[r] = arr[r], arr[l]
# Slice the number on the
# basis of odd count(k)
odd = arr[:k]
even = arr[k:]
# Sort the odd and
# even array accordingly
odd.sort(reverse = True)
even.sort()
# Extend the odd array with
# even values and return it.
odd.extend(even)
return odd
# Driver code
arr_len = 6
arr = [1, 3, 2, 7, 5, 4]
result = two_way_sort(arr, arr_len)
for i in result:
print(str(i) + " "),
# This code is contributed
# by JaySiyaRam
C#
// C# 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
using System;
using System.Linq;
class GFG {
// To do two way sort. First sort even numbers in
// ascending order, then odd numbers in descending
// order.
static void twoWaySort(int[] 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
Array.Sort(arr, 0, k);
Array.Reverse(arr, 0, k);
// Sort even number in ascending order
Array.Sort(arr, k, n - k);
}
// Driver Method
public static void Main(String[] args)
{
int[] arr = { 1, 3, 2, 7, 5, 4 };
twoWaySort(arr, arr.Length);
Console.WriteLine(String.Join(" ", arr));
}
}
// This code has been contributed by 29AjayKumar
Javascript
C++
// C++ 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
#include
using namespace std;
// To do two way sort. First sort even numbers in
// ascending order, then odd numbers in descending
// order.
void twoWaySort(int arr[], int n)
{
// Make all odd numbers negative
for (int i = 0; i < n; i++)
if (arr[i] & 1) // Check for odd
arr[i] *= -1;
// Sort all numbers
sort(arr, arr + n);
// Retaining original array
for (int i = 0; i < n; i++)
if (arr[i] & 1)
arr[i] *= -1;
}
// Driver code
int main()
{
int arr[] = { 1, 3, 2, 7, 5, 4 };
int n = sizeof(arr) / sizeof(int);
twoWaySort(arr, n);
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
return 0;
}
Java
// 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;
public class GFG
{
// To do two way sort. First sort even numbers in
// ascending order, then odd numbers in descending
// order.
static void twoWaySort(int arr[], int n)
{
// Make all odd numbers negative
for (int i = 0; i < n; i++)
if ((arr[i] & 1) != 0) // Check for odd
arr[i] *= -1;
// Sort all numbers
Arrays.sort(arr);
// Retaining original array
for (int i = 0; i < n; i++)
if ((arr[i] & 1) != 0)
arr[i] *= -1;
}
// Driver Method
public static void main(String[] args)
{
int arr[] = { 1, 3, 2, 7, 5, 4 };
twoWaySort(arr, arr.length);
System.out.println(Arrays.toString(arr));
}
}
Python3
# Python 3 program to sort array in
# even and odd manner. The odd
# numkbers are to be sorted in
# descending order and the even
# numbers in ascending order
# To do two way sort. First sort
# even numbers in ascending order,
# then odd numbers in descending order.
def twoWaySort(arr, n):
# Make all odd numbers negative
for i in range(0, n):
# Check for odd
if (arr[i] & 1):
arr[i] *= -1
# Sort all numbers
arr.sort()
# Retaining original array
for i in range(0, n):
if (arr[i] & 1):
arr[i] *= -1
# Driver code
arr = [1, 3, 2, 7, 5, 4]
n = len(arr)
twoWaySort(arr, n);
for i in range(0, n):
print(arr[i], end = " ")
# This code is contributed by Smitha Dinesh Semwal
C#
// 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
using System;
public class GFG
{
// To do two way sort. First sort
// even numbers in ascending order,
// then odd numbers in descending
// order.
static void twoWaySort(int[] arr, int n)
{
// Make all odd numbers negative
for (int i = 0; i < n; i++)
// Check for odd
if ((arr[i] & 1) != 0)
arr[i] *= -1;
// Sort all numbers
Array.Sort(arr);
// Retaining original array
for (int i = 0; i < n; i++)
if ((arr[i] & 1) != 0)
arr[i] *= -1;
}
// Driver Method
public static void Main()
{
int[] arr = { 1, 3, 2, 7, 5, 4 };
twoWaySort(arr, arr.Length);
for (int i = 0; i < arr.Length; i++)
Console.Write(arr[i] + " ");
}
}
// This code is contributed by Smitha
PHP
CPP
// C++ implementation of the approach
#include
using namespace std;
// Utility function to print
// the contents of the array
void printArr(int arr[], int n)
{
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
}
// To do two way sort. Make comparator function
// for the inbuilt sort function of c++ such that
// odd numbers are placed before even in descending
// and ascending order respectively
bool compare(int a, int b)
{
// If both numbers are even,
// smaller number should
// be placed at lower index
if (a % 2 == 0 && b % 2 == 0)
return a < b;
// If both numbers are odd larger number
// should be placed at lower index
if (a % 2 != 0 && b % 2 != 0)
return b < a;
// If a is odd and b is even,
// a should be placed before b
if (a % 2 != 0)
return true;
// If b is odd and a is even,
// b should be placed before a
return false;
}
// Driver code
int main()
{
int arr[] = { 1, 3, 2, 7, 5, 4 };
int n = sizeof(arr) / sizeof(int);
// Sort the array
sort(arr, arr + n, compare);
// Print the sorted array
printArr(arr, n);
return 0;
}
// This code is contributed by Nikhil Yadav
输出:
7 5 3 1 2 4
时间复杂度: O(n log n)
空间复杂度: O(1)
方法2(使用负乘法):
- 使所有奇数均为负数。
- 排序所有数字。
- 还原在步骤1中所做的更改以恢复原始元素。
C++
// C++ 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
#include
using namespace std;
// To do two way sort. First sort even numbers in
// ascending order, then odd numbers in descending
// order.
void twoWaySort(int arr[], int n)
{
// Make all odd numbers negative
for (int i = 0; i < n; i++)
if (arr[i] & 1) // Check for odd
arr[i] *= -1;
// Sort all numbers
sort(arr, arr + n);
// Retaining original array
for (int i = 0; i < n; i++)
if (arr[i] & 1)
arr[i] *= -1;
}
// Driver code
int main()
{
int arr[] = { 1, 3, 2, 7, 5, 4 };
int n = sizeof(arr) / sizeof(int);
twoWaySort(arr, n);
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
return 0;
}
Java
// 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;
public class GFG
{
// To do two way sort. First sort even numbers in
// ascending order, then odd numbers in descending
// order.
static void twoWaySort(int arr[], int n)
{
// Make all odd numbers negative
for (int i = 0; i < n; i++)
if ((arr[i] & 1) != 0) // Check for odd
arr[i] *= -1;
// Sort all numbers
Arrays.sort(arr);
// Retaining original array
for (int i = 0; i < n; i++)
if ((arr[i] & 1) != 0)
arr[i] *= -1;
}
// Driver Method
public static void main(String[] args)
{
int arr[] = { 1, 3, 2, 7, 5, 4 };
twoWaySort(arr, arr.length);
System.out.println(Arrays.toString(arr));
}
}
Python3
# Python 3 program to sort array in
# even and odd manner. The odd
# numkbers are to be sorted in
# descending order and the even
# numbers in ascending order
# To do two way sort. First sort
# even numbers in ascending order,
# then odd numbers in descending order.
def twoWaySort(arr, n):
# Make all odd numbers negative
for i in range(0, n):
# Check for odd
if (arr[i] & 1):
arr[i] *= -1
# Sort all numbers
arr.sort()
# Retaining original array
for i in range(0, n):
if (arr[i] & 1):
arr[i] *= -1
# Driver code
arr = [1, 3, 2, 7, 5, 4]
n = len(arr)
twoWaySort(arr, n);
for i in range(0, n):
print(arr[i], end = " ")
# This code is contributed by Smitha Dinesh Semwal
C#
// 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
using System;
public class GFG
{
// To do two way sort. First sort
// even numbers in ascending order,
// then odd numbers in descending
// order.
static void twoWaySort(int[] arr, int n)
{
// Make all odd numbers negative
for (int i = 0; i < n; i++)
// Check for odd
if ((arr[i] & 1) != 0)
arr[i] *= -1;
// Sort all numbers
Array.Sort(arr);
// Retaining original array
for (int i = 0; i < n; i++)
if ((arr[i] & 1) != 0)
arr[i] *= -1;
}
// Driver Method
public static void Main()
{
int[] arr = { 1, 3, 2, 7, 5, 4 };
twoWaySort(arr, arr.Length);
for (int i = 0; i < arr.Length; i++)
Console.Write(arr[i] + " ");
}
}
// This code is contributed by Smitha
的PHP
输出:
7 5 3 1 2 4
时间复杂度: O(n log n)
空间复杂度: O(n)
当输入数组包含负数时,此方法可能不起作用。但是,有一种方法可以处理此问题。我们在输入数组中计算正整数,然后再次排序。读者可以参考此实现。
方法3(使用比较器):
通过将内置排序函数与自定义比较方法结合使用,可以轻松解决此问题。比较任何两个元素时,将出现三种情况:
- 当两个元素均相等时:在这种情况下,较小的元素必须出现在排序数组中较大的元素的左侧。
- 当两个元素都为奇数时:较大的元素必须出现在较小的元素的左侧。
- 一个是奇数,另一个是偶数:奇数元素必须出现在偶数元素的左侧。
下面是上述方法的实现:
CPP
// C++ implementation of the approach
#include
using namespace std;
// Utility function to print
// the contents of the array
void printArr(int arr[], int n)
{
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
}
// To do two way sort. Make comparator function
// for the inbuilt sort function of c++ such that
// odd numbers are placed before even in descending
// and ascending order respectively
bool compare(int a, int b)
{
// If both numbers are even,
// smaller number should
// be placed at lower index
if (a % 2 == 0 && b % 2 == 0)
return a < b;
// If both numbers are odd larger number
// should be placed at lower index
if (a % 2 != 0 && b % 2 != 0)
return b < a;
// If a is odd and b is even,
// a should be placed before b
if (a % 2 != 0)
return true;
// If b is odd and a is even,
// b should be placed before a
return false;
}
// Driver code
int main()
{
int arr[] = { 1, 3, 2, 7, 5, 4 };
int n = sizeof(arr) / sizeof(int);
// Sort the array
sort(arr, arr + n, compare);
// Print the sorted array
printArr(arr, n);
return 0;
}
// This code is contributed by Nikhil Yadav
?list = PLqM7alHXFySEQDk2MDfbwEdjd2svVJH9p