Python Basic algorithm exercises

Source: Internet
Author: User

Bubble sort
#bubblingdefBubble (List): forJinchRange (len (List) -1,0,-1):         forIinchRange (0, j):ifList[i] > List[i+1]: list[i], List[i+1] = list[i+1], List[i]#print (' Iteration #{}.{}: {} '. Format (ABS (J-8), i,list))        #print (' Iteration #{}: {} '. Format (ABS (J-9), List))    returnListlis= [12,56,44,33,8,6,4,2,3]Print('before bubble sort: {}'. Format (LIS))Print('After bubble sort: {}'. Format (Bubble (LIS)))==>before bubble sort: [12, 56, 44, 33, 8, 6, 4, 2, 3]after Bubble Sort: [2, 3, 4, 6, 8, 12, 33, 44, 56]#Bubble Algorithm principle:#starting with sequence 0 elements, each of the two elements is compared, and if two sequential errors are exchanged#This makes use of the double loop, where each iteration of the list will select the maximum (or minimum, depending on how you take) the element to the right
# If I don't describe it in detail, you can unpack the two print statement comments in the function to see how each step of the bubbling algorithm
Select sort
#Selection AlgorithmdefSelection_sort (L): N=Len (L) Exchanges_count=0 forIinchRange (N-1): Min_index=I forJinchRange (i+1, N):ifL[min_index] >L[j]: Min_index=JifMin_index! =I:l[min_index], L[i]=L[i], L[min_index] Exchanges_count+ = 1#print (' Iteration #{}: {} '. Format (i, L))    #Print (' Total {} swappings '. Format (exchanges_count))    returnltestlist= [17, 23, 20, 14, 12, 25, 1, 20, 81, 14, 11, 12]Print('before selection sort: {}'. Format (testlist))Print('After selection sort: {}'. Format (Selection_sort (testlist)))#selection Algorithm principle:#first find the smallest (large) element in the unordered sequence, place it at the beginning of the sort sequence, and then continue looking for the smallest (large) element from the remaining unsorted elements, and place it at the end of the sorted sequence. And so on until all elements are sorted. #Similarly, you can unpack the print comment to see its detailed procedure
Insert Sort
#Insert SortdefInsertion_sort (LST):ifLen (LST) = = 1:        returnLST forIinchRange (1, Len (LST)): Temp=Lst[i] J= I-1 whileJ >= 0 andTemp <Lst[j]: lst[j+ 1] =Lst[j] J-= 1lst[j+ 1] =TempPrint(LST)returnlsttestlist= [17, 23, 20, 14, 12, 25, 1, 20, 81, 14, 11, 12]r=Insertion_sort (testlist)Print(R)#Insert Sort Principle#1. Starting with the first element, the element can be thought to have been sorted#2. Take the next element and scan from the back forward in the sequence of ordered elements#3. If the element (sorted) is greater than the new element, move the element to the next position#4. Repeat step 3 until the sorted element is found to be less than or equal to the position of the new element#5. After inserting a new element into the location#6. Repeat steps 2~5

Python Basic algorithm exercises

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.