首页 > 代码库 > python排序

python排序

import mathclass sort:    def selectSort(self, L):        size = len(L)        for i in range(0, size):            max = L[i]            index = i            for j in range(i, size):                if L[j] > max:                    max = L[j]                    index = j            temp = L[i]            L[i] = max             L[index] = temp        print L    def insertSort(self,L):        size = len(L)        for i in range(1,size):            fv = L[i]            j = i            while(j >= 1):                if fv < L[j-1]:                    L[j]=L[j-1]                else:                    break                j = j -1            L[j] = fv        print L    def bubbleSort(self,L):        size = len(L)        for i in range(size-1,-1,-1):            for j in range(0,i-1):                if L[j] > L[j+1]:                    tmp = L[j+1]                    L[j+1] = L[j]                    L[j] = tmp        print L    def merge(self, L1, L2):        L = []        index1 = 0        index2 = 0        size1 = len(L1)        size2 = len(L2)        top = min(size1, size2)        while(index1 != size1 and index2 != size2):            if L1[index1] > L2[index2]:                L.append(L2[index2])                index2 = index2 + 1            else:                L.append(L1[index1])                index1 = index1 + 1        if index1 < size1:            for i in range(index1,size1):                L.append(L1[i])        if index2 < size2:            for i in range(index2,size2):                L.append(L2[i])        return L    def mergeInL(self,L,first,mid,last):        tempa = []        tempb = []        for i in range(first,mid+1):            tempa.append(L[i])        for i in range(mid+1,last+1):            tempb.append(L[i])        tempc = self.merge(tempa,tempb)        index = 0        for i in range(first,last+1):            L[i] = tempc[index]            index += 1    def mergeSort(self,L,first,last):        if first < last:            mid = math.trunc((first+last)/2)            self.mergeSort(L,first,mid)            self.mergeSort(L,mid+1,last)            self.mergeInL(L,first,mid,last)            def quickSort(self,L,left,right):        i = left        j = right        middle = L[left]        while i <= j:            while L[i] < middle and i < right:                i += 1            while L[j] > middle and j > left:                j -= 1            if i <= j:                temp = L[i]                L[i] = L[j]                L[j] = temp                i += 1                j += 1        if left < j:            self.quickSort(L, left , j)        if right > i:            self.quickSort(L, i , right)    def partition(self,L,left,right):        tmp = L[right]        index = -1        for i in range(left,right):            if L[i] < tmp:               index += 1               tp = L[index]                L[index] = L[i]               L[i] = tp        tp = L[index+1]        L[index + 1] = L[right]        L[right] = tp        return index + 1    def quickSort1(self,L,left,right):        if left < right:            index = self.partition(L,left,right)            self.quickSort1(L,left, index - 1)            self.quickSort1(L,index + 1, right)a = sort()L = [3, 2, 5,9, 7]print "the data" print L#print "select sort"#a.selectSort(L)#print "insert sort"#a.insertSort(L)#print "bubble sort"#a.bubbleSort(L)#print "merge Sort"#a.mergeSort(L,0,5)print "quick sort"a.quickSort1(L, 0,4) print L

  

python排序