The data structure and algorithm __java in Python

Source: Internet
Author: User

Language: python3.4

First, sorting algorithm
1. Bubble sort

def bubblesort (l):
    print (L) for
    I in range (Len (l) -1,0,-1):
        flag = 1 for
        j in Range (i):
            if l[j] > l[ J+1]:
                l[j],l[j+1] = l[j+1],l[j]
                #上面的表达式相当于c中的
                #temp =l[j+1]
                #l [j+1]=l[j]
                #l [j]=temp
                flag = 0
        If flag:
            break
    print (L)

Overall Rating:
1). Average Time complexity: O (n²)
2). Array Order Sensitivity

2. Insert Sort

def insertsort (l):
    print (L) for
    I in range (1,len (l)):
        Temp=l[i] for
        J in Range (i-1,-1,-1):
            If temp >= l[j]:
                break
            else:
                l[j+1] = l[j]
                l[j] = temp
    print (l)

Overall Rating:
1). Average Time complexity O (n²)
2). Array Order Sensitivity
3). Data is not exchanged, better than bubbling

3. Select sort

def selectionsort (l):
    print (L) for
    I in range (Len (l)): for
        J in Range (I+1,len (l)):
            if l[i] > L[j]:
  L[I],L[J] = L[j],l[i]
    print (l)

Overall Rating:
1). Average Time complexity O (n²)
2. The order of the array is not sensitive
3). Less exchange times due to bubbling

4. Quick Sort

Import sys
Sys.setrecursionlimit (10000)
#手动设置递归次数10000次, default to 900
def subsort (l,left,right):
    start  = Left End
    = right
    flag = L[right] While left
    < right:
        if L[left] > flag:
            l[left],l[right] = L[right],l[left]
            right-=1
        else:
            left+=1
            Continue
        if left < right:
            if l[right] <= flag:
                l[left],l[right] = l[right],l[left]
                left+=1
            else:
                right-=1
    if left-start>1 :
        subsort (l,start,left-1)
    if end-left>1:
        subsort (L,left+1,end)

Overall Rating:
1). Average Time complexity O (NLOGN)
2). Sensitive to the order of samples

5 Heap Sort

def fixuptodown (l,t,n): #从上往下堆化, starting from T while
    2*t < n:
        i = 2*t #表示t结点的左子节点
        If I < n-1 and L[i] < l[i+1]:
            i + 1
        if l[i] > l[t]:
            l[i],l[t] = l[t],l[i]
            t = i
        else:
            break

def heapsort (l):
    n = l En (L)-1 
    for I in Range (n//2,0,-1):
        Fixuptodown (L,i,len (L))
    #此时root结点为最大值, swap the maximum with the last element while
    N >1:
        l[1],l[n] = l[n],l[1]
        fixuptodown (l,1,n)
        n-=1 return
    l[1:]

Second, the search algorithm
1. Linear Search

From one end of the sequence, check that each element is the element to look for, until it is found. Implementation is relatively simple, do not list the

2. Two-point search
Note: The premise of binary search is that sequence must be ordered

def binarysearch (l,element): Left
    = 0 Right
    = Len (l)-1 while left

    <= right:
        mid = (left + right)//2 #//= divisible by
        if L[mid] < element: Left
            = mid + 1
        elif L[mid] > element: Right
            = mid-1
        else:
            R Eturn L[mid]

    print ("Not Found")

Third, data structure
1. Implementation of Stack

Class Stack:
    def __init__ (self,size=10):
        self.stack = []
        self.top =-1
        self.size = size

    def isempty (self):
        if not self.stack: return
            True
        else: return
            False

    def isfull (self):
        if self.top+1 = = Self.size: Return
            True
        else: return
            False

    def top (self):
        if Self.isempty ():
            print (" The Stack is empty ')
        else: return
            Self.stack[self.top]

    def push (self,element):
        if Self.isfull ():
            print ("The Stack is full")
        else:
            self.stack.append (Element)
            Self.top + + 1

    def pop (self):
        if Self.isempty ():
            print (' The Stack is empty ')
        else: return
            self.stack.pop ()
            self.top-= 1< C31/>def display (self):
        print (Self.stack)

2. Implementation of queues:

Class Queue:
    def __init__ (self):
        self.queue = []
        self.front = 0
        self.rear =-1
        self.size = 0

    def IsEmpty (self):
        if not self.size: return
            True
        else: return
            False

    def getsize (self):
        return Self.size

    def push (self,element):
        self.queue.append (Element)
        self.rear + = 1
        self.size + = 1

    def pop (self):
        if Self.isempty ():
            print ("The Queue is empty")
        else:
            self.rear = 1
            Self.size-= 1 return
            self.queue.pop (self.front)

    def topdata (self):
        if Self.isempty ():
            print (" The Queue is empty ')
        else: return
            Self.queue[self.front]

    def display (self):
        print (Self.queue)

#python每个函数都有一个默认的返回值None

3. The realization of two fork tree

Class TreeNode (object):
    def __init__ (self,data=none,left=none,right=none):
        self.data = data
        Self.left = Left
        Self.right = right

class BinaryTree (object):
    def __init__ (self,data):
        self.root = Data

    def IsEmpty (self):
        if not self.root: return
           True 
        else: return
            False

    def preorder (Self,treenode):
        if not TreeNode:
            return
        print (treenode.data)
        Self.preorder (treenode.left)
        Self.preorder (treenode.right)

    def inorder (self,treenode):
        If not TreeNode: return
        Self.inorder (treenode.left)
        print (treenode.data)
        Self.inorder (treenode.right)

    def postorder (self , TreeNode):
        If not TreeNode:
            return
        self.postorder (treenode.left)
        Self.postorder ( Treenode.right)
        print (Treenode.data)

Data structure and algorithms are listed so much, followed by continuing to add

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.