Docsity

Insertion Sort-Study of Algorithms-Lecture Slides, Slides of Design and Analysis of Algorithms

Partial preview of the text

Download Insertion Sort-Study of Algorithms-Lecture Slides and more Slides Design and Analysis of Algorithms in PDF only on Docsity!

Insertion Sort ^ while some elements unsorted:^ ^ Using linear search, find the location in the sorted portionst^ where the 1^ element of the unsorted portion should beinserted^ ^ Move all the elements after the insertion location up oneposition to make space for the new element

13 2145 79 47 22 38

74 3666 94

2957 81 60

16 (^45666045) the fourth iteration of this loop is shown here

An insertion sort partitions the array into two regions

Insertion Sort Algorithm public void insertionSort(Comparable[] arr) {for (int i = 1; i < arr.length; ++i) {Comparable temp = arr[i];int pos = i;// Shuffle up all sorted items > arr[i]while (pos > 0 &&arr[pos-1].compareTo(temp) > 0) {arr[pos] = arr[pos–1];pos--;} // end while// Insert the current itemarr[pos] = temp;} }

Insertion Sort Analysispublic void insertionSort(Comparable[] arr) {for (int i = 1; i < arr.length; ++i) {Comparable temp = arr[i];int pos = i;// Shuffle up all sorted items > arr[i]while (pos > 0 &&arr[pos-1].compareTo(temp) > 0) {arr[pos] = arr[pos–1];pos--;} // end while// Insert the current itemarr[pos] = temp;} }

outer^ loop outer times inner^ loop

inner^ times

Insertion Sort: Cost Function ^ 1 operation to initialize the outer loop ^ The outer loop is evaluated

n-1^ times ^ 5 instructions (including outer loop comparison and increment) ^ Total cost of the outer loop: 5(n-1)  How many times the inner loop is evaluated is affected by thestate of the array to be sorted  Best case: the array is already completely sorted so no “shifting”of array elements is required. ^ We only test the condition of the inner loop once (2 operations = 1comparison + 1 element comparison), and the body is neverexecuted ^ Requires 2(n-1) operations.

Insertion Sort: Cost Function ^ Worst case: the array is sorted in reverse order (so each itemhas to be moved to the front of the array)^ ^ In the^ i -th iteration of the outer loop, the inner loop will perform

4i+ operations  Therefore, the total cost of the inner loop will be

2n(n-1)+n- ^ Time cost:^ ^ Best case:^ 7(n-1)^ ^ Worst case:^ 5(n-1)+2n(n-1)+n-1 ^ What about the number of moves?^ ^ Best case:^ 2(n-1)

moves  Worst case: 2(n-1)+n(n-1)/

Bubble Sort ^ Simplest sorting algorithm ^ Idea:^ ^ 1. Set flag = false^ ^ 2. Traverse the array and compare pairs of twoconsecutive elements^ ^ 1.1 If E1^ 

E2 -> OK (do nothing)  1.2 If E1 > E2 then Swap(E1, E2) and set flag = true  3. repeat 1. and 2. while flag=true.

Bubble Sort 1 1 23 2 56

---- finish the first traversal ---- 1 1 2 23 9

23 10^56

10 23 56^100

---- finish the second traversal ----

Bubble Sort: analysis ^ After the first traversal (iteration of the mainloop) – the maximum element is moved to itsplace (the end of array) ^ After the^ i -th traversal – largest

i^ elements are

in their places  Time cost, number of comparisons, numberof moves -> Assignment 4

O Notation

Growth Rate of an Algorithm ^ We often want to compare the performance ofalgorithms ^ When doing so we generally want to know how theyperform when the problem size (

n ) is large ^ Since cost functions are complex, and may bedifficult to compute, we approximate them using Onotation

Example of a Cost Function ^ Cost Function:

(^2) t ( n ) = n + 20 n A

^ Which term dominates?  It depends on the size of

n ^ n^ = 2,^ t ( n ) = 4 + 40 + 100A^ ^ The constant, 100, is the dominating term ^ n^ = 10,^ t ( n ) = 100 + 200 + 100A^ ^20 n^ is the dominating term ^ n^ = 100,^ t ( n ) = 10,000 + 2,000 + 100A^2 ^ n is the dominating term ^ n^ = 1000,^ t ( n ) = 1,000,000 + 20,000 + 100A^2 ^ n is the dominating term

Big O Notation ^ Given functions

t ( n)^ and^ g(n), w A^

e can say that the efficiency of an algorithm is

of order g(n)^ if there are positive constants

c^ and^ m^ such that ^ t (n) ·^ c.g ( n ) for allA^

n ¸ m ^ we write^ ^ t (n) is O( g ( n )) and we say thatA^ ^ t (n) is of orderA^

g ( n ) ^ e.g. if an algorithm’s running time is 3

n^ + 12 then the algorithm is O( n ). If

c^ is 3 and^ m^ is 12 then: ^ 4 *^ n^ ^3 n^ + 12 for all

n^ ^12

In English… ^ The cost function of an algorithm A,

t(n),^ can be approximated A by another, simpler, function

g(n)^ which is also a function with only 1 variable, the data size

n. ^ The function^ g(n)^

is selected such that it represents an

upper bound^ on the efficiency of the algorithm A (i.e. an upper boundon the value of^ t(n) ).A  This is expressed using the big-O notation: O

(g(n)). ^ For example, if we consider the time efficiency of algorithm Athen “ t (n) is O (g(n)) A

” would mean that ^ A cannot take more “time” than O

(g(n))^ to execute or that (more than^ c.g(n)^ for some constant

c ) ^ the cost function^ t(n) A

grows^ at most as^ fast as

g(n) docsity.com