据结构学习(冒泡、选择、插入、快速排....

[AppleScript] 纯文本查看 复制代码

?

001

002

003

004

005

006

007

008

009

010

011

012

013

014

015

016

017

018

019

020

021

022

023

024

025

026

027

028

029

030

031

032

033

034

035

036

037

038

039

040

041

042

043

044

045

046

047

048

049

050

051

052

053

054

055

056

057

058

059

060

061

062

063

064

065

066

067

068

069

070

071

072

073

074

075

076

077

078

079

080

081

082

083

084

085

086

087

088

089

090

091

092

093

094

095

096

097

098

099

100

101

102

103

104

105

106

107

#coding=utf-8

  

'''

数据结构排序

'''

#函数冒泡排序

#   参数alist:被排序的列表

def bubbleSort(alist):

    for num in range(len(alist)-1,0,-1):

        for i in range(num):

            if alist[i] < alist[i+1]:

                #进行当前位置和下一个位置的交换

                alist[i] = alist[i]^alist[i+1]

                alist[i+1] = alist[i]^alist[i+1]

                alist[i] = alist[i]^alist[i+1]

    return alist

  

#函数选择排序

#   参数alist:被排序的列表

def selectSort(alist):

    for num in range(len(alist)-1,0,-1):

        positionMax = 0

        for index in range(1,num+1):

            if alist[positionMax] < alist[index]:

                positionMax = index

              

        # temp = alist[num]

        # alist[num] = alist[positionMax]

        # alist[positionMax] =temp

  

        alist[num] = alist[num]^alist[positionMax]

        alist[positionMax] = alist[num]^alist[positionMax]

        alist[num] = alist[num]^alist[positionMax]

        print alist[num]

    return alist

  

#函数插入排序

#   参数alist:被排序的列表

def insertSort(alist):

    for index in range(1,len(alist)):

        temp = alist[index]

        protion = index

  

        while alist[protion-1] > temp and protion > 0:

            alist[protion] = alist[protion-1]

            protion = protion-1

  

        alist[protion] = temp

    return alist

  

number=[54,26,93,17,77,31,44,55,21]

  

#快速排序

#    参数alist:被排序的列表

#    参数low:左侧起始位置

#    参数hegh:右侧终止位置

def quickSort(alist,low,hegh):

    if low < hegh:

        pos = findpos(alist,low,hegh)

  

        quickSort(alist, low, pos-1)

        quickSort(alist, pos+1, hegh)

  

    return alist

  

#快排中查找中间节点

#    参数alist:被产讯的列表

#    参数low:左侧起始位置

#    参数hegh:右侧终止位置

def findpos(alist,left,right):

    temp = alist[left]

  

    while left < right:    

        while left < right and alist[right] >= temp:

            right -= 1

        alist[left] = alist[right]

          

        while left < right and alist[left] <= temp:

            left += 1

        alist[right] = alist[left]

  

  

  

    alist[right] = temp

  

  

  

  

    return right

  

if __name__ == "__main__":

  

    # #测试冒泡排序:

    # num = bubbleSort(number)

    # print num

  

    # #调用选择排序

    # num = selectSort(number)

    # print num

  

    #调入插入排序

    num = insertSort(number)

    print num

  

    # #快速排序

    # num = quickSort(number, 0, len(number)-1)

    # print num

更多学习资料可关注:itheimaGZ获取

发布了731 篇原创文章 · 获赞 3 · 访问量 11万+

猜你喜欢

转载自blog.csdn.net/u010395024/article/details/104746204