[GeeksForGeeks] Sort an array in wave form

Given an unsorted array of integers, sort the array into a wave like array. An array arr[0...n-1] is sorted in wave form

if arr[0] >= arr[1] <= arr[2] >= arr[3] <= arr[4] >= ....

Solution 1. O(n * logn) runtime, using sorting

1. sort the input array.

2. swap all adjacent elements.

 1 import java.util.Arrays;
 2 public class Solution {
 3     public void sortInWaveForm(int[] nums){
 4         if(nums == null){
 5             return;
 6         }
 7         Arrays.sort(nums);
 8         for(int i = 0; i < nums.length - 1; i += 2){
 9             swap(nums, i, i + 1);
10         }
11     }
12     private void swap(int[] nums, int idx1, int idx2){
13         int temp = nums[idx1];
14         nums[idx1] = nums[idx2];
15         nums[idx2] = temp;
16     }
17     public static void main(String[] args){
18         int[] nums1 = {3, 4, 2, 1, 5};
19         int[] nums2 = {3, 5, 2, 4, 1, 6};
20         Solution sol = new Solution();
21         sol.sortInWaveForm(nums1);
22         for(int i = 0; i < nums1.length; i++){
23             System.out.print(nums1[i] + " ");
24         }
25         System.out.println();
26         sol.sortInWaveForm(nums2);
27         for(int i = 0; i < nums2.length; i++){
28             System.out.print(nums2[i] + " ");
29         }
30     }
31 }

Solution 2. O(n) runtime

Idea:  we only need to make sure that all even positioned elements are >= than their adjacent odd elements. No need to worry about odd positioned elements.

Algorithm:

Traverse all even positioned elements of input array, and do the following.

a. If current element is smaller than previous odd positioned element, swap previous and current.

b. If current element is smaller than next odd positioned element, swap next and current.

 1 public class Solution {
 2     public void sortInWaveForm(int[] nums){
 3         if(nums == null){
 4             return;
 5         }
 6         for(int i = 0; i < nums.length; i += 2){
 7             if(i > 0 && nums[i] < nums[i - 1]){
 8                 swap(nums, i, i - 1);
 9             }
10             if(i < nums.length - 1 && nums[i] < nums[i + 1]){
11                 swap(nums, i, i + 1);
12             }
13         }
14     }
15     private void swap(int[] nums, int idx1, int idx2){
16         int temp = nums[idx1];
17         nums[idx1] = nums[idx2];
18         nums[idx2] = temp;
19     }
20 }
时间: 2024-08-29 13:20:23

[GeeksForGeeks] Sort an array in wave form的相关文章

[GeeksForGeeks] Convert an array to reduced form

Given an array with n distinct elements, convert the given array to a form where all elements are in range from 0 to n-1. The order of elements is same, i.e., 0 is placed in place of smallest element, 1 is placed for second smallest element, - n-1 is

Minimum number of swaps required to sort an array

https://www.hackerrank.com/challenges/minimum-swaps-2/problem Minimum Swaps II You are given an unordered array consisting of consecutive integers  [1, 2, 3, ..., n] without any duplicates. You are allowed to swap any two elements. You need to find t

Sort the Array

Being a programmer, you like arrays a lot. For your birthday, your friends have given you an array a consisting of n distinct integers. Unfortunately, the size of a is too small. You want a bigger array! Your friends agree to give you a bigger array,

Codeforces Round #258 (Div. 2) B. Sort the Array

Being a programmer, you like arrays a lot. For your birthday, your friends have given you an array a consisting of n distinct integers. Unfortunately, the size of a is too small. You want a bigger array! Your friends agree to give you a bigger array,

Codeforces Round #258 (Div. 2) B. Sort the Array(简单题)

题目链接:http://codeforces.com/contest/451/problem/B ---------------------------------------------------------------------------------------------------------------------------------------------------------- 欢迎光临天资小屋:http://user.qzone.qq.com/593830943/ma

(周日赛)Sort the Array

题意:一段数字,逆置其中两个使其递增 DescriptionBeing a programmer, you like arrays a lot. For your birthday, your friends have given you an array a consisting of ndistinct integers. Unfortunately, the size of a is too small. You want a bigger array! Your friends agre

CodeForces 451B Sort the Array

Description Being a programmer, you like arrays a lot. For your birthday, your friends have given you an array a consisting of n distinct integers. Unfortunately, the size of a is too small. You want a bigger array! Your friends agree to give you a b

[Swift]LeetCode912.排序数组 | Sort an Array

原文链接:https://www.cnblogs.com/strengthen/p/10886335.html Given an array of integers nums, sort the array in ascending order. Example 1: Input: [5,2,3,1] Output: [1,2,3,5] Example 2: Input: [5,1,1,2,0,0] Output: [0,0,1,1,2,5]  Note: 1 <= A.length <= 1

LeetCode-912.Sort an Array

Given an array of integers nums, sort the array in ascending order. Example 1: Input: [5,2,3,1] Output: [1,2,3,5] Example 2: Input: [5,1,1,2,0,0] Output: [0,0,1,1,2,5]  Note: 1 <= A.length <= 10000 -50000 <= A[i] <= 50000 排序 参考https://www.cnbl