📝
Tzu-Heng's wiki
  • Tzu-Heng's wiki
  • Machine Learning
    • Traditionals
    • Deep Learning
    • Image Classification (CNN)
    • Detection
    • Semantic Segmentation
    • Generative Adversarial Networks
    • Style Transfer
    • Recommender Systems
    • Meta Learning
  • Notes
    • Differientiable Sampling and Argmax
    • GAN theory
    • Multi-task Learning (MTL)
    • Disentanglement in GANs
    • CNN practical notes
    • 3D Clothes
    • OpenGL
    • Generative Art
    • nginx usage
    • Deploy Deep Learning Models
    • Character Motion Synthesis
  • Data Structure & Algorithms
    • Sorting Algorithms
Powered by GitBook
On this page
  • Bubble Sort
  • Selection Sort
  • Insertion Sort
  • Merge Sort
  • Quick Sort
  • Heap Sort

Was this helpful?

  1. Data Structure & Algorithms

Sorting Algorithms

Bubble Sort

def bubble_sort(a):
    length = len(a)
    for i in range(0, length):
        for j in range(0, i):
            if a[i] < a[j]:
                a[i], a[j] = a[j], a[i]

Selection Sort

def selection_sort(a):
    length = len(a)
    for i in range(length):
        min_idx = i
        for j in range(i, length):
            if a[j] < a[min_idx]:
                min_idx = j
        a[min_idx], a[i] = a[i], a[min_idx]

Insertion Sort

def insertion_sort(a):
    length = len(a)
    for i in range(1, length):
        key = a[i]
        j = i - 1
        while a[j] > key and j >= 0:
            a[j + 1] = a[j]
            j -= 1
        a[j + 1] = key

Merge Sort

Quick Sort

def quick_sort(a, l, r):
    if l < r:
        pos = partition(a, l, r)
        quick_sort(a, l, pos - 1)
        quick_sort(a, pos + 1, r)

def partition(a, l, r):
    pivot = a[r]
    i = l - 1
    for j in range(l, r):
        if a[j] < pivot:
            i += 1
            a[j], a[i] = a[i], a[j]
    a[i + 1], a[r] = a[r], a[i + 1]
    return i + 1

Heap Sort

PreviousCharacter Motion Synthesis

Last updated 3 years ago

Was this helpful?