Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to choose sorting and its optimization

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "how to choose sorting and its optimization". The content of the explanation in the article is simple and clear, and it is easy to learn and understand. let's study and learn "how to choose sorting and its optimization".

Let's first take a look at the original version.

For (int I = 0; I

< beg.length - 1; i++) {for (int j = i + 1; j < beg.length; j++) {if (beg[i] >

= beg [j]) {int temp = beg [j]; beg [j] = beg [I]; beg [I] = temp;}} for (int k = 0; k

< beg.length; k++) { System.out.print(beg[k] + ",");} 我们可以发现外面的循环每次都是第i个数和剩下的length-i-1个数做比较 如下图

Optimization:

Each time we traverse to find the maximum and minimum values, then we will reduce the number of loops by 1 prime 2.

For (int I = 0; I

< (beg.length - 1) / 2; i++) {int min = i;int max = i;for (int j = i + 1; j < beg.length - i; j++) { min = beg[i] >

= beg [j]? J: min; max = beg [I] > = beg [j]? Max: J;} if (min + max = = beg.length-1) {int temp3 = beg [beg.length-1-I]; beg [beg.length-1-I] = beg [I]; beg [I] = temp3;} else {int temp = beg [min]; int temp2 = beg [max]; beg [min] = beg [I]; beg [max] = beg [beg.length-1-I]; beg [I] = temp;beg [beg.length-1-I] = temp2 }} for (int k = 0; k

< beg.length; k++) { System.out.print(beg[k] + ",");} 最外层遍历少了1/2 而里层遍历又少随着最大值和最小值的缩小而缩小区间

Here we need to consider the case of the extreme value, that is, the exchange of the maximum and minimum values. For example, 1 and 9 in the figure happen to be the maximum and minimum values.

Thank you for your reading. the above is the content of "how to choose sorting and its optimization". After the study of this article, I believe you have a deeper understanding of how to choose sorting and its optimization. the specific use situation also needs to be verified by practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report