Saturday, November 9, 2013

Quick Sort

# -*- coding:utf-8 -*-
#!/usr/bin/python2
"""
===================================================
Merge Sort
Shijie Xu <xushijie520@gmail.com>
Created on Mon, Nov 9, 2013
===================================================
"""
L = [3, 5, 7, 8, 1, 9, 2, 4, 6, 123, 12, 19, 123123]

def QuickSort(m):
    if len(m) < 2:
        return m
    else:
        target = m.pop(0)
        L1 = []
        L2 = []
        for item in m:
            if item <= target:
                L1.append(item)
            else:
                L2.append(item)
        L1.append(target)
        L = QuickSort(L1) + QuickSort(L2)
        return L

result = QuickSort(L)
print result

No comments:

Post a Comment