자바·파이썬·자바스크립트/파이썬 프로그래밍

퀵정렬에서 쓰는 partiton 함수

바로이순간 2011. 12. 3. 18:23

import random

def makelist(m,n):

    al=[]

    for x in range(n):

        al.append(random.randint(1,m))

    return al

def partition(al, low, high):

    # al[low:high] pivot at  al[high-1]

    v=al[high-1]

    j=low

    for i in range(low,high-1):

        if al[i]<=v:

            if i>j: al[i],al[j]=al[j],al[i]

            j=j+1

    al[j],al[high-1]=v,al[j]

    return j

def quick(al, low, high):

    if low+1>=high: return

    i=partition(al, low, high)

    quick(al, low, i)

    quick(al, i+1, high)

n=int(input('Enter number:'))

al=makelist(10000,n)

print (al)

quick(al,0,len(al))

print(al)