Python common simple algorithms, binary lookup, bubble sort, array flipping, etc.

Source: Internet
Author: User

1, binary search: Mainly used for querying a large number of elements of the list, the use of binary search, take the median value, size comparison, can improve efficiency

1 #binary lookup, used to query a value in a larger list of data, taking into account that the element is more, simple traversal will cause memory pressure is too large, consider the use of binary search2 #the key to binary lookup is to query the middle value, compare the value you want to find with the median, and then determine the direction of the lookup3 defBinary_search (data_source,find_n):4     #take the median number5Mid=int (len (data_source)/2)6 7     ifLen (data_source) >=1:8         ifData_source[mid]>find_n:#The median is greater than the number to find, the number to find is in the left half, and the binary algorithm is called to find9 Binary_search (data_source[:mid],find_n)Ten         elifData_source[mid]<find_n:#The median is less than the number to find, the number to find is in the right half One Binary_search (data_source[mid:],find_n) A         Else:#The median is equal to the number to find -             Print("found:", Data_source[mid]) -  the     Else: -         Print("not found") -  -  + if __name__=="__main__": -Data=list (Range (1,100000)) +Binary_search (data,88888)

2. Generate a 2-D array of 5*5 and rotate it clockwise by 90 degrees

1 #algorithm Basics: Generate a 4*4 2-d array and rotate it 90 degrees clockwise2 3Array=[[col forAo.inchRange (5)] forRowinchRange (5)]#initialize an array of 4x44 #output array to view the format of the array before rotation5  forRowincharray:6     Print(Row)7 "' Analysis8 before the array is rotated9 [0, 1, 2, 3]Ten [0, 1, 2, 3] One [0, 1, 2, 3] A [0, 1, 2, 3] - " " - " " the after the array is rotated, it rotates 90 degrees clockwise, and by contrast, the first row element and the element position of the first column are exchanged each time . - that is to form part of the rotation, and so on, to achieve the final rotation - [0, 0, 0, 0] - [1, 1, 1, 1] + [2, 2, 2, 2] - [3, 3, 3, 3] + " " AName="split line before and after rotation" at Print(Name.center (60,"*")) -  forI,rowinchEnumerate (Array):#go through the line first. -     #print (I,row) -      forIndexinchRange (I,len (row)):#Gets the subscript for each row of elements, and each time the loop shrinks the extent of the adjustment -Temp=array[i][index]#Get -array[i][index]=Array[index][i] inarray[index][i]=Temp -      forRinchArrayPrint(R) to     Print("------One loop---------")

3, bubble sort, the tuples in a group of lists are arranged from small to large, and each inner loop moves only one element

1 #bubble sort, which arranges tuples in a set of lists in order from small to large, with each inner loop moving one element at a time2 #bubble Sort is relatively time-consuming, and in this case, the algorithm is added to run-time calculations3 Import Time4time1=time.time ()5data = [10,4,33,21,54,3,8,11,5,22,2,1,17,13,6]6 Print("before bubbling sort:", data)7 8  forIinchRange (len (data)):#outer loop, used to traverse all the elements9      forJinchRange (len (data)-1):#The inner loop, which is used to compare the value to the element after it, with each loop changing only the position of one valueTen         ifData[j]>data[j+1]: Onetmp=Data[j] AData[j]=data[j+1] -data[j+1]=tmp -         Print(data) the Print("after bubbling sort:", data) -Time2=time.time (); - Print('the time spent is:', time2-time1)

Python common simple algorithms, binary lookup, bubble sort, array flipping, etc.

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.