Python sort-bubble sort, select sort, insert sort

Source: Internet
Author: User

" "Bubble Sort It repeatedly visited the sequence to sort, comparing two elements at a time, swapping them out if they were in the wrong order. The work of the sequence of visits is repeated until no more need to be exchanged, that is, the sequence is sorted. The algorithm is named because the larger element will slowly "float" through the switch to the top of the sequence." "Data_set= [12,45,2,48,66,2,1,56,36,90,5,10,503] forIinchRange (len (data_set)):##控制相邻比较的轮数     forJinchRange (len (data_set)-i-1):#control the number of adjacent comparisons per round        #if the value of the preceding element is greater than the value of the subsequent element interchange        ifData_set[j]>data_set[j+1]: Temp=Data_set[j] Data_set[j]=data_set[j+1] Data_set[j+1]=Temp#print (Data_set)Print(Data_set)
" "Select Sort to find the smallest first from all the sequences, and then place it in the first position. Then look at the smallest of the remaining elements and put them in the second position ... And so on, you can do the whole sort of work" "Data_set= [12,45,2,48,66,2,1,56,36,90,5,10,503] forIinchRange (len (data_set)):#Big Circle determines the number of rounds that we chooseMinindex =I forJinchRange (I+1,len (data_set)):#a small circle is the number of times we compare each time we choose        #//If the subsequent element is smaller than the element I chose, swap the position        ifDATA_SET[J] <Data_set[minindex]: Minindex=J Temp=Data_set[i] Data_set[i]=Data_set[minindex] Data_set[minindex]=TempPrint(Data_set)
" "The basic idea of inserting sort insertion sort (insertion sort) is to divide the list into 2 parts, a sorted part on the left, an unsorted part on the right, a loop through the entire list, and insert a record to be sorted by its keyword size into the appropriate position in the previously ordered sub-sequence at a time Until all records are inserted. " "Source= [12,45,2,48,66,2,1,56,36,90,5,10,503] forIinchRange (1, Len (source)): Current_val= Source[i]#Note the value of the first element each time cycle goes toPosition =I whilePosition>0 andSource[position-1]>current_val:#Number of cycles I is greater than 0 and previous one number is greater than the last oneSource[position] = source[position-1]#post a number to the front, sort by the size of the valuePosition-= 1#Put the front number in the back.Source[position] = Current_val#I have found the position of the Current_val element in the list on the left, and put the current_val here .    Print(source)

Python sort-bubble sort, select sort, insert sort

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.