Summarize the eight sorting algorithms for Python implementation (above)

Source: Internet
Author: User
This article mainly for you in detail the Python implementation of the first eight sorting algorithm, with a certain reference value, interested in small partners can refer to

Sort

Sorting is a frequent operation within a computer that is designed to adjust a set of "unordered" record sequences to an "ordered" sequence of records. Sort internally and externally. If the entire ordering process does not require access to external memory, then this sort problem is called an internal sort. Conversely, if the number of records participating in the sorting is very large, the whole sequence of the ordering process can not be completed completely in memory, need to access external memory, it is said that such sorting problem is an external sort. The process of internal sorting is a process of gradually enlarging the sequential sequence length of a record.

Look at the picture to make the understanding more clear and profound:

It is assumed that in the sequence of records to be sorted, there are multiple records with the same keyword, and if sorted, the relative order of the records remains the same, that is, in the original sequence, RI=RJ, and RI before RJ, and in the sorted sequence, RI is still before RJ, it is said that this sort algorithm is stable Otherwise, it is called unstable.

Common sorting algorithms

Fast sorting, hill sorting, heap sorting, direct selection sorting is not a stable sorting algorithm, while Cardinal sort, bubble sort, direct insert sort, binary insert sort, merge sort are stable sorting algorithms

In this paper, we will use Python to implement the eight sorting algorithms, such as bubble sort, insert sort, hill sort, quick sort, direct select sort, heap sort, merge sort, base sort.

1. Bubble sort (Bubble sort)

Algorithm principle:

You know a set of unordered data a[1], a[2] 、...... a[n], you need to sort them in ascending order. The value of a[1] and a[2] is compared first, if A[1] is greater than a[2, the value of both is exchanged, otherwise it is unchanged. Then compare the values of a[2] and a[3], and if a[2] is greater than a[3, the values of both are exchanged, otherwise they will not change. Compare A[3] with a[4], and so on, and finally compare the values of a[n-1] with A[n]. After this process, the value of a[n] must be the largest in this set of data. Again to a[1]~a[n-1] in the same way, the value of a[n-1] must be the largest of a[1]~a[n-1]. Again to A[1]~a[n-2] the same method of processing a round, and so on. A[1], a[2] 、...... A[n] are arranged in ascending order after the n-1 round is processed. The descending arrangement is similar to the ascending arrangement, if a[1] is less than a[2] then the value of the two is exchanged, otherwise it is unchanged, and so on. In general, the maximum (or minimum) number of each round of sorting is moved to the end of the data series, theoretically a total of n (n-1)/2 exchanges.

Advantages: stable;
Cons: 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 9:03 Amauthor:lockeyemail:lockey@123.comdesc :p Ython implements eight sorting algorithms ' Lst1 = [2,5435,67,445,34,4,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]:   #对比相邻两个元素的大小, small element 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 bubble sorting algorithm

There is no room for improvement in the same train of thought for the sequence of full-fledged or non-repeating elements, but if there is a repeating element in a sequence or part of the element is ordered, there will inevitably be an unnecessary repetition of the order, so we can add an iconic variable change to the ordering process. Used to flag whether there is data exchange during a trip sort, if there is no data exchange during a certain trip, then the data has been arranged as required, can immediately end the sorting, avoid unnecessary comparison process, the following example code is improved:


Lst2 = [2,5435,67,445,34,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]:   #使用标记记录本轮排序中是否有数据交换 Change    = j
  LST2[J],LST2[J-1] = lst2[j-1],lst2[j]  print ' sorted{}: {} '. Format (times,lst2)  #将数据交换标记作为循环条件 to decide whether to proceed with sorting  i = Change return Lst2bubble_sort_improve (LST2)

The two scenarios run as follows:

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

2. Select sort (Selection sort)

Algorithm principle:

Each trip selects the smallest (or largest) element from the data element to be sorted, placed in the final order of the ordered sequence, until all the data elements are sorted out.
The direct selection sort of n records files can be ordered by N-1 Direct selection order to get an orderly result:

① Initial state: Unordered area is R[1..N], ordered area is empty.
② 1th Trip Sort
In the unordered area R[1..N], select the smallest record of the keyword R[k], and the 1th record of the unordered zone r[1] exchange, so that r[1..1] and R[2..N] respectively to increase the number of records added a new ordered area and the number of records reduced by 1 new unordered area.
......
③ Order of the first I trip
At the beginning of the sequence I, the current ordered and unordered areas are r[1..i-1] and R (1≤i≤n-1) respectively. The sequencing selects the smallest record of the keyword from the current unordered area R[k], swapping it with the 1th record R in the unordered zone, so that r[1..i] and R change to a new unordered area with a new ordered area and a number of records with a decrease of 1 respectively.
In this way, the direct selection of the files of N records can be sorted by n-1 direct selection to get an ordered result.

Advantage: The number of times to move data is known (n-1 times);
Disadvantages: More than the number of comparisons, instability.

Python Code implementation:


#-*-Coding:utf-8-*-"Created on August 31, 2017 running environment:win7.x86_64 Eclipse python3@author:lockey" ' lst = [65, 568,9,23,4,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):  #从 i+1 start loop traversal to find the smallest index   if lst[min] > Lst[j]:    min = j  Lst[min],lst[i] = lst[i],lst[min]  #一层遍历结束后将最小值赋给外层索引i所指的位置, 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))

Operation Result:

3. Insert Sort

Algorithm principle:

A set of ascending data is known a[1], a[2] 、...... A[n], a set of unordered data b[1], b[2] 、...... b[m], which need to be merged into an ascending sequence. First compare the value of b[1] and a[1], if B[1] is greater than a[1], then skip, compare b[1] and a[2], if B[1] is still greater than a[2], then continue to skip until b[1] is less than a data in an array a[x], then a[x]~a[n] Move backward one bit, will b[ 1] inserted into the original a[x], this completes the insertion of the b[1]. B[2]~B[M] is inserted in the same way. (if countless group A, b[1] as an array of n=1 a)
Advantages: stable, fast;
Disadvantage: The number of comparisons is not necessarily, the more the number of comparisons, the insertion point after the data movement more, especially when the amount of data is huge, but with the list can solve the problem.

Complexity of the algorithm

If the goal is to arrange the sequence of n elements in ascending order, then the insertion sort is the best and worst case scenario. The best case is that the sequence is already in ascending order, in which case the comparison operation needs to be done (n-1). The worst case scenario is that the sequence is sorted in descending order, then there is a total of n (n-1)/2 times to be performed at this point. The assignment operation to insert a sort is the number of times the comparison operation is added (n-1). On average, the time complexity of inserting the sorting algorithm is O (n^2). Thus, the insertion sort is not suitable for applications with a large amount of data for sorting. However, if the amount of data that needs to be sorted is small, for example, if the magnitude is less than thousand, then inserting the sort is 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,9,23,4,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))
  

Operation Result:

It is known by the sorting process that each time an element is inserted into an ordered sequence, then the order is inserted and the next element is sorted ... Until all elements are inserted, sort ends

4. Hill sort

The Hill sort (Shell sort) is a sort of insertion. Also known as narrowing incremental sorting, is a more efficient and improved version of the direct insertion sorting algorithm. Hill Sort is a non-stable sorting algorithm. The method is due to DL. The shell was named after it was introduced in 1959.

Algorithm principle

The algorithm core is grouped (by step), in-group insert sort

You know a set of unordered data a[1], a[2] 、...... a[n], you need to sort them in ascending order. It is found that when n is not big, the effect of inserting sort is good. First Take one increment D (d<n), will a[1], A[1+d], a[1+2d] ... Listed as the first group, a[2], a[2+d], a[2+2d] ... Listed as second group ..., A[d], a[2d], a[3d] ... Listed as the last group by the second analogy, in each group with the insertion sort, and then take the d ' <d, repeat the above operation until d=1.

Python Code implementation:


#!/usr/bin/env python#coding:utf-8 "File:python-8sort.pydate:9/1/17 9:03 Amauthor:lockeyemail:lockey@123.comdesc :p Ython implements eight sorting algorithms ' LST = [65,568,9,23,4,34,65,8,6,9]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)
  

To run the test results:

Process Analysis:

The first step:

1-5: The sequence is divided into 5 groups (group = int (count/step)), such as a group of columns:

Then the insertion sort within each group, after 5 (5 groups * 1 times) of the sub-group insert sort to get the sequence:

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

Step Two:

6666-7777: The sequence is divided into 2 groups (group = int (group/step)), such as a group of columns:


Then the insertion sort within each group, after 8 (2 groups * 4 times) of the sub-group insert sort to get the sequence:

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

Step Three:

888888888: Insert sort for the complete sequence obtained from the previous sort result:

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

After 9 (1 group *10-1) inserts are sorted:

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

Hill Sorting Aging analysis is difficult, the comparison of key codes and the number of records moved depends on the selection of increment factor sequence, in particular cases can be accurately estimated the number of key code comparisons and records of the number of moves. At present, no one has given a method to select the best increment factor sequence. Increment factor sequence can have a variety of ways, there are odd, but also take prime numbers, but need to note: The increment factor in addition to 1 there is no public factor, and the last increment factor must be 1. The hill sort method is an unstable sort method

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.