Sorting Algorithms

Getting your numbers the right way!



Dennis "@the_metalgamer" Fink

30 May 2014

at "Spark-Up: Tie on!" by C3L

Overview

Introduction

Types

In-Place

Stability

Notes on performance

Selectionsort

Implementation

def selectionsort(list_):

    for i in range(len(list_) - 1):
        k = i
        for j in range(i, len(list_)):
            if list_[j] < list_[k]:
                k = j
        list_[i], list_[k] = list_[k], list_[i]

Out-of-place Implementation

def selectionsort_min(list_):

    new = []

    while list_:
        minimum = min(list_)
        list_.remove(minimum)
        new.append(minimum)

    return new

Performance

Bubblesort

Implementation

def bubblesort(list_):

    swapped = True

    while swapped:

        swapped = False

        for i in range(0, len(list_) - 1):
            if list_[i] > list_[i + 1]:
                list_[i], list_[i + 1] = list_[i + 1], list_[i]
                swapped = True

    return list_

Performance

Cocktailsort

Implementation

def shakersort(list_):
    start = -1
    end = len(list) - 2
    swapped = True
    while swapped:
        # go from start to end
        swapped = False
        start = start + 1
        for i in range(start, end + 1):
            if list_[i] > list_[i + 1]:
                list_[i], list_[i + 1] = list_[i + 1], list_[i]
                swapped = True
        if not swapped:
            break
        # go from end to start
        swapped = False
        end = end - 1
        for i in range(end, start + 1, -1):
            if list_[i] > list_[i + 1]:
                list_[i], list_[i + 1] = list_[i + 1], list_[i]
                swapped = True
    return list_

Performance

Combsort

Implementation

def combsort(list_):
    step = len(list_)
    while True:
        swapped = False
        if step > 1:
            step = int(step / 1.3)
        for i in range(len(list) - step):
            if list_[i] > list_[i + step]:
                list_[i], list_[i + step] = list_[i + step], list_[i]
                swapped = True
        if not swapped and step <= 1:
            break
    return list_

Performance

Insertionsort

Implementation

def insertionsort(list_):

    for i in range(1, len(list_)):
        tmp = list_[i]
        k = i
        while k > 0 and tmp < list_[k - 1]:
            list_[k] = list_[k - 1]
            k -= 1
        list_[k] = tmp

Performance

Quicksort

Implementation

def quicksort(list_):

    if not list_:
        return list_
    else:
        pivot = list_[0]
        lesser = quicksort([x for x in list_[1:] if x < pivot])
        greater = quicksort([x for x in list_[1:] if x >= pivot])
        lesser.append(pivot)
        return lesser.extend(greater)

Performance

Mergesort

Implementation

def mergesort(list_):

    if len(list_) <= 1:
        return list_

    middle = len(list_) / 2
    left = mergesort(list_[:middle])
    right = mergesort(list_[middle:])

    return list(merge(left,right))

Implementation (Cont.)

def merge(left, right):
    result = []
    left_idx, right_idx = 0, 0
    while left_idx < len(left) and right_idx < len(right):
        if left[left_idx] <= right[right_idx]:
            result.append(left[left_idx])
            left_idx += 1
        else:
            result.append(right[right_idx])
            right_idx += 1

    if left:
        result.extend(left[left_idx:])
    else:
        result.extend(right[right_idx:])

    return result

Performance

Natural Mergesort

Timsort

Performance

Radixsort

Implementation

from math import log10

def radixsort(list_):
    maxlen = -1
    for number in list_:
        numlen = int(log10(number)) + 1
        if numlen > maxlen:
            maxlen = numlen
    buckets = [[] for i in range(0, 10)]
    for digit in range(0, maxlen):
        for number in list_:
            buckets[number / 10 ** digit % 10].append(number)
        del list_[:]
        for bucket in buckets:
            list_.extend(bucket)
            del list_[:]
    return list_

Performance

Bogosort

Implementation

from random import shuffle

def bogosort(list_):
    while list_ != sorted(list_):
        shuffle(list_)
    return list_

Performance

Sound of sorting

Conclusions

SpaceForward
Left, Down, Page DownNext slide
Right, Up, Page UpPrevious slide
POpen presenter console
HToggle this help