From bc4ffa182992ada61596719246e34c0efb73c28f Mon Sep 17 00:00:00 2001 From: fromfall Date: Mon, 26 Oct 2015 15:45:07 +0300 Subject: [PATCH 1/2] Create lab5.py --- lab5/Petrov/lab5.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 lab5/Petrov/lab5.py diff --git a/lab5/Petrov/lab5.py b/lab5/Petrov/lab5.py new file mode 100644 index 0000000..72e9635 --- /dev/null +++ b/lab5/Petrov/lab5.py @@ -0,0 +1,23 @@ +from random import * +from sys import stdin +a = list(map(int, stdin.readline().split())) + +def quickSort(a): + if len(a) <= 1: + return a + less = [] + more = [] + p = choice(a) + for i in a: + if i < p: + less.append(i) + if i > p: + more.append(i) + less = quickSort(less) + more = quickSort(more) + return less + [p] * a.count(p) + more + +b = quickSort(a) + +for i in range(len(b)): + print(b[i], end = ' ') From 18299e0b26b4ef4a915911787b17c591157085d4 Mon Sep 17 00:00:00 2001 From: fromfall Date: Sat, 28 Nov 2015 20:53:38 +0300 Subject: [PATCH 2/2] Update lab5.py --- lab5/Petrov/lab5.py | 40 ++++++++++++++++++++++++---------------- 1 file changed, 24 insertions(+), 16 deletions(-) diff --git a/lab5/Petrov/lab5.py b/lab5/Petrov/lab5.py index 72e9635..77e5dcd 100644 --- a/lab5/Petrov/lab5.py +++ b/lab5/Petrov/lab5.py @@ -2,22 +2,30 @@ from sys import stdin a = list(map(int, stdin.readline().split())) -def quickSort(a): - if len(a) <= 1: - return a - less = [] - more = [] - p = choice(a) - for i in a: - if i < p: - less.append(i) - if i > p: - more.append(i) - less = quickSort(less) - more = quickSort(more) - return less + [p] * a.count(p) + more - -b = quickSort(a) +def partition(l, r): + x = a[randint(l, r)] + i = l + j = r + while 1: + while a[i] < x: + i += 1 + while a[j] > x: + j -= 1 + if i < j: + a[i], a[j] = a[j], a[i] + i += 1 + j -= 1 + else: + return j + +def quickSort(l, r): + if l < r: + q = partition(l, r) + quickSort(l, q) + quickSort(q + 1, r) + return a + +b = quickSort(0, len(a) - 1) for i in range(len(b)): print(b[i], end = ' ')