Docsity

Analysis of Sorting Algorithms: Big-O Notation and Common Sorting Algorithms, Slides of Design and Analysis of Algorithms

Partial preview of the text

Download Analysis of Sorting Algorithms: Big-O Notation and Common Sorting Algorithms and more Slides Design and Analysis of Algorithms in PDF only on Docsity!

Analysis of Sorting Algorithms

Docsity.com

 f(x) = O(g(x)) (big-oh) means that the growth rate

of f(x) is asymptotically less than or equal to the

growth rate of g(x).

 f(x) = Ω(g(x)) (big-omega) means that the growth rate

of f(x) is asymptotically greater than or equal to the

growth rate of g(x)

 f(x) = o(g(x)) (small-oh) means that the growth rate

of f(x) is asymptotically less than to the growth rate

of g(x).

Docsity.com 2

Order Notation

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

Order Notation

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

Order Notation

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

1. If T 1 (n) = O(f(n)) and T 2 (n) = O(g(n))

Then T 1 (n) + T 2 (n) = Max(O(f(n)), O(g(n)))

T 1 (n) * T 2 (n) = O(f(n) *g(n))

2. If T(x) is a polynomial of degree n, then

T(x) = (xn)

3. logk(n) = O(n) for any constant k. This tells that

logarithms grow very slowly.

4. Do not include any constants or low order terms inside a

big-Oh, e.g.,

T(n) = O(2n^2 ) -------- wrong

T(n) = O(n^2 + n) ----- wrong

Docsity.com

10

Example of Bubble Sort

(done)

Docsity.com

11

Code for Bubble Sort

 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

Selection Sort

 Given an array of length n,

 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

Code for Selection Sort

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

One step of insertion sort

sorted next to be inserted

temp

sorted

less than 10

Docsity.com

17

Code for Insertion Sort

 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

Insertion Sort

 Algorithm

 Conceptually, incremental add element to sorted array or list, starting with an empty array (list).  Incremental or batch algorithm.

 Analysis

 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

Summary

 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