📌  相关文章
📜  上半部分按升序排序,后半部分按降序排序| 1个

📅  最后修改于: 2021-04-29 14:28:20             🧑  作者: Mango

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

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

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

算法 :
1.对给定的数组进行排序。
2.循环运行,直到数组长度的一半,然后打印已排序数组的元素。
3.从数组的最后一个索引到数组的中间运行一个循环,并以相反的顺序打印元素。
下面是相同的实现。

C++
// C++ program to print first half in
// ascending order and the second half
// in descending order
#include 
using namespace std;
 
// function to print half of the array in
// ascending order and the other half in
// descending order
void printOrder(int arr[], int n)
{
    // sorting the array
    sort(arr, arr + n);
 
    // printing first half in ascending
    // order
    for (int i = 0; i < n / 2; i++)
        cout << arr[i] << " ";   
 
    // printing second half in descending
    // order
    for (int j = n - 1; j >= n / 2; j--)
        cout << arr[j] << " ";    
}
 
// driver code
int main()
{
    int arr[] = { 5, 4, 6, 2, 1, 3, 8, 9, 7 };
    int n = sizeof(arr) / sizeof(arr[0]);
    printOrder(arr, n);
 
    return 0;
}


Java
// Java program to print first half in
// ascending order and the second half
// in descending order
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)
{
    // sorting the array
    Arrays.sort(arr);
 
    // printing first half in ascending
    // order
    for (int i = 0; i < n / 2; i++)
        System.out.print(arr[i]+" ");
 
    // printing second half in descending
    // order
    for (int j = n - 1; j >= n / 2; j--)
    System.out.print(arr[j]+" ");
     
}
 
// Driver code
public static void main(String[] args)
{
    int[] arr = { 5, 4, 6, 2, 1, 3, 8, 9, 7 };
    int n = arr.length;
    printOrder(arr, n);
 
}
}
/* This code is contributed by Mr. Somesh Awasthi */


Python
# Python program to print first half in
# ascending order and the second half
# in descending order
 
 
# function to print half of the array in
# ascending order and the other half in
# descending order
def printOrder(arr,n) :
     
    # sorting the array
    arr.sort()
  
    # printing first half in ascending
    # order
    i = 0
    while (i< n/ 2 ) :
        print arr[i],
        i = i + 1
         
    # printing second half in descending
    # order
    j = n - 1
    while j >= n / 2 :
        print arr[j],
        j = j - 1
         
# Driver code
arr = [5, 4, 6, 2, 1, 3, 8, 9, 7]
n = len(arr)
printOrder(arr, n)
 
# This code is contributed by Nikita Tiwari.


C#
// C# program to print first half in
// ascending order and the second half
// in descending order
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)
    {
         
        // sorting the array
        Array.Sort(arr);
     
        // printing first half in ascending
        // order
        for (int i = 0; i < n / 2; i++)
            Console.Write(arr[i] + " ");
     
        // printing second half in descending
        // order
        for (int j = n - 1; j >= n / 2; j--)
            Console.Write(arr[j] + " ");
    }
     
    // Driver code
    public static void Main()
    {
        int[] arr = { 5, 4, 6, 2, 1, 3, 8, 9, 7 };
        int n = arr.Length;
         
        printOrder(arr, n);
    }
}
 
// This code is contributed by vt_m.


PHP
= intval($n / 2); $j--)
        echo $arr[$j] . " ";    
}
 
    // Driver Code
    $arr = array(5, 4, 6, 2, 1,
                    3, 8, 9, 7);
    $n = count($arr);
    printOrder($arr, $n);
 
// This code is contributed by Sam007
?>


Javascript


C++
// C++ program to print first half in
// ascending order and the second half
// in descending order
#include 
using namespace std;
 
// function to print half of the array in
// ascending order and the other half in
// descending order
void printOrder(int arr[], int n)
{
    // sorting the array
    sort(arr, arr + n);
 
    // printing first half in ascending
    // order
    for (int i = 0; i < n / 2; i++)
        cout << arr[i] << " ";
 
    // printing second half in descending
    // order
    for (int j = n - 1; j >= n / 2; j--)
        cout << arr[j] << " ";    
}
 
// driver code
int main()
{
    int arr[] = { 5, 4, 6, 2, 1, 3, 8, -1 };
    int n = sizeof(arr) / sizeof(arr[0]);
    printOrder(arr, n);
 
    return 0;
}


Java
// Java program to print first half in
// ascending order and the second half
// in descending order
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)
    {
        // sorting the array
        Arrays.sort(arr);
 
        // printing first half in ascending
        // order
        for (int i = 0; i < n / 2; i++)
        {
            System.out.print(arr[i] + " ");
        }
 
        // printing second half in descending
        // order
        for (int j = n - 1; j >= n / 2; j--)
        {
            System.out.print(arr[j] + " ");
        }
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int[] arr = {5, 4, 6, 2, 1, 3, 8, -1};
        int n = arr.length;
        printOrder(arr, n);
    }
}
 
// This code has been contributed by 29AjayKumar


Python 3
# Python 3 program to print first half
# in ascending order and the second half
# in descending order
 
# function to print half of the array in
# ascending order and the other half in
# descending order
def printOrder(arr, n):
 
    # sorting the array
    arr.sort()
 
    # printing first half in ascending
    # order
    for i in range(n // 2):
        print(arr[i], end = " ")
 
    # printing second half in descending
    # order
    for j in range(n - 1, n // 2 -1, -1) :
        print(arr[j], end = " ")
 
# Driver code
if __name__ == "__main__":
     
    arr = [ 5, 4, 6, 2, 1, 3, 8, -1 ]
    n = len(arr)
    printOrder(arr, n)
 
# This code is contributed by ita_c


C#
// C# program to print first half in
// ascending order and the second half
// in descending order
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)
{
    // sorting the array
    Array.Sort(arr);
 
    // printing first half in ascending
    // order
    for (int i = 0; i < n / 2; i++)
        Console.Write(arr[i] + " ");
 
    // printing second half in descending
    // order
    for (int j = n - 1; j >= n / 2; j--)
        Console.Write(arr[j] + " ");    
}
 
// Driver code
public static void Main()
{
    int[] arr = {5, 4, 6, 2, 1, 3, 8, -1};
    int n = arr.Length;
    printOrder(arr, n);
}
}
 
// This code is contributed
// by Akanksha Rai


PHP
= $n / 2; $j--)
        echo $arr[$j] . " ";    
}
 
// Driver code
$arr = array(5, 4, 6, 2, 1, 3, 8, -1);
$n = sizeof($arr);
printOrder($arr, $n);
 
// This code is contributed
// by Akanksha Rai
?>


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

替代解决方案:
在此,数组第一半的元素将保留在第一半的元素中,但在第一半中以升序排列,而数组第二半的元素将保留在第二半的元素中,但在第二半的元素之间按降序排列一半。
1.仅按输入数组第一半中的元素就需要按升序排序(因为这样,数组第一半中的原始元素将保留在数组的第一半中,所以仅按升序对数组的第一半进行排序)。前半部分,但按升序排列)。
2.仅以降序对数组第二半的元素进行排序,因为仅输入数组第二半的元素需要按降序排序(这样,数组第二半的原始元素将保留在数组的第二个半数中)。前半部分,但按降序排列)。

C++

// C++ program to print first half in
// ascending order and the second half
// in descending order
#include 
using namespace std;
 
// function to print half of the array in
// ascending order and the other half in
// descending order
void printOrder(int arr[], int n)
{
    // sorting the array
    sort(arr, arr + n);
 
    // printing first half in ascending
    // order
    for (int i = 0; i < n / 2; i++)
        cout << arr[i] << " ";
 
    // printing second half in descending
    // order
    for (int j = n - 1; j >= n / 2; j--)
        cout << arr[j] << " ";    
}
 
// driver code
int main()
{
    int arr[] = { 5, 4, 6, 2, 1, 3, 8, -1 };
    int n = sizeof(arr) / sizeof(arr[0]);
    printOrder(arr, n);
 
    return 0;
}

Java

// Java program to print first half in
// ascending order and the second half
// in descending order
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)
    {
        // sorting the array
        Arrays.sort(arr);
 
        // printing first half in ascending
        // order
        for (int i = 0; i < n / 2; i++)
        {
            System.out.print(arr[i] + " ");
        }
 
        // printing second half in descending
        // order
        for (int j = n - 1; j >= n / 2; j--)
        {
            System.out.print(arr[j] + " ");
        }
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int[] arr = {5, 4, 6, 2, 1, 3, 8, -1};
        int n = arr.length;
        printOrder(arr, n);
    }
}
 
// This code has been contributed by 29AjayKumar

的Python 3

# Python 3 program to print first half
# in ascending order and the second half
# in descending order
 
# function to print half of the array in
# ascending order and the other half in
# descending order
def printOrder(arr, n):
 
    # sorting the array
    arr.sort()
 
    # printing first half in ascending
    # order
    for i in range(n // 2):
        print(arr[i], end = " ")
 
    # printing second half in descending
    # order
    for j in range(n - 1, n // 2 -1, -1) :
        print(arr[j], end = " ")
 
# Driver code
if __name__ == "__main__":
     
    arr = [ 5, 4, 6, 2, 1, 3, 8, -1 ]
    n = len(arr)
    printOrder(arr, n)
 
# This code is contributed by ita_c

C#

// C# program to print first half in
// ascending order and the second half
// in descending order
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)
{
    // sorting the array
    Array.Sort(arr);
 
    // printing first half in ascending
    // order
    for (int i = 0; i < n / 2; i++)
        Console.Write(arr[i] + " ");
 
    // printing second half in descending
    // order
    for (int j = n - 1; j >= n / 2; j--)
        Console.Write(arr[j] + " ");    
}
 
// Driver code
public static void Main()
{
    int[] arr = {5, 4, 6, 2, 1, 3, 8, -1};
    int n = arr.Length;
    printOrder(arr, n);
}
}
 
// This code is contributed
// by Akanksha Rai

的PHP

= $n / 2; $j--)
        echo $arr[$j] . " ";    
}
 
// Driver code
$arr = array(5, 4, 6, 2, 1, 3, 8, -1);
$n = sizeof($arr);
printOrder($arr, $n);
 
// This code is contributed
// by Akanksha Rai
?>
输出:
-1 1 2 3 8 6 5 4