Dada una array de números enteros, ordene la primera mitad de la array en orden ascendente y la segunda mitad en orden descendente.
Ejemplos:
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}
Algoritmo:
- Ordenar la array dada.
- Ejecute un ciclo hasta la mitad de la longitud de la array e imprima los elementos de la array ordenada.
- Ejecute un ciclo desde el último índice de la array hasta la mitad de la array e imprima los elementos en orden inverso.
A continuación se muestra la implementación de la misma.
C++
// C++ program to print first half in // ascending order and the second half // in descending order #include <bits/stdc++.h> 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
<?php // PHP 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 function printOrder($arr, $n) { // sorting the array sort($arr); // printing first half // in ascending order for ($i = 0; $i < intval($n / 2); $i++) echo $arr[$i] . " "; // printing second half // in descending order for ($j = $n - 1; $j >= 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
<script> // javascript 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 function printOrder(arr , n) { // sorting the array arr.sort(); // printing first half in ascending // order for (i = 0; i < parseInt(n / 2); i++) document.write(arr[i] + " "); // printing second half in descending // order for (j = n - 1; j >= parseInt(n / 2); j--) document.write(arr[j] + " "); } // Driver code var arr = [ 5, 4, 6, 2, 1, 3, 8, 9, 7 ]; var n = arr.length; printOrder(arr, n); // This code contributed by aashish1995 </script>
1 2 3 4 9 8 7 6 5
Complejidad de tiempo: O(n logn) , donde n representa el tamaño de la array dada.
Espacio auxiliar: O(1), no se requiere espacio adicional, por lo que es una constante.
Solución alternativa: aquí, los elementos de la primera mitad de la array permanecerán en la primera mitad pero en orden ascendente entre los elementos de la primera mitad, y los de la segunda mitad de la array permanecerán en la segunda mitad pero en orden descendente entre los elementos en la 2ª mitad.
- Ordene la primera mitad de la array en orden ascendente solo porque solo los elementos en la primera mitad de la array de entrada deben ordenarse en orden ascendente (de esta manera, los elementos originales en la primera mitad de la array permanecerán en la primera mitad pero en orden ascendente).
- Ordene la segunda mitad de la array en orden descendente solo porque solo los elementos en la segunda mitad de la array de entrada deben ordenarse en orden descendente (de esta manera, los elementos originales en la segunda mitad de la array permanecerán en la primera mitad pero en orden descendente).
Implementación:
C++
// C++ program to print first half in // ascending order and the second half // in descending order #include <bits/stdc++.h> 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
<?php // PHP 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 function printOrder($arr, $n) { // sorting the array sort($arr); // printing first half in ascending // order for ($i = 0; $i < $n / 2; $i++) echo $arr[$i] . " "; // printing second half in descending // order for ($j = $n - 1; $j >= $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 ?>
Javascript
<script> // Javascript 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 function printOrder(arr , n) { // sorting the array arr.sort(); // printing first half in ascending // order for (i = 0; i < parseInt(n / 2); i++) { document.write(arr[i] + " "); } // printing second half in descending // order for (j = n - 1; j >= parseInt(n / 2); j--) { document.write(arr[j] + " "); } } // Driver code var arr = [ 5, 4, 6, 2, 1, 3, 8, -1 ]; var n = arr.length; printOrder(arr, n); // This code contributed by Rajput-Ji </script>
-1 1 2 3 8 6 5 4
Complejidad de Tiempo: O(n logn)
Espacio Auxiliar: O(1)
Ordenar la primera mitad en orden ascendente y la segunda mitad en orden descendente | conjunto 2
Este artículo es una contribución de Ayush Saxena . Si le gusta GeeksforGeeks y le gustaría contribuir, también puede escribir un artículo usando contribuya.geeksforgeeks.org o envíe su artículo por correo a review-team@geeksforgeeks.org. Vea su artículo que aparece en la página principal de GeeksforGeeks y ayude a otros Geeks.
Publicación traducida automáticamente
Artículo escrito por GeeksforGeeks-1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA