In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Editor to share with you how the java sorting algorithm to achieve Hill sorting, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's learn about it!
1. Introduction.
Hill sorting (Shell's Sort) is a kind of insertion sorting, also known as "reduced incremental sorting" (Diminishing Increment Sort), which is a more efficient and improved version of the direct insertion sorting algorithm. Hill sorting is an unstable sorting algorithm. This method is named after D.L.Shell proposed in 1959. 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.
Hill sort is an unstable sort, does not need additional memory, and the space complexity is O (1). Time complexity, best case: O (log ^ 2n) worst case: O (log ^ 2n) average case: O (nlogn).
2. Steps.
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 increment sequence of Hill ordering is a mathematical problem. We choose the increment sequence of XM rebate www.kaifx.cn/broker/xm.html, which is also the increment suggested by Hill, which is called Hill increment, but in fact, this increment sequence is not optimal. Here we do an example using Hill Increment. First, the entire record sequence to be sorted is divided into several sub-sequences for direct insertion sorting, specific algorithm description: select an incremental sequence T1 T2, … , tk, where ti > tj,tk=1; sorts the sequence k times according to the number of incremental sequences k; each sorting, according to the corresponding incremental ti, divides the sequence to be sorted into several subsequences of length m, and directly inserts and sorts each subtable. Only when the increment factor is 1, the whole sequence is treated as a table, and the table length is the length of the whole sequence.
3. Code.
Public static void main (String [] args) {
System.out.println ("- start -")
/ / generate two random arrays, which are sorted by the system's own method and verified at that time.
Final int number = 100000
Int [] sortArray = new int [number]
Int [] sortArrayCopy = new int [number]
For (int I = 0; I
< sortArray.length; i++) { sortArray[i] = (int) (Math.random() number); } System.arraycopy(sortArray, 0, sortArrayCopy, 0, number);//数组复制 Arrays.sort(sortArrayCopy); //开始排序 long startTime = System.currentTimeMillis(); shellInsertSort(sortArray);//希尔插入排序 System.out.println("花费时间:" + (System.currentTimeMillis() - startTime)); //跟系统排序之后数组进行比较,查看是否排序成功。 if (Arrays.equals(sortArray, sortArrayCopy)) { System.out.println("排序成功"); } else { System.out.println("排序失败"); } System.out.println("------结束------"); } //希尔插入排序 最佳情况:T(n) = O(nlog2 n) 最坏情况:T(n) = O(nlog2 n) 平均情况:T(n) =O(nlog2n) private static void shellInsertSort(int[] array) { int groups = array.length / 2;//增量,一共的组数 while (groups >0) {
/ / if you think of groups as 1, it will be exactly the same as the algorithm that inserts sorting directly.
For (int I = groups; I
< array.length; i++) {//n-1轮 第一个无需排序 int curIndex = i; while (curIndex >Groups-1) {
If (array [curIndex] > array [curIndex-groups]) {
Break
}
Int flag = array [curIndex]
Array [curIndex] = array [curIndex-groups]
Array [curIndex-groups] = flag
CurIndex-= groups
}
}
Groups / = 2
}
}
# include
Using namespace std
Void shellSort (int a, int n)
{
Int gap,i,j,temp
For (gap = n / 2; gap > 0 / 2)
{
For (I = gap; I
< n;i++) { if (a[i]= 0 && a[j] >Temp;j-=gap)
{
A [j + gap] = a [j]
}
A [j + gap] = temp
}
}
}
}
Int main (int argc, char argv [])
{
Int a [10] = {5pjcr 3jpg 4pjm 8pr 6pr 1jr 2jr 9jr 7je 10}
ShellSort (a, 10)
For (int I = 0; I
< 10;i++) { cout h - 1 && arr[j - h] >= tmp) {
Arr [j] = arr [j-h]
J-= h
}
Arr [j] = tmp
}
/ / reduce the interval
H = (h-1) / 3
}
}
}
The above is all the content of the article "how to achieve Hill sorting by java sorting algorithm". 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.
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.