原文:

给定一个由 n 个元素组成的数组。任务是以这样的方式打印数组元素:前两个元素按升序排列,后三个按降序排列,后四个按升序排列,依此类推。

示例:

输入 : arr = {2,6,2,4,0,1,4,8,2,0,0,5,2,2} 输出:0 0 8 6 5 0 1 2 4 2 2 2 2

输入 : arr = {1,2,3,4,5,6} 输出 : 1 2 6 5 4 3

来源 : 思路是使用 。首先按递增顺序对数组进行排序,并保持两个指针和,其中是按递增顺序打印数组,是按递减顺序打印数组。保留一个变量以指定迭代中要打印的元素数量,并保留一个变量标志以交替地在升序和降序打印之间切换。

下面是上述方法的实现:

c

// c   program to print array elements in
// alternative increasing and decreasing
// order
#include 
using namespace std;
// function to print array elements in
// alternative increasing and decreasing
// order
void printarray(int arr[], int n)
{
    // first sort the array in increasing order
    sort(arr, arr   n);
    int l = 0, r = n - 1, flag = 0, i;
    // start with 2 elements in
    // increasing order
    int k = 2;
    // till all the elements are not printed
    while (l <= r) {
        // printing the elements in
        // increasing order
        if (flag == 0) {
            for (i = l; i < l   k && i <= r; i  )
                cout << arr[i] << " ";
            flag = 1;
            l = i;
        }
        else // printing the elements in
        // decreasing order
        {
            for (i = r; i > r - k && i >= l; i--)
                cout << arr[i] << " ";
            flag = 0;
            r = i;
        }
        // increasing the number of elements
        // to printed in next iteration
        k  ;
    }
}
// driver code
int main()
{
    int n = 6;
    int arr[] = { 1, 2, 3, 4, 5, 6 };
    printarray(arr, n);
    return 0;
}

java 语言(一种计算机语言,尤用于创建网站)

// java program to print array elements in
// alternative increasing and decreasing
// order
import java.util.*;
class solution
{
// function to print array elements in
// alternative increasing and decreasing
// order
static void printarray(int arr[], int n)
{
    // first sort the array in increasing order
    arrays.sort(arr);
    int l = 0, r = n - 1, flag = 0, i;
    // start with 2 elements in
    // increasing order
    int k = 2;
    // till all the elements are not printed
    while (l <= r) {
        // printing the elements in
        // increasing order
        if (flag == 0) {
            for (i = l; i < l   k && i <= r; i  )
                system.out.print(arr[i]   " ");
            flag = 1;
            l = i;
        }
        else // printing the elements in
        // decreasing order
        {
            for (i = r; i > r - k && i >= l; i--)
                system.out.print(arr[i]   " ");
            flag = 0;
            r = i;
        }
        // increasing the number of elements
        // to printed in next iteration
        k  ;
    }
}
// driver code
public static void main(string args[])
{
    int n = 6;
    int arr[] = { 1, 2, 3, 4, 5, 6 };
    printarray(arr, n);
}
}
//contributed by arnab kundu

python 3

# python 3 program to print array elements
# in alternative increasing and decreasing
# order
# function to print array elements in
# alternative increasing and decreasing
# order
def printarray(arr, n):
    # first sort the array in
    # increasing order
    arr.sort()
    l = 0
    r = n - 1
    flag = 0
    # start with 2 elements in
    # increasing order
    k = 2
    # till all the elements are not printed
    while (l <= r) :
        # printing the elements in
        # increasing order
        if (flag == 0):
            i = l
            while i < l   k and i <= r:
                print(arr[i], end = " ")
                i  = 1
            flag = 1
            l = i
        else:     # printing the elements in
                 # decreasing order
            i = r
            while i > r - k and i >= l:
                print(arr[i], end = " ")
                i -= 1
            flag = 0
            r = i
        # increasing the number of elements
        # to printed in next iteration
        k  = 1
# driver code
if __name__ == "__main__":
    n = 6
    arr = [ 1, 2, 3, 4, 5, 6 ]
    printarray(arr, n)
# this code is contributed by ita_c

c

// c# program to print array elements in
// alternative increasing and decreasing
// order
using system;
class gfg{
// function to print array elements in
// alternative increasing and decreasing
// order
static void printarray(int []arr, int n)
{
    // first sort the array
    // in increasing order
    array.sort(arr);
    int l = 0, r = n - 1, flag = 0, i;
    // start with 2 elements in
    // increasing order
    int k = 2;
    // till all the elements
    // are not printed
    while (l <= r) {
        // printing the elements in
        // increasing order
        if (flag == 0) {
            for (i = l; i < l   k && i <= r; i  )
                    console.write(arr[i]   " ");
            flag = 1;
            l = i;
        }
        else
        // printing the elements in
        // decreasing order
        {
            for (i = r; i > r - k && i >= l; i--)
                console.write(arr[i]   " ");
            flag = 0;
            r = i;
        }
        // increasing the number of elements
        // to printed in next iteration
        k  ;
    }
}
// driver code
static public void main ()
{
    int n = 6;
    int []arr = { 1, 2, 3, 4, 5, 6 };
    printarray(arr, n);
}
}
// this code is contributed by sach_code

服务器端编程语言(professional hypertext preprocessor 的缩写)

 $r - $k &&
                 $i >= $l; $i--)
                echo $arr[$i] , " ";
            $flag = 0;
            $r = $i;
        }
        // increasing the number of elements
        // to printed in next iteration
        $k  ;
    }
}
// driver code
$n = 6;
$arr = array( 1, 2, 3, 4, 5, 6 );
printarray($arr, $n);
// this code is contributed by jit_t
?>

java 描述语言


output: 

1 2 6 5 4 3

时间复杂度:o(nlogn) t3】辅助空间: o(1)