Professional Documents
Culture Documents
General definition
Locate an element x in a list of distinct elements a1, a2 , , an, or determine that it is not in the list Linear search
Algorithm 2: Linear search Input: unsorted sequence a1, a2 , , an position of target value x Output: subscript of entry equal to target value; 0 if not found Initialize: i 1 while (i n and x ai) ii+1 if i n then location i else location 0
F. Pedone, N. Schiper
Searching Algorithms
Binary search
Requires input sequence to be sorted General Idea:
Find 9 in the sequence: <1, 4, 6, 9, 10, 14> left mid right left mid right
Searching Algorithms
Binary search
Algorithm 3: Binary search Input: sorted sequence a 1 , a2, , an position of target value x Output: subscript of entry equal to target value; 0 if not found Initialize: left 1; right n while (left < right) mid (left + right) / 2 if x > amid then left mid + 1 else right mid if x = a left then location left else location 0
F. Pedone, N. Schiper
Complexity Analysis
Usually time complexity considered Space complexity can also be considered RAM Model
Constant time basic operations (add, sub, load, store)
Complexity Analysis
Algorithm 2: Linear search Initialize: i 1 while (i n and x ai) ii+1 if i n then location i else location 0
Worst-case: element is at the end of the sequence or is not present n iterations of loop c1 + c2 => (n)
Avrg-case: element is at the middle of the sequence n/2 iterations of loop c1 + c2 => (n)
F. Pedone, N. Schiper 6
Complexity Analysis
Algorithm 3: Binary search Initialize: left 1; right n while (left < right) mid (left + right) / 2 if x > amid then left mid + 1 else right mid if x = a left then location left else location 0 n elements height = k
Sorting Algorithms
General definition
Putting a number of elements into a list in which the elements are in increasing order
Input:
A sequence of n numbers <a1, a2, , an>
Output:
A permutation (reordering) <a1, a 2, , a n> of the input sequence such that a 1 a2 an
=> (k) = (log2 n)
7 F. Pedone, N. Schiper 8
Sorting Algorithms
Insertion sort General idea:
Same idea as what you do when cards are distributed Your left hand is initially empty Until all cards have been distributed
Take a card with right hand and insert it at the right position in left hand
Sorting Algorithms
Insertion sort
Algorithm 3: Insertion sort Input: unsorted sequence a1, a2 , , an Output: sorted sequence a 1, a2 , , an for j 2 to n distribute all cards key a j ij-1 while i > 0 and a i > a j a i+1 a i ii-1 a i key insert at right position in left hand
F. Pedone, N. Schiper
F. Pedone, N. Schiper
10
Termination:
<3, 4, 10, 18, 7> j All operations of the algorithm take a finite amount of time The algorithm executes a bounded number of loop iterations
F. Pedone, N. Schiper
11
F. Pedone, N. Schiper
12
F. Pedone, N. Schiper
13
F. Pedone, N. Schiper
14
Induction step:
Property holds by condition of while loop and last line
15 F. Pedone, N. Schiper 16
F. Pedone, N. Schiper
Sorting Algorithms
Bubble Sort
Algorithm 4: Bubble sort Input: unsorted sequence a1, a2 , , an Output: sorted sequence a 1, a2 , , an for i 1 to n for j n to i + 1 if a j < aj-1 then interchange a j and aj-1
F. Pedone, N. Schiper
17
F. Pedone, N. Schiper
18
Complexity Analysis
Algorithm 3: Insertion sort for j 2 to n key a j ij-1 while i > 0 and a i > a j a i+1 a i ii-1 a i key
Worst-case: input is sorted in reverse order n j"1 (n "1) 2 # # c = c + 2c + ...+ (n "1)c = 2 c => (n2) j= 2 i=1
19 F. Pedone, N. Schiper 20
F. Pedone, N. Schiper
Complexity Analysis
Algorithm 3: Insertion sort for j 2 to n key a j ij-1 while i > 0 and a i > a j a i+1 a i ii-1 a i key
Complexity Analysis
Algorithm 4: Bubble sort for i 1 to n for j n to i+1 if a j < aj-1 then interchange a j and a j-1
(n #1) 2 c 2
=> (n2)
Average-case: half of the elements in a1..aj-1 are greater than aj n #( j"1)/ 2$ # n "1$ n + 1 n "1 % % c = c + c + 2c + 2c + ...+ & 2 'c ( 2 ) 2 => (n2) & ' j= 2 i=1
F. Pedone, N. Schiper 21
!
F. Pedone, N. Schiper 22
Summary
Algorithm Linear search Binary search Insertion sort Bubble sort Avrg-case (n) (log2(n)) (n2 ) (n2 ) Worst-case (n) (log2(n)) (n2 ) (n2 )
Complexity of algorithms
Common terminology
Complexity Terminology O(1) O(log n) O(n) O(n log n) O(n b ) O(b n ), b > 1 O(n!) Constant complexity Logarithmic complexity Linear complexity n log n complexity Polynomial complexity Exponential complexity Factorial complexity
F. Pedone, N. Schiper
23
F. Pedone, N. Schiper
24