Python Learning note 19 (algorithm)

Source: Internet
Author: User

1. Two points Search

Find an ordered list with two-point lookup only

defBin_search (Data,val):#data is the list to be looked up, Val is the value to look forLow =0 High= Len (data)-1 whileLow <=High:mid= (Low+high)//2ifData[mid] = =Val:returnMid#found, returns the index where Val is located        elifData[mid] <Val:low= Mid + 1Else: High= Mid-1return                        #not found, return none
Two-point search2. Bubble sort
def bubble_sort (data):                   #data is required to sort the list for in     Range (len (data)- 1):        = 0        for in range (len (data)-i-1):             if Data[j] > data[j+1]:                data[j],data[j+1] = data[j+1],data[j]                = 1         If FLAG = = 0            :break
Bubble Sort3. Select sort
def select_sort (data):      for  in range (len (data)-1):        = i        for in range ( i+1, Len (data)) :            if data[j] < data[min]:                = J        = Data[min],data[i]
Select Sort4. Insert Sort
def insert_sort (data):      for  in range (1, Len (data)):        = Data[i]        = i-1 while and         data[ J] > tmp:            data[j+1] = Data[j]            = j-1        + 1] = tmp
Insert Sort5. Quick Sort
defQuick_sort (data,left,right):ifLeft <Right:mid=partition (Data,left,right) quick_sort (Data,left,mid-1) Quick_sort (Data,mid+ 1, right)defpartition (Data,left,right): TMP=Data[left] whileLeft <Right : whileLeft < Right andData[right] >=Tmp:right-= 1Data[left]=Data[right] whileLeft < Right andData[left] <=Tmp:left+ = 1Data[right]=Data[left] Data[left]=tmpreturnLeft
Quick Sort6. Heap Sequencing
defSift (Data,low,high): I=Low J= 2 * I +1tmp=Data[i] whileJ <=High :ifJ + 1 <= High andDATA[J] < data[j+1]: J+ = 1ifDATA[J] >Tmp:data[i]=Data[j] I=J J= 2 * i + 1Else:             BreakData[i]=tmpdefheap_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] Sift (data,0,i-1)
Heap Sort7. Merge sort
defmerge (Data,low,mid,high): I=Low J= Mid+1tmp= []     whileI <= mid andJ <=High :ifData[i] <Data[j]: Tmp.append (data[i]) I+ = 1Else: Tmp.append (Data[j]) J+ = 1 whileI <=mid:tmp.append (data[i]) I+ = 1 whileJ <=high:tmp.append (Data[j]) J+ = 1Data[low:high+1] =tmpdefmergesort (data,low,high):ifLow <High:mid= (Low+high)//2mergesort (Data,low,mid) mergesort (Data,mid+1, High) merge (Data,low,mid,high)
Merge Sort

Python Learning note 19 (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.