Chapter5-2-DivideConquer

Comparisons no for a sorted list of one million for

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 2 comparisons to no find an element in a sorted list of 1000 elements find • no more than 20 iterations, 20*2 comparisons no for a sorted list of one million! for Design and Analysis of Algorithms – Chapter 4 8 Improved insertion a Binary search technique can be used to Binary improve the insertion into an ordered array list improve Design and Analysis of Algorithms – Chapter 4 9 Insertion sort - - Design and Analysis of Algorithms – Chapter 4 10 Improved insertion a Binary search technique can be used to improve the Binary insertion into an ordered array list insertion a First, using the binary search to find the location First, where the new element should be inserted where a Second, insert the new element into the array list Design and Analysis of Algorithms – Chapter 4 11 BinaryInsertionSort Algorithm BinaryInsertionSort(A[0…n-1]) //Sorts a given array by binary insertion sort //Input: An array A[0…n-1] of n orderable elements //Output: Array A[0…n-1] sorted in nondecreasing order for i ← 1 to n -1 do v ← A[i]; left ← 0; right ← i; while left < right do middle ← (left + right) / 2; if v ≥ A[middle] left ← middle + 1; else right ← middle; for j ← i to left + 1 do A[j] ← A[j-1]; A[left] ← v Design and Analysis of Algorithms – Chapter 4 12 In-Class Exercise (4.3.1) Design and Analysis of Algorithms – Chapter 4 13 Multiplication of large integers a a = a1a0 a b = b1b0 ac = a * b n/2 = (a110n/2 + a0) * (b110n/2 + b0) (a n/2 =(a1 * b1)10n + (a1 * b0 + a0 * b1)10n/2 + (a0 * b0) =(a For instance: a = 123456, b = 117933: For Then c = a * b = (123*103+456)*(117*103+933) Then =(123 * 117)106 + (123 * 933 + 456 * 117)103 + =(123 117)10 (456 * 933)Design and Analysis of Algorithms – Chapter 4 (456 14 Multiplication of large integers a a = a1a0 a b = b1b0 ac=a*b n/2 = (a110n/2 + a0) * (b110n/2 + b0) (a n/2 =(a1 * b1)10n + (a1 * b0 + a0 * b1)10n/2 + (a0 * b0) =(a =c210n + c110n/2 + c0, =c where c2 = a1 * b1 is the product of their first half is the c0 = a0 * b0 is the product of their secon...
View Full Document

This note was uploaded on 11/29/2012 for the course CSCE 310 taught by Professor Staff during the Fall '08 term at UNL.

Ask a homework question - tutors are online