In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "analyzing the Hill sorting of the Java sorting algorithm". In the daily operation, I believe that many people have doubts in analyzing the Hill sorting problem of the Java sorting algorithm. The editor consulted all kinds of data and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "analyzing the Hill sorting algorithm of Java sorting algorithm". Next, please follow the editor to study!
Basic thought
Hill sorting is to group records according to a certain increment of subscript and sort each group using the direct insertion sorting algorithm; as the increment decreases gradually, each group contains more and more keywords, and when the increment is reduced to 1, the whole file is divided into a group. the algorithm is terminated.
Simple insertion sorting is very regular. No matter what the array distribution is, we still compare, move, and insert elements step by step, such as [5pjm, 4je, 3je, 2je, and d0]. It is very difficult for the 0 at the end of the array to return to the first position, and it takes nMuk to compare and move the elements. Hill sort adopts the strategy of jumping grouping in the array, dividing the array elements into several groups through a certain increment, then grouping for insertion sorting, then gradually reducing the increment, and continuing to insert and sort by group until the increment is 1. Hill sort through this strategy to make the whole array in the initial stage basically orderly from a macro point of view, the small is basically the first, and the big is the last. Then reduce the increment to 1, in fact, in most cases only need to fine-tune, will not involve too much data movement.
Let's take a look at the basic steps of Hill sorting, where we choose incremental gap=length/2 and shrink the increment to continue in the way of gap= gap/2. This incremental selection can be represented by a sequence, {nmax 2, (nmax 2) / 2. 1}, which is called an incremental sequence. The selection and proof of the incremental sequence of Hill ordering is a mathematical problem. The incremental sequence we choose is more commonly used, and it is also the increment suggested by Hill, which is called Hill increment, but in fact, this incremental sequence is not optimal.
Code implementation
In Hill's understanding of sorting, we tend to process each group group by group, but in the code implementation, we don't have to process one group step by step and then switch back to the next group (so we have to add a for loop to deal with the group). For example, [5Perry 4pm 3pm 2pm 1P 0], the first increment set gap=length/2=3, then it is 3 groups [5jue 2] [4jue 1] [3line 0], so there is no need to cycle through the group processing. We can start with the gap element and deal with it one by one across groups. At the same time, when inserting data, we can use the element exchange method to find the final location, or we can use the array element movement method to find the final location. The code for Hill sorting is relatively simple, as follows:
Package sortdemo;import java.util.Arrays;/** * Created by chengxiao on 2016-11-24. * / public class ShellSort {public static void main (String [] args) {int [] arr = {1 (arr1); System.out.println (Arrays.toString (arr1)); sort (arr); System.out.println (Arrays.toString (arr)); int [] arr1 = } / * * Hill sort uses the exchange method * @ param arr * / public static void sort (int [] arr) {/ / incremental gap for ordered sequences when inserting, and gradually reduces the incremental for (int gap=arr.length/2;gap > 0) Gap/=2) {/ / from the gap element, directly insert and sort the group one by one, for (int itemgapterial 0 & & arr [j] 0transfergaptable2) {/ / from the gap element, directly insert the sorting operation for (int itemgapteri) to the group one by one
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.