Python Common algorithm (i)

Source: Internet
Author: User

1. Select sort

Keep finding the smallest (find the biggest is also possible)

First get the first one, and then find it smaller than it, remember the subscript. Loop round, find the smallest number of positions and the leftmost number of exchange positions

Then start with the second one .... and a second swap position, the loop finally becomes orderly

 #  coding=utf-8  def   Select_sort (list):  for  i in    Range (len list): Min  = i  for  J in  range (i+1,len (list)):  if  list[min]>list[j]: Min  = J List[min], List[i]  = List[i], List[min]  return   lista  = Select_sort ([1,4,2,8,6,9,10])  print  (a) 

2. Bubble sort

First get the first element, compare it to the second one, put the larger number on the right.

The second and third comparison .... One round down and the last one is the biggest number.

Another round, the penultimate one becomes orderly ... Eventually they become orderly.

 def   Bubble_sort (lists): Count  = Len (lists)  for  i in   range (0,count):  for  J in  range (i+1,count):  if   Lists[i] > Lists[j]: Lists[i], lists[j]  = Lists[j], Lists[i]  return   Listsa  = Bubble_sort ([3,2,1,6,9,5])  print  (a) 
def Bubble_sort (list):      for inch Range (len (list)):          for  in range (len (list)-i-1):            if list[j] > list[j+1]:                list[j], list[j +1] = list[j+1], list[j]    return= Bubble_sort ([3,2,10,6,9,5])  Print(a)

3. Insert Sort

Starting with a value, inserting a value randomly, and then comparing the two to see if you need to swap positions, the two values become orderly.

In this way, the value is gradually inserted and finally becomes an ordered sequence.

 def   Insert_sort (list):  for  i in  range (1,len (list) ):  for  J in   Range (0,i):  if  list[i]<list[j ]: List[i], list[j]  = List[j],list[i]  return   lista  = Insert_sort ([2,1,5,4,6,7,3 ])  print  (a) 
def Insert1_sort (list):      for  in range (1, Len (list)):        = List[i]        = i-1 while         J >= 0:             if list[j] > key:                list[j+1] = List[j]                = key            -= 1< c20/>return= Insert1_sort ([2,1,5,4,6,7,3])Print(A1)

Python Common algorithm (i)

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.