Python 学习笔记(一)

/*

第一次写py代码,写个小算法以熟悉py的基本语法。

QuickSort.py

*/

def Test1():
    print ("begin !")
    list = [10,9,8,7,6,5,4,3,2,1]
    QuickSort(list,0,9)
    for l in list:        
        print (str(l))
    return

def QuickSort(sortList,beginIndex,endIndex):
    PrintList(sortList,beginIndex,endIndex)
    if( endIndex-beginIndex + 1 == 2): #刚好要排序的只有两个
        ExchangeElementInList(sortList,beginIndex,endIndex)
    elif(endIndex-beginIndex + 1 <= 1):
        DoNothing()
    else:
        result = HandleList(sortList,beginIndex,endIndex)
        QuickSort(sortList,result[0],result[1])
        QuickSort(sortList,result[2],result[3])
    return


def DoNothing():
    
    return
def ExchangeElementInList(list,elIndex1,elIndex2):
    if list[elIndex2] >= list[elIndex1] :
        return
    else :
        temp = list[elIndex1]
        list[elIndex1] = list[elIndex2]
        list[elIndex2] = temp
        return
def HandleList(list,elIndex1,elIndex2):
    newList1 = []
    newList2 = []
    result = [] #0:left begin 1:left end  2:right begin 3:right end

    baseNum = list[elIndex1]
    for index in range(elIndex2-elIndex1):
        id = index+elIndex1+1
        if list[id] >= baseNum :
            newList2.append(list[id])
        else :
            newList1.append(list[id])

    index = elIndex1
    for id in range(len(newList1)):
        list[index] = newList1[id]
        index = index+1

    result.append(elIndex1)
    result.append(index-1)

    list[index] = baseNum
    index=index+1

    result.append(index)
    result.append(elIndex2)

    for id in range(len(newList2)):
        list[index] = newList2[id]
        index = index+1

    return result

def PrintList(list,startIndex,endIndex):
    return
    index = startIndex
    print("------- print array from "+str(startIndex)+" to "+str(endIndex)+" -------")
    while index<= endIndex :
        print(list[index])
        index = index+1
    print("------- end -------------")


#run
Test1()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值