Python quick sort, insert sort algorithm, custom sort instance detailed

Source: Internet
Author: User
This article mainly describes the Python implementation of the fast sorting and insertion sorting algorithm and custom sorting examples, custom sorting used in the Python sort and sorted function, the need for friends can refer to the following

First, quick sort

Quick Sort (Quicksort) is an improvement to the bubbling sort. by C. A. R. Hoare was introduced in 1962. Its basic idea is: by a trip to sort the data to be sorted into two separate parts, one part of all the data is smaller than the other part of all the data, and then the two parts of the data are quickly sorted by this method, the entire sorting process can be recursive, so as to achieve the entire data into an ordered sequence.

Fast sorting, recursive implementation

def quick_sort (num_list): "" "Quick Sort" ""  if num_list = = []:    return num_list  smalllist = []  Biglist = []  middleelement = num_list[0] for  i in Num_list[1:]:    If I <= middleelement:      Smalllist.append (i)    else:      biglist.append (i)  return Quick_sort (smalllist) +[middleelement]+quick_ Sort (biglist)

Second, insert sort

The algorithm description of the insertion sort (insertion sort) is a simple and intuitive sorting algorithm. It works by constructing an ordered sequence, for unsorted data, to scan from backward forward in the sorted sequence, to find the appropriate position and insert it. The insertion sort is implemented on an implementation, usually in the order of In-place (that is, the ordering of extra space using only O (1), so that in the backward-forward scanning process, the ordered elements need to be moved back and forth gradually, providing the insertion space for the newest elements.

Insert Sort

def insert_sort (num_list): "" "Insert Sort" ""  for I in range (len (num_list)-1):    for J in range (I+1, Len ( num_list)):      if NUM_LIST[I]>NUM_LIST[J]:        num_list[i],num_list[j] = num_list[j],num_list[i]  return Num_list

Three, custom sorting
Use the key of sort () or sorted () to implement.

Examples are as follows:

def sort_key (obj):  sorted_list = [4, 2, 5, 9, 7, 8, 1, 3, 6, 0]  return sorted_list.index (obj)  if name = = ' Ma In ':  print sorted (range, Key=sort_key) # The output results are as follows [4, 2, 5, 9, 7, 8, 1, 3, 6, 0]

# Use keywords to make custom sorting in the index position in the list

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.