给定一个大小为N的数组arr[]和一个整数K ,任务是对数组进行排序,使得数组的前K 个元素按降序排列,数组的最后N – K 个元素按升序排列。
例子:
Input: arr[]= {7, 6, 8, 9, 0, 1, 2, 2, 1, 8, 9, 6, 7}, K = 6
Output: 9 9 8 8 7 7 0 1 1 2 2 6 6
Explanation:
The first K (= 6) elements of the sorted array are {9, 9, 8, 8, 7, 7}, which are in descending order.
The last N – K (= 6) elements of the sorted array are {0, 1, 1, 2, 2, 6, 6}, which are in ascending order.
Therefore, the required output is 9 9 8 8 7 7 0 1 1 2 2 6 6
Input: arr[]= {65, 34, 54, 56, 75, 34, 54, 65, 56, 75, 15}, K = 5
Output: 75 75 65 65 56 56 15 34 34 54 54
处理方法:按照以下步骤解决问题:
- 按降序对数组进行排序。
- 按升序对数组的最后(N – K) 个元素进行排序。
- 打印数组。
下面是上述方法的实现:
C++
// C++ program to implement
// the above approach
#include
using namespace std;
// Function to sort first K array elements in
// descending and last N - K in ascending order
void sortArrayInDescAsc(int arr[], int N, int K)
{
// Sort the array in descending order
sort(arr, arr + N, greater());
// Sort last (N - K) array
// elements in ascending order
sort(arr + K, arr + N);
// Print array elements
for (int i = 0; i < N; i++) {
cout << arr[i] << " ";
}
}
// Driver Code
int main()
{
int arr[] = { 7, 6, 8, 9, 0, 1, 2,
2, 1, 8, 9, 6, 7 };
int N = sizeof(arr) / sizeof(arr[0]);
int K = 6;
sortArrayInDescAsc(arr, N, K);
}
Java
// Java program to implement
// the above approach
import java.util.*;
class GFG{
// Function to sort first K array elements in
// descending and last N - K in ascending order
static void sortArrayInDescAsc(int arr[], int N,
int K)
{
// Sort the array in descending order
Arrays.sort(arr);
for(int i = 0, j = N - 1; i < N / 2; i++)
{
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
j--;
}
// Sort last (N - K) array
// elements in ascending order
Arrays.sort(arr, K, N);
// Print array elements
for(int i = 0; i < N; i++)
{
System.out.print(arr[i] + " ");
}
}
// Driver Code
public static void main(String[] args)
{
int arr[] = { 7, 6, 8, 9, 0, 1, 2,
2, 1, 8, 9, 6, 7 };
int N = arr.length;
int K = 6;
sortArrayInDescAsc(arr, N, K);
}
}
// This code is contributed by Amit Katiyar
Python3
# Python3 program to implement
# the above approach
# Function to sort first K array
# elements in descending and last
# N - K in ascending order
def sortArrayInDescAsc(arr, N, K):
# Sort the array in descending order
arr = sorted(arr)
arr = arr[::-1]
# Sort last (N - K) array
# elements in ascending order
for i in arr[:K]:
print(i, end = " ")
for i in reversed(arr[K:]):
print(i, end = " ")
# Driver Code
if __name__ == '__main__':
arr = [ 7, 6, 8, 9, 0, 1,
2, 2, 1, 8, 9, 6, 7 ]
N = len(arr)
K = 6
sortArrayInDescAsc(arr, N, K)
# This code is contributed by mohit kumar 29
C#
// C# program to implement
// the above approach
using System;
class GFG{
// Function to sort first K array elements in
// descending and last N - K in ascending order
static void sortArrayInDescAsc(int[] arr, int N,
int K)
{
// Sort the array in descending order
Array.Sort(arr);
Array.Reverse(arr);
// Sort last (N - K) array
// elements in ascending order
int temp = 0;
for(int i = K; i < N; i++)
{
for(int j = i + 1; j < N; j++)
{
if (arr[i] > arr[j])
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
// Print array elements
for(int i = 0; i < N; i++)
{
Console.Write(arr[i] + " ");
}
}
// Driver code
public static void Main()
{
int[] arr = { 7, 6, 8, 9, 0, 1, 2,
2, 1, 8, 9, 6, 7 };
int N = arr.Length;
int K = 6;
sortArrayInDescAsc(arr, N, K);
}
}
// This code is contributed by sanjoy_62
Javascript
9 9 8 8 7 7 0 1 1 2 2 6 6
时间复杂度: O(N * log(N))
辅助空间: O(1)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live