📌  相关文章
📜  上半部分按升序排序,后半部分按降序排序|套装2

📅  最后修改于: 2021-04-29 13:15:33             🧑  作者: Mango

给定一个整数数组,请按升序对数组的前半部分进行排序,然后按降序对后半部分进行排序。
例子:

Input : arr[] = {10, 20, 30, 40}
Output : arr[] = {10, 20, 40, 30}

Input : arr[] = {5, 4, 6, 2, 1, 3, 8, 9, 7 }
Output : arr[] = {2, 4, 5, 6, 9, 8, 7, 3, 1 }

我们已经讨论了一种解决方案,该解决方案只按升序打印上半部分,然后按降序打印下半部分|套装1

简单方法
这个想法很简单,我们使用库函数按升序对前半部分进行排序,然后按降序对下半部分进行排序。像Java和C++这样的大多数语言都提供了按指定顺序对子数组进行排序的规定。在这篇文章中,讨论了修改原始数组的另一种解决方案。

C++
// C++ program to sort first half in increasing
// order and second half in decreasing
#include
using namespace std;
  
void mySort(int arr[], int n)
{
    sort(arr, arr+n/2);
    sort(arr+n/2, arr+n, greater());
}
  
int main()
{
    int arr[] = { 5, 4, 6, 2, 1, 3, 8, 9, 7 };
    int n = sizeof(arr)/sizeof(arr[0]);
    mySort(arr, n);
    cout << "Modified Array : \n";
    for (int i=0; i


Java
// Java program to sort first half in increasing
// order and second half in decreasing
import java.util.*;
  
public class SortExample {
    static void mySort(Integer[] arr)
    {
        int n = arr.length;
  
        // Sort subarray from index 1 to 4, i.e.,
        // only sort subarray {7, 6, 45, 21} and
        // keep other elements as it is.
        Arrays.sort(arr, 0, n / 2);
        Arrays.sort(arr, n / 2, n, Collections.reverseOrder());
    }
  
    public static void main(String[] args)
    {
        // Our arr contains 8 elements
        Integer[] arr = { 5, 4, 6, 2, 1, 3, 8, 9, 7 };
        mySort(arr);
        System.out.printf("Modified arr[] : %s",
                          Arrays.toString(arr));
    }
}


Python 3
# Python3 program to sort first half in increasing
# order and second half in decreasing
  
# required sorting function
def mySort( arr, n):
      
    arr1 = arr[:n//2]
    arr2 = arr[n//2:]
    arr1.sort()
    arr2.sort(reverse=True)
    return arr1+arr2
      
      
  
# driving function
if __name__=='__main__':
    arr= [5, 4, 6, 2, 1, 3, 8, 9, 7 ]
    n = len(arr)
    arr=mySort(arr, n)
    print( "Modified Array : ")
    print(arr)
  
# this code is contributed by ash264


C#
// C# program to sort first half in increasing
// order and second half in decreasing
using System;
  
public class SortExample 
{
    static void mySort(int[] arr)
    {
        int n = arr.Length;
  
        // Sort subarray from index 1 to 4, i.e.,
        // only sort subarray {7, 6, 45, 21} and
        // keep other elements as it is.
        Array.Sort(arr, 0, n / 2);
        Array.Sort(arr, n / 2, (n/2)+1);
        Array.Reverse(arr, n / 2, (n/2)+1);
    }
  
    // Driver code
    public static void Main(String[] args)
    {
        // Our arr contains 8 elements
        int[] arr = { 5, 4, 6, 2, 1, 3, 8, 9, 7 };
        mySort(arr);
        Console.Write("Modified arr[] : {0}",
                        String.Join(" ",arr));
    }
}
  
/* This code contributed by PrinciRaj1992 */


PHP


C++
// C++ program to sort first half in increasing
// order and second half in decreasing
#include
using namespace std;
  
void mySort(int arr[], int n)
{
    // Sort the first half
    sort(arr, arr+n/2);
    sort(arr+n/2, arr+n);
  
    reverse(arr+n/2, arr+n);
}
  
int main()
{
    int arr[] = { 5, 4, 6, 2, 1, 3, 8, 9, 7 };
    int n = sizeof(arr)/sizeof(arr[0]);
    mySort(arr, n);
    cout << "Modified Array : \n";
    for (int i=0; i


Java
// Java program to sort first half in increasing
// order and second half in decreasing
import java.util.*;
  
public class SortExample {
    static void mySort(Integer[] arr)
    {
        int n = arr.length;
          
        // Sort the whole array 
        Arrays.sort(arr, 0, n/2);
        Arrays.sort(arr, n/2, n);
  
        // Reverse the second half
        int low = n/2, high = n-1;
        while (low < high)
        {
            Integer temp = arr[low];
            arr[low] = arr[high];
            arr[high] = temp;
            low++; high--;
        }  
    }
  
    public static void main(String[] args)
    {
        // Our arr contains 8 elements
        Integer[] arr = { 5, 4, 6, 2, 1, 3, 8, 9, 7 };
        mySort(arr);
        System.out.printf("Modified arr[] : %s",
                          Arrays.toString(arr));
    }
}


Python3
# Python3 program to sort first half in increasing
# order and second half in decreasing
def mySort(arr):
    n = len(arr);
  
    # Sort the whole array
    arr1 = arr[:n // 2]
    arr2 = arr[n // 2:]
    arr1.sort()
    arr2.sort()
    arr = arr1 + arr2
  
    # Reverse the second half
    low = n // 2;
    high = n - 1;
    while (low < high):
        temp = arr[low];
        arr[low] = arr[high];
        arr[high] = temp;
        low += 1;
        high -= 1;
    return arr;
  
# Driver code
if __name__ == '__main__':
      
    # Our arr contains 8 elements
    arr = [5, 4, 6, 2, 1, 3, 8, 9, 7];
    arr = mySort(arr);
    print("Modified Array : ")
    print(arr)
  
# This code is contributed by 29AjayKumar


C#
// C# program to sort first half in increasing 
// order and second half in decreasing 
using System;
  
public class SortExample 
{ 
    static void mySort(int[] arr) 
    { 
        int n = arr.Length; 
          
        // Sort the whole array 
        Array.Sort(arr, 0, n/2); 
        Array.Sort(arr, n/2, n/2+1); 
  
        // Reverse the second half 
        int low = n/2, high = n-1; 
        while (low < high) 
        { 
            int temp = arr[low]; 
            arr[low] = arr[high]; 
            arr[high] = temp; 
            low++; high--; 
        } 
    } 
  
    // Driver code
    public static void Main(String[] args) 
    { 
        // Our arr contains 8 elements 
        int[] arr = { 5, 4, 6, 2, 1, 3, 8, 9, 7 }; 
        mySort(arr); 
        Console.WriteLine("Modified arr[] : {0}", 
                        String.Join(", ",arr)); 
    } 
} 
  
// This code has been contributed by 29AjayKumar


输出:
Modified array :
2 4 5 6 9 8 7 3 1 

替代解决方案
1)以升序对整个数组进行排序。
2)排序后将后半部分反转。

C++

// C++ program to sort first half in increasing
// order and second half in decreasing
#include
using namespace std;
  
void mySort(int arr[], int n)
{
    // Sort the first half
    sort(arr, arr+n/2);
    sort(arr+n/2, arr+n);
  
    reverse(arr+n/2, arr+n);
}
  
int main()
{
    int arr[] = { 5, 4, 6, 2, 1, 3, 8, 9, 7 };
    int n = sizeof(arr)/sizeof(arr[0]);
    mySort(arr, n);
    cout << "Modified Array : \n";
    for (int i=0; i

Java

// Java program to sort first half in increasing
// order and second half in decreasing
import java.util.*;
  
public class SortExample {
    static void mySort(Integer[] arr)
    {
        int n = arr.length;
          
        // Sort the whole array 
        Arrays.sort(arr, 0, n/2);
        Arrays.sort(arr, n/2, n);
  
        // Reverse the second half
        int low = n/2, high = n-1;
        while (low < high)
        {
            Integer temp = arr[low];
            arr[low] = arr[high];
            arr[high] = temp;
            low++; high--;
        }  
    }
  
    public static void main(String[] args)
    {
        // Our arr contains 8 elements
        Integer[] arr = { 5, 4, 6, 2, 1, 3, 8, 9, 7 };
        mySort(arr);
        System.out.printf("Modified arr[] : %s",
                          Arrays.toString(arr));
    }
}

Python3

# Python3 program to sort first half in increasing
# order and second half in decreasing
def mySort(arr):
    n = len(arr);
  
    # Sort the whole array
    arr1 = arr[:n // 2]
    arr2 = arr[n // 2:]
    arr1.sort()
    arr2.sort()
    arr = arr1 + arr2
  
    # Reverse the second half
    low = n // 2;
    high = n - 1;
    while (low < high):
        temp = arr[low];
        arr[low] = arr[high];
        arr[high] = temp;
        low += 1;
        high -= 1;
    return arr;
  
# Driver code
if __name__ == '__main__':
      
    # Our arr contains 8 elements
    arr = [5, 4, 6, 2, 1, 3, 8, 9, 7];
    arr = mySort(arr);
    print("Modified Array : ")
    print(arr)
  
# This code is contributed by 29AjayKumar

C#

// C# program to sort first half in increasing 
// order and second half in decreasing 
using System;
  
public class SortExample 
{ 
    static void mySort(int[] arr) 
    { 
        int n = arr.Length; 
          
        // Sort the whole array 
        Array.Sort(arr, 0, n/2); 
        Array.Sort(arr, n/2, n/2+1); 
  
        // Reverse the second half 
        int low = n/2, high = n-1; 
        while (low < high) 
        { 
            int temp = arr[low]; 
            arr[low] = arr[high]; 
            arr[high] = temp; 
            low++; high--; 
        } 
    } 
  
    // Driver code
    public static void Main(String[] args) 
    { 
        // Our arr contains 8 elements 
        int[] arr = { 5, 4, 6, 2, 1, 3, 8, 9, 7 }; 
        mySort(arr); 
        Console.WriteLine("Modified arr[] : {0}", 
                        String.Join(", ",arr)); 
    } 
} 
  
// This code has been contributed by 29AjayKumar
输出:
Modified arr[] : [2, 4, 5, 6, 9, 8, 7, 3, 1]

两种方法的时间复杂度均为O(n Log n)