🐻
berkeley
  • #Despair
  • 16A/B
    • Thevenin and Norton Equivalence
    • Resistive Touchscreen
    • Trilateration and Correlation
    • RC Circuits & Transient Analysis
  • 61B
    • Testing
    • References in Java
    • Linked Lists in Java
    • Implementation Inheritance
    • Comparables and Iterators
    • Casting
    • Lists and Sets
    • Asymptotics
    • Trees
    • Hashing
    • PriorityQueue and Heap
    • Graphs, Tree Traversals
    • Tries
    • Sorting
  • Discrete Mathematics
    • Sets + Mathematical Notation
    • Logic and Proofs
    • Induction
    • Cryptography/RSA
    • Modular Arithmetic
    • Discrete Probability
  • Linear Algebra
    • Linear Equations
    • Matrix Algebra
    • Determinants
    • Vector Spaces
    • Eigenvectors and Eigenvalues
    • Projections and Orthogonality
    • SVD/PCA
  • Math 275 — Ordinary Differential Equations
    • Math 275 — Overview
    • Modeling via Differential Equations
    • Linear Systems
  • INTEGBI 35AC
    • Humans Came From Fish
    • Evolution's History
    • Typology
    • The Human Diaspora
  • 170
    • Basics
    • Divide and Conquer
    • Fast Fourier Transform
    • Graphs
    • Greedy
  • 61C
    • Pointers and Memory
    • Floating Point
    • Compliation, Assembly, Linking, Loading
    • RISC-V, Assembly
Powered by GitBook
On this page
  • Selection sort
  • Heapsort
  • Merge Sort
  • Insertion Sort
  • Counting Sort
  • LSD Radix Sort

Was this helpful?

  1. 61B

Sorting

Selection sort

Selection sort involves the following.

  • Find the smallest item in the unsorted part of the array.

  • Move it to the end of the sorted array.

  • Repeat for the rest of the unsorted portion, until the unsorted portion no longer exists.

This is slow. We need Θ(N2)\Theta(N^2)Θ(N2) time to run it.

Heapsort

A naive implementation would involve inserting all the elements into a max heap, and then removing every element from the heap and adding it to an imput array. We can save space by doing the following.

  • Bottom up heapify the the input array — sink nodes in reverse level order.

  • Delete largest item from the heap, swapping this item with the last one in the heap array.

Runtime is linear for an array of all duplicates, but the worst case runtime is Θ(NlogN)\Theta(NlogN)Θ(NlogN) and it uses constant space.

Merge Sort

  • Split items into 2 even pieces.

  • Merge sort each half.

  • Merge two sorted halves to form the final result.

    • Compare the items in the list, whichever one is lesser add it to the final array until there's no more left of the two merge sorted arrays.

    Runtime wise, merge sort is always Θ(Nlog⁡(N))\Theta(N\log(N))Θ(Nlog(N)) in both the worst and best case.

Insertion Sort

  • Designate i as a traveling item.

  • Swap it backwards until the item is in the right place.

In the best case, we get a Θ(N)\Theta(N)Θ(N) runtime — furthermore, this runtime is actually the same as the runtime for an almost sorted list. Unfortunately, in the case of the average case, we have Θ(N2)\Theta(N^2)Θ(N2) .

Any comparison based sort uses at least Θ(Nlog⁡N)\Theta(N\log N) Θ(NlogN) comparisons.

Counting Sort

In order to avoid the previous constraint, we can just sort without comparing. If items in a list have some kind of key, and those keys have some kind of ordering to them, we can

  • Count the number of times each key occurs

  • Iterate through list and place into new list, using cumulative count array to decide where everything goes.

LSD Radix Sort

This is a counting sort that does not rely on an alphabet keys having a finite length. We just sort by each digit independently, in this case doing a counting sort by the least significant digit. When keys are of different lengths, just treat the empty spaces as less than all the other digits.

PreviousTriesNextSets + Mathematical Notation

Last updated 4 years ago

Was this helpful?