📌  相关文章
📜  按升序对前k个值进行排序,按降序对其余nk个值进行排序

📅  最后修改于: 2021-04-24 05:13:34             🧑  作者: Mango

给定一个大小为n的数组,请按升序排列该数组的前k个元素,并按降序排列其余nk个元素。
例子:

算法:

  1. 将前k个元素存储在数组中,并按升序对其进行排序。
  2. 将剩余的nk个元素存储在一个数组中,并按降序对其进行排序。
  3. 通过以相反的顺序添加第二个数组中的元素来合并两个数组。
C++
// C++ program to sort first k elements
// in increasing order and remaining
// n-k elements in decreasing
#include 
using namespace std;
 
// Function to sort the array
void printOrder(int arr[], int n, int k)
{
    int len1 = k, len2 = n - k;
    int arr1[k], arr2[n - k];
 
    // Store the k elements in an array
    for (int i = 0; i < k; i++)
        arr1[i] = arr[i];
 
    // Store the remaining n-k elements in an array
    for (int i = k; i < n; i++)
        arr2[i - k] = arr[i];
 
    // sorting the array from 0 to k-1 places
    sort(arr1, arr1 + len1);
 
    // sorting the array from k to n places
    sort(arr2, arr2 + len2);
 
    // storing the values in the final array arr
    for (int i = 0; i < n; i++) {
        if (i < k)
            arr[i] = arr1[i];
 
        else {
            arr[i] = arr2[len2 - 1];
            len2--;
        }
    }
    // printing the array
    for (int i = 0; i < n; i++)
        cout << arr[i] << " ";
}
 
// Driver code
int main()
{
    int arr[] = { 5, 4, 6, 2, 1, 3, 8, 9, -1 };
    int k = 4;
 
    int n = sizeof(arr) / sizeof(arr[0]);
 
    printOrder(arr, n, k);
 
    return 0;
}


Java
// Java program to sort first k elements
// in increasing order and remaining
// n-k elements in decreasing
import java.util.*;
 
class GFG {
    // function to print half of the array in
    // ascending order and the other half in
    // descending order
    static void printOrder(int[] arr, int n, int k)
    {
        int len1 = k, len2 = n - k;
        int[] arr1 = new int[k];
        int[] arr2 = new int[n - k];
 
        // Store the k elements in an array
        for (int i = 0; i < k; i++)
            arr1[i] = arr[i];
 
        // Store the remaining n-k elements in an array
        for (int i = k; i < n; i++)
            arr2[i - k] = arr[i];
 
        // sorting the array from 0 to k-1 places
        Arrays.sort(arr1, 0, k);
 
        // sorting the array from k to n places
        Arrays.sort(arr2, k, n - k);
 
        // storing the values in the final array arr
        for (int i = 0; i < n; i++) {
            if (i < k)
                arr[i] = arr1[i];
 
            else {
                arr[i] = arr2[len2 - 1];
                len2--;
            }
        }
        // printing the array
        for (int i = 0; i < n; i++) {
            System.out.print(arr[i] + " ");
        }
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int arr[] = { 5, 4, 6, 2, 1, 3, 8, 9, -1 };
        int k = 4;
        int n = arr.length;
        printOrder(arr, n, k);
    }
}


Python 3
# Python 3 program to sort first
# k elements in increasing order
# and remaining n-k elements in
# decreasing
 
# Function to sort the array
def printOrder(arr, n, k):
 
    len1 = k
    len2 = n - k
    arr1 = [0] * k
    arr2 = [0] * (n - k)
 
    # Store the k elements
    # in an array
    for i in range(k):
        arr1[i] = arr[i]
 
    # Store the remaining n-k
    # elements in an array
    for i in range(k, n):
        arr2[i - k] = arr[i]
 
    # sorting the array from
    # 0 to k-1 places
    arr1.sort()
 
    # sorting the array from
    # k to n places
    arr2.sort()
 
    # storing the values in the
    # final array arr
    for i in range(n):
        if (i < k):
            arr[i] = arr1[i]
 
        else :
            arr[i] = arr2[len2 - 1]
            len2 -= 1
     
    # printing the array
    for i in range(n):
        print(arr[i], end = " ")
 
# Driver code
if __name__ == "__main__":
    arr = [ 5, 4, 6, 2, 1,
            3, 8, 9, -1 ]
    k = 4
 
    n = len(arr)
 
    printOrder(arr, n, k)
 
# This code is contributed
# by ChitraNayal


C#
// C# program to sort first k elements
// in increasing order and remaining
// n-k elements in decreasing
using System;
 
class GFG {
    // function to print half of the array in
    // ascending order and the other half in
    // descending order
    static void printOrder(int[] arr,
                           int n, int k)
    {
        int len2 = n - k;
        int[] arr1 = new int[k];
        int[] arr2 = new int[n - k];
 
        // Store the k elements in an array
        for (int i = 0; i < k; i++)
            arr1[i] = arr[i];
 
        // Store the remaining n-k
        // elements in an array
        for (int i = k; i < n; i++)
            arr2[i - k] = arr[i];
 
        // sorting the array from
        // 0 to k-1 places
        Array.Sort(arr1, 0, k);
 
        // sorting the array from k to n places
        Array.Sort(arr2, 0, n - k);
 
        // storing the values in
        // the final array arr
        for (int i = 0; i < n; i++) {
            if (i < k)
                arr[i] = arr1[i];
 
            else {
                arr[i] = arr2[len2 - 1];
                len2--;
            }
        }
 
        // printing the array
        for (int i = 0; i < n; i++) {
            Console.Write(arr[i] + " ");
        }
    }
 
    // Driver code
    public static void Main()
    {
        int[] arr = { 5, 4, 6, 2, 1,
                      3, 8, 9, -1 };
        int k = 4;
        int n = arr.Length;
        printOrder(arr, n, k);
    }
}
 
// This code is contributed by Subhadeep


PHP


C++
// C++ program to sort first k elements
// in increasing order and remaining
// n-k elements in decreasing
#include 
using namespace std;
 
// function to sort the array
void printOrder(int arr[], int n, int k)
{
    // Sort first k elements in ascending order
    sort(arr, arr + k);
 
    // Sort remaining n-k elements in descending order
    sort(arr + k, arr + n, greater());
}
 
// Driver code
int main()
{
    int arr[] = { 5, 4, 6, 2, 1, 3, 8, 9, -1 };
    int k = 4;
    int n = sizeof(arr) / sizeof(arr[0]);
 
    printOrder(arr, n, k);
 
    for (int i = 0; i < n; i++)
        cout << arr[i] << " ";
 
    return 0;
}


Java
// Java program to sort first k elements
// in increasing order and remaining
// n-k elements in decreasing
import java.util.*;
 
public class SortExample {
    static void printOrder(Integer[] arr, int k)
    {
        int n = arr.length;
 
        // Sort first k elements in ascending order
        Arrays.sort(arr, 0, k);
 
        // Sort remaining n-k elements in descending order
        Arrays.sort(arr, k, n, Collections.reverseOrder());
    }
 
    public static void main(String[] args)
    {
        // Our arr contains 8 elements
        Integer[] arr = { 5, 4, 6, 2, 1, 3, 8, 9, -1 };
        int k = 4;
        printOrder(arr, k);
        System.out.printf("%s", Arrays.toString(arr));
    }
}


Python3
# Python3 program to sort first k elements
# in increasing order and remaining
# n-k elements in decreasing
 
# function to sort the array
def printOrder(arr, n, k):
 
    # Sort first k elements in ascending order
    a = arr[0:k];
    a.sort();
 
    # Sort remaining n-k elements in descending order
    b = arr[k:n];
    b.sort();
    b.reverse();
    return a + b;
 
# Driver code
arr = [ 5, 4, 6, 2, 1, 3, 8, 9, -1 ];
k = 4;
n = len(arr);
 
arr = printOrder(arr, n, k);
 
for i in range(n):
    print(arr[i], end =" ");
 
# This code is contributed by mits


C#
// C# program to sort first k elements
// in increasing order and remaining
// n-k elements in decreasing
using System;
 
public class SortExample {
    static void printOrder(int[] arr, int k)
    {
        int n = arr.Length;
 
        // Sort first k elements in ascending order
        Array.Sort(arr, 0, k);
 
        // Sort remaining n-k elements in descending order
        Array.Sort(arr, k, n - k);
        Array.Reverse(arr, k, n - k);
    }
 
    // Driver code
    public static void Main(String[] args)
    {
        // Our arr contains 8 elements
        int[] arr = { 5, 4, 6, 2, 1, 3, 8, 9, -1 };
        int k = 4;
        printOrder(arr, k);
        Console.Write("{0}", String.Join(" ", arr));
    }
}
 
// This code contributed by Rajput-Ji


PHP


输出:
2 4 5 6 9 8 3 1 -1

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

高效的方法:这个想法很简单,使用库函数按升序对前k个元素进行排序,对剩余的nk个元素进行降序排序。

C++

// C++ program to sort first k elements
// in increasing order and remaining
// n-k elements in decreasing
#include 
using namespace std;
 
// function to sort the array
void printOrder(int arr[], int n, int k)
{
    // Sort first k elements in ascending order
    sort(arr, arr + k);
 
    // Sort remaining n-k elements in descending order
    sort(arr + k, arr + n, greater());
}
 
// Driver code
int main()
{
    int arr[] = { 5, 4, 6, 2, 1, 3, 8, 9, -1 };
    int k = 4;
    int n = sizeof(arr) / sizeof(arr[0]);
 
    printOrder(arr, n, k);
 
    for (int i = 0; i < n; i++)
        cout << arr[i] << " ";
 
    return 0;
}

Java

// Java program to sort first k elements
// in increasing order and remaining
// n-k elements in decreasing
import java.util.*;
 
public class SortExample {
    static void printOrder(Integer[] arr, int k)
    {
        int n = arr.length;
 
        // Sort first k elements in ascending order
        Arrays.sort(arr, 0, k);
 
        // Sort remaining n-k elements in descending order
        Arrays.sort(arr, k, n, Collections.reverseOrder());
    }
 
    public static void main(String[] args)
    {
        // Our arr contains 8 elements
        Integer[] arr = { 5, 4, 6, 2, 1, 3, 8, 9, -1 };
        int k = 4;
        printOrder(arr, k);
        System.out.printf("%s", Arrays.toString(arr));
    }
}

Python3

# Python3 program to sort first k elements
# in increasing order and remaining
# n-k elements in decreasing
 
# function to sort the array
def printOrder(arr, n, k):
 
    # Sort first k elements in ascending order
    a = arr[0:k];
    a.sort();
 
    # Sort remaining n-k elements in descending order
    b = arr[k:n];
    b.sort();
    b.reverse();
    return a + b;
 
# Driver code
arr = [ 5, 4, 6, 2, 1, 3, 8, 9, -1 ];
k = 4;
n = len(arr);
 
arr = printOrder(arr, n, k);
 
for i in range(n):
    print(arr[i], end =" ");
 
# This code is contributed by mits

C#

// C# program to sort first k elements
// in increasing order and remaining
// n-k elements in decreasing
using System;
 
public class SortExample {
    static void printOrder(int[] arr, int k)
    {
        int n = arr.Length;
 
        // Sort first k elements in ascending order
        Array.Sort(arr, 0, k);
 
        // Sort remaining n-k elements in descending order
        Array.Sort(arr, k, n - k);
        Array.Reverse(arr, k, n - k);
    }
 
    // Driver code
    public static void Main(String[] args)
    {
        // Our arr contains 8 elements
        int[] arr = { 5, 4, 6, 2, 1, 3, 8, 9, -1 };
        int k = 4;
        printOrder(arr, k);
        Console.Write("{0}", String.Join(" ", arr));
    }
}
 
// This code contributed by Rajput-Ji

的PHP


输出:
2 4 5 6 9 8 3 1 -1

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