In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces java how to achieve the insertion sorting algorithm, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian take you to understand.
Insert sort, which is very common in daily life, such as getting messy hand order when playing poker, sorting out large and small banknotes, and so on.
Insert is to insert the target element into an element that has been sorted.
So we need two layers of loops. The first loop takes each element as the sorted element, and the second loop inserts the sorted element into the correct location.
That seems easy to say. In fact, for the second layer loop, insert the element in the right place. I think there are two ways. First, the element to be inserted uses the bubbling method to compare and loop to find the correct position, and the other is to compare the elements to be sorted with the elements that have already been sorted, get the correct corner mark, and finally achieve the insertion.
For the first method, it doesn't feel like an insert, because it loops through comparison and gets the correct position by moving. The second method ends by inserting it into the right place because it is only relatively large. The efficiency is twice as high as the first one.
So look at the code implementation.
The first kind of realization, efficiency underground.
@ Override public void sort (int [] a) {int len=a.length; for (int iTunes 1 / SortUtils.exch 1 / a [j]) {/ / start comparing SortUtils.less (a [juni1], a [j]) / / swap the elements to be sorted with the elements that have already been sorted.
The second implementation is more efficient.
@ Override public void sort (int [] a) {int len=a.length; for (int iTunes 1 to compare SortUtils.less (e, a [j])) {/ / start to compare a [juni1] = a [j]; / / simply move elements, which is faster than the above method. / / Loop to find the correct location of the elements to be sorted, jroom1} a [juni1] = e / / because the inner while loop ends with finding the first array element that is smaller than the element to be inserted, and the corner mark of this element is j _ so the correct place to insert is _ I hope the article "how to implement the insertion sorting algorithm in java" shared by the editor is helpful to everyone. At the same time, I also hope that you can support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.