I have made a sorting algorithm in Python 3. This sorting algorithm is a modified optimized median of 3 quicksort, with network sorting for lists of size up to 16 and for finding the median. I have tried using the O(N) approach to finding the median, but sorting normally was faster.
Can I get some advice on how to make this faster?
Also, can anyone link fast sorting algorithms? I want to compare them to mine and see if there are faster, and if they are, how I can improve mine. The first like 1000 lines is the network sorting (I made a program to automatically generate code for the network sorting, I am not a madman), and the final 20ish is testing how fast it is. On my laptop, it sorts 100,000 random numbers in about 0.192 seconds and 1,000,000 random numbers in about 2.754 seconds. (Note that python's time is not 100% accurate. I used multiple tests and found the average.)
I have compared my code to mergesort, heapsort, other quicksorts, timsort, and radix sort, but none of them seem to be close to beating my code. However, that is likely because they were from how-to sites, and were not optimized. Sadly, most people who care about having a quick sorting algorithm seem to write it in Java, C or C++ which I can't really compare. So, I would appreciate any other optimized sorting algorithms, or even suggestions to my original code.
#importing import math import random import time #this is the sorting network (scroll a bunch, this is like 1000 lines) def sorting_network(lst): l = len(lst) if l < 9: if l < 5: if l < 3: if not l or l == 1: return lst else: a = lst[0] b = lst[1] if a > b: a, b = b, a return [a, b] else: if l == 3: a = lst[0] b = lst[1] c = lst[2] if a > b: a, b = b, a if b > c: b, c = c, b if a > b: a, b = b, a return [a, b, c] else: a = lst[0] b = lst[1] c = lst[2] d = lst[3] if a > b: a, b = b, a if c > d: c, d = d, c if a > c: a, c = c, a if b > d: b, d = d, b if b > c: b, c = c, b return [a, b, c, d] else: if l < 7: if l == 5: a = lst[0] b = lst[1] c = lst[2] d = lst[3] e = lst[4] if a > d: a, d = d, a if b > e: b, e = e, b if a > b: a, b = b, a if c > e: c, e = e, c if b > c: b, c = c, b if d > e: d, e = e, d if c > d: c, d = d, c return [a, b, c, d, e] else: a = lst[0] b = lst[1] c = lst[2] d = lst[3] e = lst[4] f = lst[5] if a > f: a, f = f, a if b > d: b, d = d, b if c > e: c, e = e, c if b > c: b, c = c, b if d > e: d, e = e, d if a > d: a, d = d, a if c > f: c, f = f, c if a > b: a, b = b, a if c > d: c, d = d, c if e > f: e, f = f, e if b > c: b, c = c, b if d > e: d, e = e, d return [a, b, c, d, e, f] else: if l == 7: a = lst[0] b = lst[1] c = lst[2] d = lst[3] e = lst[4] f = lst[5] g = lst[6] if a > b: a, b = a, b if c > d: c, d = d, c if e > f: e, f = f, e if a > c: a, c = c, a if b > e: b, e = e, b if d > g: d, g = g, d if a > b: a, b = b, a if c > f: c, f = f, c if d > e: d, e = e, d if b > c: b, c = c, b if e > g: e, g = g, e if c > d: c, d = d, c if e > f: e, f = f, e if b > c: b, c = c, b if d > e: d, e = e, d if f > g: f, g = g, f return [a, b, c, d, e, f, g] else: a = lst[0] b = lst[1] c = lst[2] d = lst[3] e = lst[4] f = lst[5] g = lst[6] h = lst[7] if a > c: a, c = c, a if b > d: b, d = d, b if e > g: e, g = g, e if f > h: f, h = h, f if a > e: a, e = e, a if b > f: b, f = f, b if c > g: c, g = g, c if d > h: d, h = h, d if a > b: a, b = b, a if c > d: c, d = d, c if e > f: e, f = f, e if g > h: g, h = h, g if c > e: c, e = e, c if d > f: d, f = f, d if b > e: b, e = e, b if d > g: d, g = g, d if b > c: b, c = c, b if d > e: d, e = e, d if f > g: f, g = g, f return [a, b, c, d, e, f, g, h] else: if l < 13: if l < 11: if l == 9: a = lst[0] b = lst[1] c = lst[2] d = lst[3] e = lst[4] f = lst[5] g = lst[6] h = lst[7] i = lst[8] if a > d: a, d = d, a if b > h: b, h = h, b if c > f: c, f = f, c if e > i: e, i = i, e if a > h: a, h = h, a if c > e: c, e = e, c if d > i: d, i = i, d if f > g: f, g = g, f if a > c: a, c = c, a if b > d: b, d = d, b if e > f: e, f = f, e if h > i: h, i = i, h if b > e: b, e = e, b if d > g: d, g = g, d if f > h: f, h = h, f if a > b: a, b = b, a if c > e: c, e = e, c if d > f: d, f = f, d if g > i: g, i = i, g if c > d: c, d = d, c if e > f: e, f = f, e if g > h: g, h = h, g if b > c: b, c = c, b if d > e: d, e = e, d if f > g: f, g = g, f return [a, b, c, d, e, f, g, h, i] else: a = lst[0] b = lst[1] c = lst[2] d = lst[3] e = lst[4] f = lst[5] g = lst[6] h = lst[7] i = lst[8] j = lst[9] if a > i: a, i = i, a if b > j: b, j = j, b if c > h: c, h = h, c if d > f: d, f = f, d if e > g: e, g = g, e if a > c: a, c = c, a if b > e: b, e = e, b if f > i: f, i = i, f if h > j: h, j = j, h if a > d: a, d = d, a if c > e: c, e = e, c if f > h: f, h = h, f if g > j: g, j = j, g if a > b: a, b = b, a if d > g: d, g = g, d if i > j: i, j = j, i if b > f: b, f = f, b if c > d: c, d = d, c if e > i: e, i = i, e if g > h: g, h = h, g if b > c: b, c = c, b if d > f: d, f = f, d if e > g: e, g = g, e if h > i: h, i = i, h if c > d: c, d = d, c if e > f: e, f = f, e if g > h: g, h = h, g if d > e: d, e = e, d if f > g: f, g = g, f return [a, b, c, d, e, f, g, h, i, j] else: if l == 11: a = lst[0] b = lst[1] c = lst[2] d = lst[3] e = lst[4] f = lst[5] g = lst[6] h = lst[7] i = lst[8] j = lst[9] k = lst[10] if b > g: b, g = g, b if c > e: c, e = e, c if d > h: d, h = h, d if f > i: f, i = i, f if a > b: a, b = b, a if d > f: d, f = f, d if e > k: e, k = k, e if g > j: g, j = j, g if h > i: h, i = i, h if b > d: b, d = d, b if c > f: c, f = f, c if e > h: e, h = h, e if i > k: i, k = k, i if a > e: a, e = e, a if b > c: b, c = c, b if d > h: d, h = h, d if f > j: f, j = j, f if g > i: g, i = i, g if a > b: a, b = b, a if c > g: c, g = g, c if e > f: e, f = f, e if h > i: h, i = i, h if j > k: j, k = k, j if c > e: c, e = e, c if d > g: d, g = g, d if f > h: f, h = h, f if i > j: i, j = j, i if b > c: b, c = c, b if d > e: d, e = e, d if f > g: f, g = g, f if h > i: h, i = i, h if c > d: c, d = d, c if e > f: e, f = f, e if g > h: g, h = h, g return [a, b, c, d, e, f, g, h, i, j, k] else: a = lst[0] b = lst[1] c = lst[2] d = lst[3] e = lst[4] f = lst[5] g = lst[6] h = lst[7] i = lst[8] j = lst[9] k = lst[10] l = lst[11] if b > h: b, h = h, b if c > g: c, g = g, c if d > l: d, l = l, d if e > k: e, k = k, e if f > j: f, j = j, f if a > b: a, b = b, a if c > f: c, f = f, c if d > e: d, e = e, d if g > j: g, j = j, g if h > i: h, i = i, h if k > l: k, l = l, k if a > c: a, c = c, a if b > g: b, g = g, b if f > k: f, k = k, f if j > l: j, l = l, j if a > d: a, d = d, a if b > c: b, c = c, b if e > g: e, g = g, e if f > h: f, h = h, f if i > l: i, l = l, i if j > k: j, k = k, j if b > e: b, e = e, b if d > f: d, f = f, d if g > i: g, i = i, g if h > k: h, k = k, h if b > d: b, d = d, b if c > f: c, f = f, c if g > j: g, j = j, g if i > k: i, k = k, i if c > d: c, d = d, c if e > f: e, f = f, e if g > h: g, h = h, g if i > j: i, j = j, i if e > g: e, g = g, e if f > h: f, h = h, f if d > e: d, e = e, d if f > g: f, g = g, f if h > i: h, i = i, h return [a, b, c, d, e, f, g, h, i, k, l] else: if l < 15: if l == 13: a = lst[0] b = lst[1] c = lst[2] d = lst[3] e = lst[4] f = lst[5] g = lst[6] h = lst[7] i = lst[8] j = lst[9] k = lst[10] l = lst[11] m = lst[12] if a > m: a, m = m, a if b > k: b, k = k, b if c > j: c, j = j, c if d > h: d, h = h, d if f > l: f, l = l, f if g > i: g, i = i, g if b > g: b, g = g, b if c > d: c, d = d, c if e > l: e, l = l, e if h > j: h, j = j, h if i > k: i, k = k, i if a > e: a, e = e, a if b > c: b, c = c, b if d > g: d, g = g, d if h > i: h, i = i, h if j > k: j, k = k, j if l > m: l, m = m, l if e > g: e, g = g, e if f > j: f, j = j, f if i > l: i, l = l, i if k > m: k, m = m, k if a > f: a, f = f, a if d > i: d, i = i, d if e > h: e, h = h, e if g > l: g, l = l, g if j > k: j, k = k, j if a > b: a, b = b, a if c > f: c, f = f, c if g > j: g, j = j, g if h > i: h, i = i, h if k > l: k, l = l, k if b > d: b, d = d, b if c > e: c, e = e, c if f > g: f, g = g, f if j > k: j, k = k, j if b > c: b, c = c, b if d > e: d, e = e, d if f > h: f, h = h, f if g > i: g, i = i, g if c > d: c, d = d, c if e > f: e, f = f, e if g > h: g, h = h, g if i > j: i, j = j, i if d > e: d, e = e, d if f > g: f, g = g, f return [a, b, c, d, e, f, g, h, i, j, k, l, m] else: a = lst[0] b = lst[1] c = lst[2] d = lst[3] e = lst[4] f = lst[5] g = lst[6] h = lst[7] i = lst[8] j = lst[9] k = lst[10] l = lst[11] m = lst[12] n = lst[13] if a > g: a, g = g, a if b > l: b, l = l, b if c > m: c, m = m, c if d > k: d, k = k, d if e > f: e, f = f, e if h > n: h, n = n, h if i > j: i, j = j, i if b > c: b, c = c, b if d > h: d, h = h, d if e > i: e, i = i, e if f > j: f, j = j, f if g > k: g, k = k, g if l > m: l, m = m, l if a > e: a, e = e, a if b > d: b, d = d, b if f > g: f, g = g, f if h > i: h, i = i, h if j > n: j, n = n, j if k > m: k, m = m, k if a > b: a, b = b, a if c > j: c, j = j, c if d > h: d, h = h, d if e > l: e, l = l, e if g > k: g, k = k, g if m > n: m, n = n, m if c > f: c, f = f, c if e > h: e, h = h, e if g > j: g, j = j, g if i > l: i, l = l, i if b > c: b, c = c, b if d > e: d, e = e, d if g > h: g, h = h, g if j > k: j, k = k, j if l > m: l, m = m, l if b > d: b, d = d, b if c > e: c, e = e, c if f > g: f, g = g, f if h > i: h, i = i, h if j > l: j, l = l, j if k > m: k, m = m, k if c > d: c, d = d, c if e > h: e, h = h, e if g > j: g, j = j, g if k > l: k, l = l, k if e > f: e, f = f, e if g > h: g, h = h, g if i > j: i, j = j, i if d > e: d, e = e, d if f > g: f, g = g, f if h > i: h, i = i, h if j > k: j, k = k, j return [a, b, c, d, e, f, g, h, i, j, k, l, m, n] else: if l == 15: a = lst[0] b = lst[1] c = lst[2] d = lst[3] e = lst[4] f = lst[5] g = lst[6] h = lst[7] i = lst[8] j = lst[9] k = lst[10] l = lst[11] m = lst[12] n = lst[13] o = lst[14] if b > c: b, c = c, b if d > k: d, k = k, d if e > o: e, o = o, e if f > i: f, i = i, f if g > n: g, n = n, g if h > m: h, m = m, h if j > l: j, l = l, j if a > o: a, o = o, a if b > f: b, f = f, b if c > i: c, i = i, c if d > h: d, h = h, d if g > j: g, j = j, g if k > m: k, m = m, k if l > n: l, n = n, l if a > h: a, h = h, a if b > g: b, g = g, b if c > j: c, j = j, c if e > k: e, k = k, e if f > l: f, l = l, f if i > n: i, n = n, i if m > o: m, o = o, m if a > g: a, g = g, a if c > e: c, e = e, c if d > f: d, f = f, d if h > l: h, l = l, h if i > k: i, k = k, i if j > m: j, m = m, j if n > o: n, o = o, n if a > d: a, d = d, a if b > c: b, c = c, b if e > h: e, h = h, e if f > j: f, j = j, f if g > i: g, i = i, g if k > l: k, l = l, k if m > n: m, n = n, m if a > b: a, b = b, a if c > d: c, d = d, c if e > g: e, g = g, e if h > j: h, j = j, h if k > m: k, m = m, k if l > n: l, n = n, l if b > c: b, c = c, b if d > f: d, f = f, d if i > k: i, k = k, i if l > m: l, m = m, l if d > e: d, e = e, d if f > g: f, g = g, f if h > i: h, i = i, h if j > k: j, k = k, j if c > d: c, d = d, c if e > f: e, f = f, e if g > h: g, h = h, g if i > j: i, j = j, i if k > l: k, l = l, k if f > g: f, g = g, f if h > i: h, i = i, h return [a, b, c, d, e, f, g, h, i, j, k, l, m, n, o] else: a = lst[0] b = lst[1] c = lst[2] d = lst[3] e = lst[4] f = lst[5] g = lst[6] h = lst[7] i = lst[8] j = lst[9] k = lst[10] l = lst[11] m = lst[12] n = lst[13] o = lst[14] p = lst[15] if a > n: a, n = n, a if b > m: b, m = m, b if c > p: c, p = p, c if d > o: d, o = o, d if e > i: e, i = i, e if f > g: f, g = g, f if h > l: h, l = l, h if j > k: j, k = k, j if a > f: a, f = f, a if b > h: b, h = h, b if c > j: c, j = j, c if d > e: d, e = e, d if g > n: g, n = n, g if i > o: i, o = o, i if k > p: k, p = p, k if l > m: l, m = m, l if a > b: a, b = b, a if c > d: c, d = d, c if e > f: e, f = f, e if g > i: g, i = i, g if h > j: h, j = j, h if k > l: k, l = l, k if m > n: m, n = n, m if o > p: o, p = p, o if a > c: a, c = c, a if b > d: b, d = d, b if e > k: e, k = k, e if f > l: f, l = l, f if g > h: g, h = h, g if i > j: i, j = j, i if m > o: m, o = o, m if n > p: n, p = p, n if b > c: b, c = c, b if d > m: d, m = m, d if e > g: e, g = g, e if f > h: f, h = h, f if i > k: i, k = k, i if j > l: j, l = l, j if n > o: n, o = o, n if b > e: b, e = e, b if c > g: c, g = g, c if f > i: f, i = i, f if h > k: h, k = k, h if j > n: j, n = n, j if l > o: l, o = o, l if c > e: c, e = e, c if d > g: d, g = g, d if j > m: j, m = m, j if l > n: l, n = n, l if d > f: d, f = f, d if g > i: g, i = i, g if h > j: h, j = j, h if k > m: k, m = m, k if d > e: d, e = e, d if f > g: f, g = g, f if h > i: h, i = i, h if j > k: j, k = k, j if l > m: l, m = m, l if g > h: g, h = h, g if i > j: i, j = j, i return [a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p] #and this is the quicksort algorithm! def swiftsort(lst): l = len(lst) if l < 17: return sorting_network(lst) elif l < 128: a = lst[0] b = lst[l // 2] c = lst[-1] if a <= b <= c or c <= b <= a: split = b elif b <= c <= a or a <= c <= b: split = c else: split = a elif l < 8192: a = [lst[0], lst[l // 8], lst[2 * l // 8], lst[3 * l // 8], lst[4 * l // 8], lst[5 * l // 8], lst[6 * l // 8], lst[7 * l // 8], lst[-1]] split = sorting_network(lst)[4] else: a = [lst[0]] for x in range(1, 32): a.append(lst[x * l // 32]) a.append(lst[-1]) split = swiftsort(a)[16] lst1 = [] lst2 = [] mid = [] for x in lst: if x < split: lst1.append(x) elif x == split: mid.append(x) else: lst2.append(x) return swiftsort(lst1) + mid + swiftsort(lst2) #This is for testing the time of my algorithm. length = 100000 runs = 60 test_list = [random.randint(1, length) for _ in range(length)] t = 0 for _ in range(runs): start = time.time() sort = swiftsort(test_list) end = time.time() t += end - start print(t / runs)
I have made a sorting algorithm…
I don't think so - you tried to implement quicksort, but left out base cases, for starters. Tryprint(swiftsort(list('abramccarthybra')))
. (…made a sorting algorithm in python
conventional wordings include coded, implemented, crafted an implementation.)\$\endgroup\$I have removed the insertion sort part
funny - I thought the "almost median of 32" uses it.\$\endgroup\$