Example of implementation of Hill Sorting in python

Source: Internet
Author: User
This article mainly introduces how to implement Hill Sorting in python and how to implement Hill sorting by programming, which has some reference value, interested friends can refer to this article mainly to introduce how to implement Hill Sorting in python. The Hill sorting implemented by programming has some reference value. interested friends can refer to it.

Observe "insert sort": in fact, it is not difficult to find that she has a disadvantage:

If the data is "5, 4, 3, 2, 1", we insert the records in the "unordered block" into the "ordered block", it is estimated that the records will crash, each insert operation must move its position. the efficiency of Insertion sorting can be imagined.

Shell improved the algorithm based on this weakness and integrated the idea of "downgrading incremental sorting". it is actually quite simple, but it should be noted that:

The increment is not random, but regular.

Def ShellInsetSort (array, len_array, dk): # insert the sort directly for I in range (dk, len_array ): # Sort by position = I current_val = array [position] # index = I j = int (index/dk) # index and dk operator index = index-j * dk # while True: # Find the first subscript. in incremental dk, the first subscript index must be 0 <= index
 
  
Index. the subscript of the number to be inserted must be greater than the first subscript while position> index and current_val <array [position-dk]: array [position] = array [position-dk] # Move position later = position-dk else: array [position] = current_valdef ShellSort (array, len_array ): # hill sort dk = int (len_array/2) # incremental while (dk> = 1): ShellInsetSort (array, len_array, dk) print (">:", array) dk = int (dk/2) if _ name _ = "_ main _": array = [49, 38, 65, 97, 76, 13, 27, 49, 55, 4] print (">:", array) ShellSort (array, len (array ))
 

Output:

>: [49, 38, 65, 97, 76, 13, 27, 49, 55, 4]
>>: [13, 27, 49, 55, 4, 49, 38, 65, 97, 76]
>>: [4, 27, 13, 49, 38, 55, 49, 65, 97, 76]
>>: [4, 13, 27, 38, 49, 49, 55, 65, 76, 97]

First, you have to insert the sorting, but you will not understand it.

While True: # Find the first subscript. in the incremental dk, the first subscript index must be 0 <= index
 
  

In Debug, it is too time-consuming to use a loop. especially when incremental d is set to 1, insert the sort directly to insert the last number of the list, and reduce the number by 1, after the subscript of the first number, I learned how to use the following method:


J = int (index/dk) # index and dk operator index = index-j * dk

Time complexity:

The time complexity of Hill sorting is the function of incremental sequence, which is difficult to analyze accurately. Some documents have pointed out that when the incremental sequence is d [k] = 2 ^ (t-k + 1), the time complexity of Hill sorting is O (n ^ 1.5 ), t indicates the number of sorted shards.

Stability: unstable

Hill sorting effect:

Eight Sorting Algorithms in c ++

Visual perception of several common sorting algorithms

C # seven classical sorting algorithm series (below)

1. non-systematic learning is also a waste of time 2. be a technician who will appreciate the beauty, understand the art, and be artistic

The above is the detailed description of the example for implementing the Hill Sorting in python. For more details, please follow other related articles in the first PHP community!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.