Python: Examples of fast sorting and insertion sorting algorithms and custom sorting. python Algorithm

Source: Internet
Author: User

Python: Examples of fast sorting and insertion sorting algorithms and custom sorting. python Algorithm

I. Quick sorting

Quicksort is an improvement in Bubble sorting. Proposed by C. A. R. Hoare in 1962. Its basic idea is: Split the data to be sorted into two independent parts by one sort, and all the data in one part is smaller than all the data in the other part, then, sort the two data parts by using this method. The entire sorting process can be recursive to convert the entire data into an ordered sequence.

Quick sorting and Recursion

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)


Ii. Insert sorting

The description of Insertion Sort is a simple and intuitive sorting algorithm. Its working principle is to build an ordered sequence. For unordered data, scan the sorted sequence from the back to the front, locate the corresponding position, and insert it. Insert sorting usually uses in-place sorting (that is, sorting of the extra space of O (1). Therefore, during the scanning from the back to the forward, the sorted elements need to be moved backward repeatedly to provide the insert space for the new 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


Iii. Custom sorting
You can use the sort () or sorted () key.

Example:

Def sort_key (obj): sorted_list = [4, 2, 5, 9, 7, 8, 1, 3, 6, 0] return sorted_list.index (obj) if _ name _ = '_ main _': print sorted (range (10), key = sort_key) # The output result is as follows: [4, 2, 5, 9, 7, 8, 1, 3, 6, 0]

 
# Use the index position of the keyword in the list for custom sorting

Articles you may be interested in:
  • Reverse, sort, and sorted
  • Python implementation of eight sorting algorithms
  • Example of how to sort tuples and lists by conditions in Python
  • How to sort large files in Python
  • Python simple implementation of Bubble Sorting
  • Python insertion Sorting Algorithm instance analysis
  • Python Sorting Algorithm instance selection Summary
  • Example of the hill sorting algorithm implemented by python
  • A simple tutorial on sorting using sort () in Python
  • Python-based bucket Sorting Algorithm instance analysis

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.