Sorting Algorithms: Insertion Sort, Bubble Sort, Selection Sort, and Stability - Prof. Dav, Study notes of Data Structures and Algorithms

An overview of various sorting algorithms, including insertion sort, bubble sort, selection sort, and their stability. It covers the running time complexities, best and worst cases, and stability of each algorithm.

Typology: Study notes

Pre 2010

Uploaded on 07/30/2009

koofers-user-emz
koofers-user-emz 🇺🇸

9 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS245-2009S-10 Sorting 1
10-0: Main Memory Sorting
All data elements can be stored in memory at the same time
Data stored in an array, indexed from 0.. . n 1, where nis the number of elements
Each element has a key value (accessed with a key() method)
We can compare keys for ¡, ¿, =
For illustration, we will use arrays of integers though often keys will be strings, other Comparable types
10-1: Stable Sorting
A sorting algorithm is Stable if the relative order of duplicates is preserved
The order of duplicates matters if the keys are duplicated, but the records are not.
3 1 2 1 1 2 3
B
o
b
J
o
e
E
dA
m
y
S
u
e
A
lB
u
d
Key
Data
1 1 1 2 2 3 3
A
m
y
J
o
e
S
u
e
E
dA
lB
o
b
B
u
d
Key
Data
Anon-Stable sort
10-2: Insertion Sort
Separate list into sorted portion, and unsorted portion
Initially, sorted portion contains first element in the list, unsorted portion is the rest of the list
(A list of one element is always sorted)
Repeatedly insert an element from the unsorted list into the sorted list, until the list is sorted
10-3: Θ() For Insertion Sort
Running time # of comparisons
Worst Case:
10-4: Θ() For Insertion Sort
Running time # of comparisons
Worst Case: Inverse sorted list
# of comparisons:
10-5: Θ() For Insertion Sort
Running time # of comparisons
pf3
pf4

Partial preview of the text

Download Sorting Algorithms: Insertion Sort, Bubble Sort, Selection Sort, and Stability - Prof. Dav and more Study notes Data Structures and Algorithms in PDF only on Docsity!

10-0: Main Memory Sorting

  • All data elements can be stored in memory at the same time
  • Data stored in an array, indexed from 0... n − 1 , where n is the number of elements
  • Each element has a key value (accessed with a key() method)
  • We can compare keys for ¡, ¿, =
  • For illustration, we will use arrays of integers – though often keys will be strings, other Comparable types

10-1: Stable Sorting

  • A sorting algorithm is Stable if the relative order of duplicates is preserved
  • The order of duplicates matters if the keys are duplicated, but the records are not.

Bo b

Jo e

Ed Am y

Su e

Al Bu d

Key

Data

Am y

Jo e

Su e

Ed Al Bo b

Bu d

Key

Data

A non- Stable sort

10-2: Insertion Sort

  • Separate list into sorted portion, and unsorted portion
  • Initially, sorted portion contains first element in the list, unsorted portion is the rest of the list
    • (A list of one element is always sorted)
  • Repeatedly insert an element from the unsorted list into the sorted list, until the list is sorted 10-3: Θ() For Insertion Sort
  • Running time ∝ # of comparisons
  • Worst Case: 10-4: Θ() For Insertion Sort
  • Running time ∝ # of comparisons
  • Worst Case: Inverse sorted list

of comparisons:

10-5: Θ() For Insertion Sort

  • Running time ∝ # of comparisons
  • Worst Case: Inverse sorted list

of comparisons: (^) n− 1

∑ i=

i ∈ Θ(n^2 )

10-6: Θ() For Insertion Sort

  • Running time ∝ # of comparisons
  • Best Case: 10-7: Θ() For Insertion Sort
  • Running time ∝ # of comparisons
  • Best Case: Sorted List

of comparisons:

10-8: Θ() For Insertion Sort

  • Running time ∝ # of comparisons
  • Best Case: Sorted List

of comparisons:

n − 1 10-9: Bubble Sort

  • Scan list from the last index to index 0, swapping the smallest element to the front of the list
  • Scan the list from the last index to index 1, swapping the second smallest element to index 1
  • Scan the list from the last index to index 2, swapping the third smallest element to index 2 ...
  • Swap the second largest element into position (n − 2) 10-10: Θ() for Bubble Sort
  • Running time ∝ # of comparisons
  • Number of Comparisons: 10-11: Θ() for Bubble Sort
  • Running time ∝ # of comparisons
  • Number of Comparisons: n∑− 1 i=

i ∈ Θ(n^2 )

10-12: Selection Sort

  • Scan through the list, and find the smallest element
  • Various sorts do not interact much
  • If all large elements are stored in large indices, and small elements are stored in even indices, what hap- pens? 10-18: Other Increments
  • Shell’s Increments: {n/ 2 , n/ 4 ,... 4 , 2 , 1 }
  • Running time: O(n^2 )
  • “/ 3 ” increments: {n/ 3 , n/ 9 ,... , 9 , 3 , 1 }
  • Running time: O(n 32 )
  • Hibbard’s Increments: { 2 k^ − 1 , 2 k−^1 − 1 ,... 7 , 3 , 1 }
  • Running time: O(n 32 )

10-19: Stability

  • Is Insertion sort stable?
  • Is Bubble Sort stable?
  • Is Selection Sort stable?
  • Is Shell Sort stable?

10-20: Stability

  • Is Insertion sort stable? Yes!
  • Is Bubble Sort stable? Yes!
  • Is Selection Sort stable? No!
  • Is Shell Sort stable? No! Note that minor changes to the stable sorting algorithms will make them unstable (for instance, swaping A[i] and A[i + 1] when A[i] ≥ A[i + 1], not just when A[i] > A[i + 1]