给定一个整数数组,以这样的方式打印数组:第一个元素是第一个最大值,第二个元素是第一个最小值,依此类推。 例如:
null
Input : arr[] = {7, 1, 2, 3, 4, 5, 6}Output : 7 1 6 2 5 3 4Input : arr[] = {1, 6, 9, 4, 3, 7, 8, 2}Output : 9 1 8 2 7 3 6 4
A. 简单解决方案 首先打印最大元素,然后打印最小元素,然后打印第二个最大元素,依此类推。该方法的时间复杂度为O(n) 2. ). 一 有效解决方案 包括以下步骤。 1) 使用O(n logn)算法对输入数组进行排序。 2) 我们在排序数组中维护两个指针,一个从开始,一个从结束。我们也可以打印由两个指针指向的元素,并将它们相互移动。
C++
// C++ program to print an array in alternate // sorted manner. #include <bits/stdc++.h> using namespace std; // Function to print alternate sorted values void alternateSort( int arr[], int n) { // Sorting the array sort(arr, arr+n); // Printing the last element of array // first and then first element and then // second last element and then second // element and so on. int i = 0, j = n-1; while (i < j) { cout << arr[j--] << " " ; cout << arr[i++] << " " ; } // If the total element in array is odd // then print the last middle element. if (n % 2 != 0) cout << arr[i]; } // Driver code int main() { int arr[] = {1, 12, 4, 6, 7, 10}; int n = sizeof (arr)/ sizeof (arr[0]); alternateSort(arr, n); return 0; } |
JAVA
// Java program to print an array in alternate // sorted manner import java.io.*; import java.util.Arrays; class AlternativeString { // Function to print alternate sorted values static void alternateSort( int arr[], int n) { Arrays.sort(arr); // Printing the last element of array // first and then first element and then // second last element and then second // element and so on. int i = 0 , j = n- 1 ; while (i < j) { System.out.print(arr[j--] + " " ); System.out.print(arr[i++] + " " ); } // If the total element in array is odd // then print the last middle element. if (n % 2 != 0 ) System.out.print(arr[i]); } /* Driver program to test above functions */ public static void main (String[] args) { int arr[] = { 1 , 12 , 4 , 6 , 7 , 10 }; int n = arr.length; alternateSort(arr, n); } } /*This code is contributed by Prakriti Gupta*/ |
Python3
# Python 3 program to print an array # in alternate sorted manner. # Function to print alternate sorted # values def alternateSort(arr, n): # Sorting the array arr.sort() # Printing the last element of array # first and then first element and then # second last element and then second # element and so on. i = 0 j = n - 1 while (i < j): print (arr[j], end = " " ) j - = 1 print (arr[i], end = " " ) i + = 1 # If the total element in array is odd # then print the last middle element. if (n % 2 ! = 0 ): print (arr[i]) # Driver code arr = [ 1 , 12 , 4 , 6 , 7 , 10 ] n = len (arr) alternateSort(arr, n) # This code is contributed by # Smitha Dinesh Semwal |
C#
// C# program to print an array in alternate // sorted manner using System; class AlternativeString { // Function to print alternate sorted values static void alternateSort( int [] arr, int n) { Array.Sort(arr); // Printing the last element of array // first and then first element and then // second last element and then second // element and so on. int i = 0, j = n - 1; while (i < j) { Console.Write(arr[j--] + " " ); Console.Write(arr[i++] + " " ); } // If the total element in array is odd // then print the last middle element. if (n % 2 != 0) Console.WriteLine(arr[i]); } /* Driver program to test above functions */ public static void Main() { int [] arr = { 1, 12, 4, 6, 7, 10 }; int n = arr.Length; alternateSort(arr, n); } } // This article is contributed by vt_m. |
PHP
<?php // PHP program to print an array in // alternate sorted manner. // Function to print alternate // sorted values function alternateSort( $arr , $n ) { // Sorting the array sort( $arr ); // Printing the last element // of array first and then // first element and then second // last element and then second // element and so on. $i = 0; $j = $n - 1; while ( $i < $j ) { echo $arr [ $j --]. " " ; echo $arr [ $i ++]. " " ; } // If the total element in array // is odd then print the last // middle element. if ( $n % 2 != 0) echo $arr [ $i ]; } // Driver code $arr = array (1, 12, 4, 6, 7, 10); $n = sizeof( $arr ) / sizeof( $arr [0]); alternateSort( $arr , $n ); // This code is contributed by Mithun Kumar ?> |
Javascript
<script> // JavaScript program to print an array in alternate // sorted manner. // Function to print alternate sorted values function alternateSort(arr, n) { // Sorting the array console.log(arr); arr.sort( function (a, b) { return a - b; }); console.log(arr); // Printing the last element of array // first and then first element and then // second last element and then second // element and so on. var i = 0, j = n - 1; while (i < j) { document.write(arr[j--] + " " ); document.write(arr[i++] + " " ); } // If the total element in array is odd // then print the last middle element. if (n % 2 != 0) document.write(arr[i]); } // Driver code var arr = [1, 12, 4, 6, 7, 10]; var n = arr.length; alternateSort(arr, n); // This code is contributed by rdtank. </script> |
输出:
12 1 10 4 7 6
时间复杂性: O(n日志n) 辅助空间: O(1) 本文由 萨钦·比什特 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄去评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END