In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly shows you "java how to achieve wobble sorting", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "java how to achieve wobble sorting" this article.
Given an unordered array nums, rearrange it into nums [0]
< nums[1] >Nums [2]
< nums[3]... 的顺序。 示例 1: 输入: 输出: 一个可能的答案是 示例 2: 输入: 输出: 一个可能的答案是 答案: 1public void wiggleSort(int[] nums) { 2 int n = nums.length, m = (n + 1) >> 1
3 int [] copy = Arrays.copyOf (nums, n)
4 Arrays.sort (copy)
5 for (int I = m-1, j = 0; I > = 0; imuri, j + = 2)
6 nums [j] = copy [I]
7 for (int I = n-1, j = 1; I > = m; imuri, j + = 2)
8 nums [j] = copy [I]
9}
Parsing:
First copy the nums into another array copy, and then sort the copy array. Here, the first half of the copy array is first inserted from back to front into the even digits of the array nums subscript (the subscript starts at 0), and then the second half of the array copy is inserted into the odd digits of the array nums subscript from back to front, because the array copy is sorted. So the value in the array nums will be that the value in the odd digit of the subscript is greater than its previous and latter one, and the value in the even digit of the subscript will be less than its previous and latter value. We can also write it differently.
1public void wiggleSort (int [] nums) {
2 int [] copy = Arrays.copyOf (nums, nums.length)
3 Arrays.sort (copy)
4 int n = nums.length
5 int left = (n + 1) / 2-1; / / intermediate index
6 int right = n-1; / / Index of the maximum value
7 for (int I = 0; I < nums.length; iTunes +) {
8 if (I% 2 = = 1) {
9 nums [I] = copy [right]
10 right--
11} else {
12 nums [I] = copy [left]
13 left--
14}
15}
16}
Although there is a slight difference in writing, the principle is still the same.
The above is all the content of the article "how to order the wobble in java". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.