📌  相关文章
📜  将给定数组排序为降序-最低-升序形式

📅  最后修改于: 2021-04-30 02:28:42             🧑  作者: Mango

给定大小为N且整数K的数组arr [] ,任务是对数组进行排序,以使数组的前K个元素按降序排列,而数组的最后N – K个元素按升序排列。

例子:

方法:请按照以下步骤解决问题:

  • 按降序对数组进行排序。
  • 按升序对数组的最后(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


输出:
9 9 8 8 7 7 0 1 1 2 2 6 6

时间复杂度: O(N * log(N))
辅助空间: O(1)