Python sorting algorithm

Source: Internet
Author: User

First, bubble sort

Two-layer for loop, in which the inner loop compares the current number with the next bit size, such as small, swap position.

1 def bubble_sort (data): 2      for  in range (len (data)-1):3for in          Range (len (data)-i-1):  4             if data[j]<data[j + 1]:5                 data[j], data[j + 1] = data[ J + 1], data[j]
View Code

Second, insert sort

The number of each digit after the sequence of loops, if it is greater than the last of the ordered series, the two-digit exchange position is compared with the last second of the ordered series, and so on.

1 def insect_sort (data): 2      for  in range (1, Len (data)):3         tmp = data[i]4         j = i-15 while and          data[j] > tmp:6             data[j],data[j + 1] = Data[j + 1],data[j]7             J-= 18         data[j + 1] = tmp
View Code

Iii. Choice of sorting

Each for loop finds the smallest number, as a result of that loop

1 def select_sort (data): 2      for  in range (len (data)-1):3         min_num = I4          for  in range (i+1, Len (data)):5             if data[j]<data[i]: 6                 Min_num = J7             data[i], data[min_num] = Data[min_num], data[i]
View Code

Four, Quick row

Select the first number, from the left to start the loop comparison, such as less than, stop the loop, put the small number to the first number position, from the right to start the loop, such as greater than, stop the loop, the large number is placed on the last loop slot, so loop until Leave = right, return to the mid, recursive

1 defQuick_sort (data,left,right):2     ifleft<Right :3MID =partition (data,left,right):4Quick_sort (data,left,mid-1)5Quick_sort (data,mid+1, right)6 7 defpartition (data,left,right):8TMP =Data[left]9      whileLeft <Right :Ten          whileLeft < Right anddata[right]>=tmp: OneRight-= 1 AData[left] =Data[right] -          whileLeft < Right anddata[left]<=tmp: -Left + = 1 theData[right] =data [left] -Data[left] =tmp -     returnLeft
View Code

Five, heap sorting

Starting from the smallest child node of the parent node to start the loop adjustment to build the heap, and then take the number, the heap end and heap top transposition, and then once the replacement of the heap top of the adjustment, so loop.

1 defSift (Data,low,high):2i = Low3j = 2*i + 14TMP =Data[i]5      whileJ <= High:#as long as it's not the last root6         ifJ < High andDATA[J] < Data[j + 1]:#If there is a right child and the right child is greater than the left child7j = j + 1#J becomes subscript of the largest child8         ifTMP < DATA[J]:#whether the largest child is greater than the Father9Data[i] = Data[j]#son of a child upperTeni = j#re-assign subscript Onej = 2*i + 1 A         Else:#These conditions are not satisfied, jumping out of the loop -              Break -Data[i] =tmp the  - defheap_sort (data): -n =len (data) -      forIinchRange (n//2-1, -1,-1): +Sift (data,i,n-1) -      forIinchRange (n-1,-1,-1): +Data[0],data[i] =Data[i],data[0] ASift (data,0,i-1)
View Code

Python sorting algorithm

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.