Nine sorting algorithms you need to know the cardinality of the "Python implementation"

Source: Internet
Author: User

Eight, the base sort
  1. Basic idea: the Cardinal sort (radix sort) is "assigned sort" (distribution sort), also known as "bucket method" (bucket sort) or bin sort, as the name implies, it is part of the information through the key value, The elements to be sorted are assigned to some "buckets", in order to achieve the order, the cardinal sort method is a sort of stability, its time complexity is O (Nlog (r) m), where R is the cardinality taken, and M is the number of heaps, at some point, the cardinality of the sorting method is more efficient than other stability ranking method.

  2. Algorithm implementation:

    #Coding:utf-8#!/usr/bin/pythonImportRandomImportMath#randomly generate a value between 0~100defget_andomnumber (num): Lists=[] I=0 whilei<num:lists.append (random.randint (0,100)) I+=1returnlists#The head needs to be imported into mathdefRadix_sort (lists, radix=10): K=Int (Math.ceil (Math.log (max (lists), radix )) bucket= [[] forIinchrange (radix)] forIinchRange (1, k+1):         forJinchLists:bucket[int (J/(radix** (i-1))% (radix**i)]. Append (j)dellists[:] forZinchbucket:lists+=Zdelz[:]returnListsa= Get_andomnumber (10)Print("before sorting:%s"%a) b=Radix_sort (a)Print("after sorting:%s"%b

Nine sorting algorithms you need to know the cardinality of the "Python implementation"

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.