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 implement Hill sorting by Java, PHP and Python

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

Share

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

This article mainly introduces the relevant knowledge of "how to achieve Hill sorting by Java, PHP and Python". The editor shows you the operation process through an actual case. The operation method is simple, fast and practical. I hope this article "how to achieve Hill sorting by Java, PHP and Python" can help you solve the problem.

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 an improved method based on the following two properties of insertion sorting:

Insert sorting is efficient when operating on almost sorted data, that is, it can achieve the efficiency of linear sorting, but insert sorting is generally inefficient because insert sorting can only move data one bit at a time.

The basic idea of Hill sorting is that the whole sequence of records to be sorted is divided into several sub-sequences for direct insertion and sorting, and then all the records are directly inserted and sorted when the records in the whole sequence are "basically ordered".

Algorithm step

Select an incremental sequence T1 and T2,. , tk, where ti > tj, tk = 1

Sort the sequence k times according to the number of incremental sequences k

In each sorting, according to the corresponding incremental ti, the sequence to be arranged is divided into several subsequences of length m, and each subtable is sorted directly. 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.

Moving picture demonstration

Code implementation

JavaScript

Example

Function shellSort (arr) {var len = arr.length, temp, gap = 1; while (gap for (gap; gap > 0; gap = Math.floor (gap/3)) {for (var I = gap; i for (var j = gap; i for; j > = 0 & arr [j] > temp; j-=gap) {arr [j+gap] = arr [j];} arr [j+gap] = temp }} return arr;}

Python

Example

Def shellSort (arr): import math gap=1 while (gap while gap > 0: for i in range (gap,len (arr)): temp = arr [I] j = i-gap while j > = 0 and arr [j] > temp: arr [j+gap] = ARR [j] j-=gap arr [j+gap] = temp gap= math.floor (gap/3) return arr

Go

Example

Func shellSort (arr [] int) [] int {length: = len (arr) gap: = 1 for gap for gap > 0 {for I: = gap I for j > = 0 & & arr [j] > temp {arr [j+gap] = arr [j] j-= gap} arr [j+gap] = temp} gap = gap / 3} return arr}

Java

Example

Public static void shellSort (int [] arr) {int length = arr.length; int temp; for (int step = length / 2; step > = 1; step / = 2) {for (int I = step; i while (j > = 0 & & arr [j] > temp) {arr [j + step] = arr [j]; j-= step;} arr [j + step] = temp }}}

PHP

Example

Function shellSort ($arr) {$len = count ($arr); $temp = 0; $gap = 1; while ($gap $len / 3) {$gap = $gap * 3 + 1;} for ($gap; $gap > 0; $gap = floor ($gap / 3)) {for ($I = $gap; $I $len; $iTunes +) {$temp = $arr [$I]; for ($j = $I-$gap; $j > = 0 & $arr [$j] > $temp $j-= $gap) {$arr [$j+$gap] = $arr [$j];} $arr [$j+$gap] = $temp;}} return $arr;}

C

Example

Void shell_sort (int arr [], int len) {int gap, I, j; int temp; for (gap = len > > 1; gap > 0; gap > > = 1) for (I = gap; i for (j = I-gap; j > = 0 & arr [j] > temp; j-= gap) arr [j + gap] = arr [j] Arr [j + gap] = temp;}}

C++

Example

Templatevoid shell_sort (T array [], int length) {int h = 1; while (h while (h > = 1) {for (int I = h; i for (int j = I; j > = h & & array [j]) on "how Java, PHP, Python achieve Hill ranking", thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

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