Data Structures and Algorithms Lecture Notes

13 April 2011 • Sorting Basics


Outline

Some Questions

Arrangements and Access

Sorting

Sorting Properties

Sorting Components

Sort Data

Sort Predicates

Sort Ordering

Data Collections

Sorting Algorithm

How to Sort

Bubble Sort

bubble sorting

bubble sort(T a[], n)
  for i = 0 to n - 2
    for min = n - 1 to i + 1
      if a[min - 1] > a[min]
        swap(a[min - 1], a[min])

Bubble Sorting

Another Question

another question

Insertion Sort

another question answered

insertion sort(T a[], n)
  for i = 1 to n - 1
    T x = a[i]
    gap = i
    while gap > 0 and a[gap - 1] > x
      a[gap] = a[gap - 1]
      gap--
    a[gap] = x

Bubbling vs. Inserting

Oh Really?

bubble vs insert on ints

bubble vs insert on points

The Timing Details

Bubbling Without Drag

Selection Sort

bubble sorting

selection sort(T a[], n)
  for i = 0 to n - 2
    min = i
    for j = i + 1 to n - 1
      if a[min] > a[j]
        min = j
    swap(a[min], a[i])

Selection Sorting

Selecting vs. Inserting

Does It?

bubble vs insert vs selection on ints

bubble vs insert vs selection on points

Summary


This page last modified on 2010 April 11.

Creative
    Commons License