




















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An analysis of various sorting algorithms, including bubble sort, selection sort, insertion sort, merge sort, and quicksort. It explains the concept of order notation, including big-o, big-omega, small-oh, and small-omega notations. The document also includes examples and code snippets for each sorting algorithm, as well as their respective time complexities.
Typology: Slides
1 / 28
This page cannot be seen from the preview
Don't miss anything!





















Docsity.com
Docsity.com 2
There may be a situation, e.g.
Tt
1 n
g(n)
f(n)
n 0 f(n) <= g(n) for all n >= n 0 Or f(n) <= cg(n) for all n >= n 0 and c = 1 g(n) is an asymptotic upper bound on f(n).
f(n) = O(g(n)) iff there exist two positive constants c and n 0 such that
f(n) <= cg(n) for all n >= n 0 Docsity.com
Asymptotic Lower Bound: f(n) = (g(n)), iff there exit positive constants c and n 0 such that f(n) >= cg(n) for all n >= n 0
n 0 n
f(n)
g(n)
Docsity.com
Example: show that (1/2)n^2 – 3n = (n^2 )
To do so we must determine positive constants c 1 , c 2 and n 0 such that c 1 n^2 <= (1/2)n^2 – 3n <= c 2 n^2 , n >= n 0
Dividing by n^2 c 1 <= ½ - 3/n <= c 2
Right Hand Inequality ½ <= c 2 + 3/n
For positive n, if c 2 >= ½ then the inequality holds.
Left Hand Inequality c 1 + 3/n <= ½
For n = 7 and c 1 <= 1/14, the inequality holds.
c 1 <= 1/14, c 2 >= ½ and n = 7
Docsity.com
Some Rules About Asymptotic Notation
T(x) = (xn)
Docsity.com
10
(done)
Docsity.com
11
public static void bubbleSort(int[] a) { int outer, inner; for (outer = a.length - 1; outer > 0; outer--) { // counting down for (inner = 0; inner < outer; inner++) { // bubbling up if (a[inner] > a[inner + 1]) { // if out of order... int temp = a[inner]; // ...then swap a[inner] = a[inner + 1]; a[inner + 1] = temp; } } } }
Docsity.com
13
Search elements 0 through n-1 and select the smallest Swap it with the element in location 0 Search elements 1 through n-1 and select the smallest Swap it with the element in location 1 Search elements 2 through n-1 and select the smallest Swap it with the element in location 2 Search elements 3 through n-1 and select the smallest Swap it with the element in location 3 Continue in this fashion until there’s nothing left to search
Docsity.com
14
public static void selectionSort(int[] a) { int outer, inner, min; for (outer = 0; outer < a.length - 1; outer++) { // outer counts down min = outer; for (inner = outer + 1; inner < a.length; inner++) { if (a[inner] < a[min]) { min = inner; } // Invariant: for all i, if outer <= i <= inner, then a[min] <= a[i] } // a[min] is least among a[outer]..a[a.length - 1] int temp = a[outer]; a[outer] = a[min]; a[min] = temp; // Invariant: for all i <= outer, if i < j then a[i] <= a[j] } } Docsity.com
16
sorted next to be inserted
temp
sorted
less than 10
Docsity.com
17
public static void insertionSort(int[] array) { int inner, outer;
for (outer = 1; outer < array.length; outer++) { int temp = array[outer]; inner = outer; while (inner > 0 && array[inner - 1] >= temp) { array[inner] = array[inner - 1]; inner--; } array[inner] = temp; // Invariant: For all i < outer, j < outer, if i < j then a[i] <= a[j] } }
Docsity.com
Conceptually, incremental add element to sorted array or list, starting with an empty array (list). Incremental or batch algorithm.
In best case, input is sorted: time is O(N) In worst case, input is reverse sorted: time is O(N^2 ). Average case is (loose argument) is O(N^2 )
Docsity.com
20
Bubble Sort, Selection Sort, and Insertion Sort are all O(n^2 )
Within O(n^2 ),
Bubble Sort is very slow, and should probably never be used for anything Selection Sort is intermediate in speed Insertion Sort is usually the fastest of the three--in fact, for small arrays (say, 10 or 15 elements), insertion sort is faster than more complicated sorting algorithms
Selection Sort and Insertion Sort are “good enough” for small
arrays
Docsity.com