快速排序

from unittest import (
        main,
        TestCase,
        )
from random import Random


def quick(lst):
    if len(lst) <= 1:
        return lst
    key = lst[0]
    left = [i for i in lst if i < key]
    right = [i for i in lst[1:] if i >= key]
    return quick(left) + [key] + quick(right)



class QuickTestCase(TestCase):
    def setUp(self):
        self.data = list(
                Random().sample(range(10000), 100)
                for i in range(100)
                )

    def testQuick(self):
        for _data in self.data:
            print _data
            self.assertEqual(
                    quick(_data),
                    sorted(_data),
                    )


if __name__ == '__main__':
    main()
Advertisements
此条目发表在Python分类目录。将固定链接加入收藏夹。

发表评论

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / 更改 )

Twitter picture

You are commenting using your Twitter account. Log Out / 更改 )

Facebook photo

You are commenting using your Facebook account. Log Out / 更改 )

Google+ photo

You are commenting using your Google+ account. Log Out / 更改 )

Connecting to %s