Python implements eight sorting algorithms (1) and python eight

Source: Internet
Author: User

Python implements eight sorting algorithms (1) and python eight

Sort

Sorting is a kind of operation that is often performed in a computer. Its purpose is to adjust a set of "unordered" record sequences to "ordered" record sequences. Internal sorting and external sorting. If the sorting process can be completed without access to external storage, this Sorting Problem is called internal sorting. Otherwise, if the number of records participating in sorting is large, the sorting process of the entire sequence cannot be completed completely in the memory and external storage needs to be accessed, this sort problem is called external sorting. The internal sorting process is a process of gradually expanding the sequence length of records.

See the picture to make your understanding clearer:

Assume that there are multiple records with the same keywords in the record sequence to be sorted. If the records are sorted, the relative sequence of these records remains unchanged, that is, in the original sequence, ri = rj, and ri before rj, while in the sorted sequence, ri is still before rj, it is called this sort algorithm is stable; otherwise it is called unstable.

Common sorting algorithms

Fast sorting, Hill sorting, heap sorting, and direct selection sorting are not stable sorting algorithms. Base sorting, Bubble sorting, direct insertion sorting, semi-insertion sorting, and Merge Sorting are stable sorting algorithms.

This article uses Python to implement the eight sorting algorithms: Bubble sorting, insert sorting, Hill sorting, fast sorting, Direct selection sorting, heap sorting, Merge Sorting, and base sorting.

1. Bubble Sort)

Algorithm principle:

A group of unordered data a [1], a [2],... A [n], which needs to be arranged in ascending order. First, compare the values of a [1] and a [2]. If a [1] is greater than a [2], the values of the two are exchanged. Otherwise, the values remain unchanged. Compare the values of a [2] and a [3]. If a [2] is greater than a [3], the values of the two are exchanged. Otherwise, the values remain unchanged. Compare the values of a [3] and a [4], and so on. Finally, compare the values of a [n-1] And a [n. In this case, the value of a [n] must be the largest in this set of data. Then, for a [1] ~ If a [n-1] processes a round in the same way, the value of a [n-1] must be a [1] ~ The largest value in a [n-1. Then, for a [1] ~ A [N-2] handles a round in the same way, and so on. Processing of n-1 round after a [1], a [2],... A [n] is arranged in ascending order. The descending order is similar to the ascending order. If a [1] is smaller than a [2], the values of the two are exchanged; otherwise, the values remain unchanged, and so on. In general, the maximum (or minimum) number after each round of sorting will be moved to the end of the data sequence. Theoretically, n (n-1)/two exchanges are required.

Advantages: Stability;
Disadvantage: slow. Only two adjacent data can be moved at a time.

Python code implementation:

#! /Usr/bin/env python # coding: UTF-8 ''' file: python-8sort.pydate: 9/1/17 AMauthor: lockeyemail: lockey@123.comdesc: python implements the eight sorting algorithms ''' lst1 = [67,445, 34] def bubble_sort_basic (lst1): lstlen = len (lst1); I = 0 while I <lstlen: for j in range (1, lstlen): if lst1 [J-1]> lst1 [j]: # compare the size of two adjacent elements, small elements floating lst1 [j], lst1 [J-1] = lst1 [J-1], lst1 [j] I + = 1 print 'sorted {}:{}'. format (I, lst1) print '------------------------------- 'Return lst1bubble_sort_basic (lst1)

Improvement of the Bubble Sorting Algorithm

For sequences with full disorder or no repeated elements, the above algorithms have no room for improvement in the same way of thinking. However, if there are repeated elements or some elements in a sequence are ordered, in this case, there will inevitably be unnecessary repeated sorting, so we can add a symbolic variable change in the sorting process to indicate whether there is data exchange in a certain sorting process, if data is not exchanged during a certain sort, it indicates that the data has been arranged as required, and sorting can be completed immediately to avoid unnecessary comparison. The improved sample code is as follows:

Lst2 = [67,445, 4, 34] def bubble_sort_improve (lst2): lstlen = len (lst2) I = 1; times = 0 while I> 0: times + = 1 change = 0 for j in range (1, lstlen): if lst2 [J-1]> lst2 [j]: # use tags to record whether there is data exchange in this round of sorting change = j lst2 [j], lst2 [J-1] = lst2 [J-1], lst2 [j] print 'sorted {}: {}'. format (times, lst2) # use the data exchange tag as the cyclic condition to determine whether to continue sorting. I = change return lst2bubble_sort_improve (lst2)

In either case, run the following:

It can be seen that the optimized algorithm reduces two rounds of sorting for some elements in an ordered sequence.

2. Selection Sort)

Algorithm principle:

Each trip selects the smallest (or largest) element from the data element to be sorted, and places it at the end of the sorted series until all the data elements to be sorted are arranged.
The Direct selection and sorting of files with n records can be directly selected and sorted through n-1 rows to get the ordered results:

① Initial status: the disordered area is R [1. n], and the ordered area is empty.
② Sorting by 1st bits
In the unordered zone R [1 .. n] selects the record R [k] with the minimum keyword, swaps it with the 1st records R [1] In the unordered area, so that R [1 .. 1] and R [2 .. n] into a new ordered area with one more record count and a new unordered area with one fewer record count.
......
③ Sort by I
At the beginning of the I-th sorting, the current ordered and disordered areas are R [1 .. I-1] and R (1 ≤ I ≤ N-1), respectively ). This sort field selects the record R [k] with the smallest keyword from the current unordered area and exchanges it with the R of the 1st records in the unordered area so that R [1 .. i] and R change to a new ordered area with one more record count and a new unordered area with one fewer record count.
In this way, the direct sorting of files with n records can be directly selected through n-1 to obtain the ordered results.

Advantage: the number of moving data is known (n-1 );
Disadvantage: The comparison is frequent and unstable.

Python code implementation:

#-*-Coding: UTF-8-*-''' Created on August 31, 2017 Running environment: win7.x86 _ 64 eclipse python3 @ author: Lockey ''' lst = [65,568, 23, 34, 65,8, 6,9] def selection_sort (lst): lstlen = len (lst) for I in range (0, lstlen): min = I for j in range (I + 1, lstlen): # search for the smallest index from I + 1. if lst [min]> lst [j]: min = j lst [min], lst [I] = lst [I], lst [min] # assign the minimum value to the position indicated by the outer index I after a layer traversal ends, assign The value of I to The minimum index print ('The {} sorted :{}'. format (I + 1, lst) return lstsorted = selection_sort (lst) print ('the sorted result is :{}'. format (sorted ))

Running result:

3. Insert sorting

Algorithm principle:

A group of sorted data in ascending order is known: a [1], a [2],… A [n], a group of unordered data B [1], B [2],... B [m], which must be combined into an ascending sequence. First, compare the values of B [1] and a [1]. If B [1] is greater than a [1], Skip, compare the values of B [1] and a [2]. If B [1] is still greater than a [2], Skip, until B [1] is smaller than a data in array a [x], a [x] ~ A [n] moves one digit backward, and inserts B [1] to the original position of a [x]. This completes the insertion of B [1. B [2] ~ B [m] is inserted in the same way. (If there is no array a, you can regard B [1] as an array a with n = 1)
Advantages: stable and fast;
Disadvantage: the number of comparisons is not certain. The more the comparisons, the more data is moved after the inserted point. Especially when the total amount of data is large, the linked list can solve this problem.

Algorithm complexity

If the target is to sort the sequences of n elements in ascending order, there are best cases and worst cases of adopting insert sorting. The best case is that the sequence is already in ascending order. In this case, the comparison operation needs to be performed (n-1) times. The worst case is that the sequence is sorted in descending order. At this time, we need to compare n (n-1)/two times. The value assignment operation for insert sorting adds (n-1) the number of comparison operations. On average, the time complexity of inserting a sorting algorithm is O (n ^ 2 ). Therefore, insert sorting is not suitable for sorting applications with large data volumes. However, if the amount of data to be sorted is small, for example, if the order is smaller than, insertion sorting is still a good choice.

Python code implementation:

#-*-Coding: UTF-8-*-''' Created on August 31, 2017 Running environment: win7.x86 _ 64 eclipse python3 @ author: Lockey ''' lst = [65,568, 23, 34, 65,8, 6,9] def insert_sort (lst): count = len (lst) for I in range (1, count ): key = lst [I] j = I-1 while j> = 0: if lst [j]> key: lst [j + 1] = lst [j] lst [j] = key j-= 1 print ('the {} sorted :{}'. format (I, lst) return lstsorted = insert_sort (lst) print ('the sorted result is :{}'. format (sorted ))

Running result:

It can be seen from the sorting process that an element is inserted into the sorted sequence each time, then sorted, and another element is inserted next time... The sorting ends until all elements are inserted.

4. Hill sorting

Shell Sort is a Sort of insert. Also known as downgrading incremental sorting, it is a more efficient and improved version that directly inserts the sorting algorithm. Hill sorting is a non-stable sorting algorithm. This method was named after DL. Shell was proposed in 1959.

Algorithm principle

The core of the algorithm is grouping (by step) and intra-group insertion sorting.

A group of unordered data a [1], a [2],... A [n], which needs to be arranged in ascending order. It is found that insertion sorting works well when n is not large. First, take an incremental d (d <n) and convert a [1], a [1 + d], a [1 + 2d]… Column a [2], a [2 + d], a [2 + 2d]… Column 2 ......, A [d], a [2d], a [3d]... Column is the last group, and so on. insert and sort in each group, and then take d' <d. Repeat the preceding operation until d = 1.

Python code implementation:

#! /Usr/bin/env python # coding: UTF-8 ''' file: python-8sort.pydate: 9/1/17 AMauthor: lockeyemail: lockey@123.comdesc: python implements eight sorting algorithms '''lst = [65,568, 23,] def shell_sort (lists): print 'orginal list is {}'. format (lst) count = len (lists) step = 2 times = 0 group = int (count/step) while group> 0: for I in range (0, group ): times + = 1 j = I + group while j <count: k = j-group key = lists [j] while k> = 0: if lists [k]> key: lists [k + group] = lists [k] lists [k] = key k-= group j + = group print 'the {} sorted :{}'. format (times, lists) group = int (group/step) print 'the final result is :{}'. format (lists) return listsshell_sort (lst)

Test result:

Process Analysis:

Step 1:

1-5: The sequence is divided into five groups (group = int (count/step). For example, column 1 is a group:

Then the insertion sorting is performed in each group, and the sequence is obtained after 5 (5 groups * 1 time) insertion sorting in the group:

The 1-5 sorted: [34, 65, 8, 6, 4, 65,568, 9, 23, 9]

Step 2:

6666-7777: The sequence is divided into two groups (group = int (group/step). For example, column 1 is a group:

 

The insertion sorting is performed in each group, and the sequence is obtained after 8 (2 groups * 4 times) insertion sorting in the group:

The 6-7 sorted: [4, 6, 8, 9, 23, 9, 34, 65,568, 65]

Step 3:

888888888: insert and sort the complete sequence obtained from the previous sorting result:

[4, 6, 8, 9, 23, 9, 34, 65,568, 65]

After 9 (1 group * 10-1) insertion sorting:

The final result is: [4, 6, 8, 9, 9, 23, 34, 65, 65,568]

It is difficult to analyze the sort timeliness of Hill. The comparison times of key codes and the number of records moving depend on the selection of incremental factor sequences, you can accurately estimate the number of comparison times and the number of movements of records for a specific case. No one has provided a method to select the best incremental factor sequence. Incremental factor sequences can have various methods, including odd and prime numbers. However, note that there are no common factors except 1 in incremental factors, the last increment factor must be 1. The hill sorting method is an unstable sorting method.

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

Related Article

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.