0% found this document useful (0 votes)
29 views22 pages

Practice 3

Uploaded by

khoipn.23bi14228
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views22 pages

Practice 3

Uploaded by

khoipn.23bi14228
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 22

Algorithm analysis

Practice
Sample to practice
Computing Prefix Averages Algorithm
 We further illustrate asymptotic analysis with two
algorithms for prefix averages
 The i-th prefix average of an array X is average of
the first (i + 1) elements of X:
A[i] = (X[0] + X[1] + … + X[i])/(i+1)
1. Calculate running time
Algorithm prefixAverages1(X, n)
Input array X of n integers
Output array A of prefix averages of X #operations
A  new array of n integers ---------- n
for i  0 to n  1 do ---------- n
{ s  X[0] ---------- n
for j  1 to i do ---------- 1+
2 + …+ (n  1)
s  s + X[j] ----------
1 + 2 + …+ (n  1)
A[i]  s / (i + 1) } ---------- n
return A T(n) =----------
n+n+n+(1+2+…+(n-1)) + (1+2+…+(n-1))
1 + n+1
= 2(1+2+…+n) + 2n + 1 = n(n+1)+2n+1
= n2+3n+1
2. Simplify and return big Oh

 Simplify T(n) = n2
 Thus, algorithm prefixAverages1 runs in O(n2) time
Practice
Insert additional 2 slides after each algorithm
• Calculate total running time into a function
• Simplify the running-time function and indicate the big Oh
Find maximum number in nxn matrix

0 n-1
Algorithm: 0 6 4 … 2
Input: A, n 12 3 … 9
curMax = A[0][0] … … … …
for i=0; i<n; i=i+1 n-1 5 8 … 1

for j=0; j<n; j=j+1


if curMax < A[i][j]
curMax = A[i][j]
return curMax
Matrix multiplication
Maximum subsequence sum (1)
int MaxSubSum (int A[], int N) {
int thissum, maxsum, i,j,k;
1. maxsum = 0;
2. for (i=0; i<N; i=i+1)
3. for (j=i; j<N; j=j+1) {
4. thissum = 0;
5. for (k=i; k <= j; k++)
6. thissum += A[k];
7. if (thissum > maxsum)
8. maxsum = thissum;
}
9. return maxsum;
}
Counting binary digits
Binary Search Algorithm

int BinarySearch (int A[], int X, int N) {


int low, mid, high;
while (low <= high) {
mid = (low+high)/2;
if (A[mid] < X) low = mid+1;
else if (A[mid] > X) high = mid-1;
else return mid;
}
return -1;
}
Sum of the prefix

Algorithm Ex1(A, n)
Input an array X of n integers
Output the sum of the prefix sums A
s0
for i  0 to n  1 do
{ s  s + A[0]
for j 1 to i do
s  s + A[j]
}
return s
Determine how many elements of array
1 match elements of array 2
 Algorithm?
0 n-1
Algorithm:
2 4 … 6
Input: A, B, n
0 n-1
for i=0; i<n; i=i+1 6 8 … 3
for j=0; j<n; j=j+1
if A[i] == A[j]
matches++
break
Maximum subsequence sum (2)
int MaxSubSum2 (int A[], int N)
{
int thissum, maxsum, i, j;
1. maxsum = 0;
2. for (i=0; i<N; i=i+1)
3. {
3. thissum = 0;
4. for (j=i; j < N; j=j+1)
5. {
5. thissum += A[j];
6. if (thissum > maxsum)
7. maxsum = thissum;
}
}
8. return maxsum;
}
Linear Search algorithm

int search (int A[], int X, int N)


{
int i;
for (i=0; i<N; i=i+1)
if (A[i] == X)
return i;
return -1;
}
Sorting integers Algorithm

void sort (int A[], int N) {


int i, j, x;
for (i=1; i<N; i=i+1) {
x = A[i];
for (j=i; j>0 && x<A[j-1]; j--)
A[j] = A[j-1];
A[j] = x;
}
}
Bubble Sort Algorithm
void swap(int *xp, int *yp)
{
int temp = *xp;
*xp = *yp;
*yp = temp;
}
// A function to implement bubble sort
void bubbleSort(int arr[], int n)
{
int i, j;
for (i = 0; i < n-1; i=i+1)

// Last i elements are already in place


for (j = 0; j < n-i-1; j=j+1)
if (arr[j] > arr[j+1])
swap(&arr[j], &arr[j+1]);
}
Insertion Sort Algorithm
void insertionSort(int arr[], int n)
{
int i, key, j;
for (i = 1; i < n; i=i+1) {
key = arr[i];
j = i - 1;

/* Move elements of arr[0..i-1], that are


greater than key, to one position ahead
of their current position */
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}
Selection Sort Algorithm
void selectionSort(int arr[], int n)
{
int i, j, min_idx;

// One by one move boundary of unsorted subarray


for (i = 0; i < n-1; i=i+1)
{
// Find the minimum element in unsorted array
min_idx = i;
for (j = i+1; j < n; j=j+1)
if (arr[j] < arr[min_idx])
min_idx = j;

// Swap the found minimum element with the first element


swap(&arr[min_idx], &arr[i]);
}
}
Maximum element
Element uniqueness problem
Gaussian elimination
Algorithm GaussianElimination(A[0..n-1,0..n])
//Implements Gaussian elimination of an n-by-(n+1) matrix A
for i  0 to n - 2 do
for j  i + 1 to n - 1 do
for k  i to n do
A[j,k]  A[j,k] - A[i,k]  A[j,i] / A[i,i]

You might also like