Sei sulla pagina 1di 4

Compute running time of the following algorithms: Algorithm 1:

ALGORITHM MinDistance(A[0..n 1]) //Input: Array A[0..n 1] of numbers //Output: Minimum distance between two of its elements dmin for i 0 to n 1 do for j 0 to n 1 do if i _= j and |A[i] A[j ]| < dmin dmin |A[i] A[j ]| return dmin

Algorithm 2:
ALGORITHM SequentialSearch(A[0..n 1], K) //Searches for a given value in a given array by sequential search //Input: An array A[0..n 1] and a search key K //Output: The index of the first element in A that matches K // or 1 if there are no matching elements i 0 while i < n and A[i] _= K do i i + 1 if i < n return i else return 1

Algorithm 3:
ALGORITHM MaxElement(A[0..n 1]) //Determines the value of the largest element in a given array //Input: An array A[0..n 1] of real numbers //Output: The value of the largest element in A maxval A[0] for i 1 to n 1 do if A[i]>maxval maxvalA[i] return maxval

Algorithm 4: ALGORITHM UniqueElements(A[0..n 1]) //Determines whether all the elements in a given array are distinct //Input: An array A[0..n 1] //Output: Returns true if all the elements in A are distinct // and false otherwise for i 0 to n 2 do for j i + 1 to n 1 do if A[i]= A[j ] return false return true Algorithm 5: ALGORITHM MatrixMultiplication(A[0..n 1, 0..n 1], B[0..n 1, 0..n 1]) //Multiplies two square matrices of order n by the definition-based algorithm //Input: Two n n matrices A and B //Output: Matrix C = AB for i 0 to n 1 do for j 0 to n 1 do C[i, j ]0.0 for k0 to n 1 do C[i, j ]C[i, j ]+ A[i, k] B[k, j] return C Algorithm 6: ALGORITHM Binary(n) //Input: A positive decimal integer n //Output: The number of binary digits in ns binary representation count 1 while n > 1 do count count + 1 n_n/2_ return count

Algorithm 7: ALGORITHM Mystery(n) //Input: A nonnegative integer n S 0 for i 1 to n do S S + i i return S Algorithm 8: ALGORITHM Secret(A[0..n 1]) //Input: An array A[0..n 1] of n real numbers minvalA[0]; maxvalA[0] for i 1 to n 1 do if A[i]< minval minvalA[i] if A[i]> maxval maxvalA[i] return maxval minval Algorithm 9: ALGORITHM Enigma(A[0..n 1, 0..n 1]) //Input: A matrix A[0..n 1, 0..n 1] of real numbers for i 0 to n 2 do for j i + 1 to n 1 do if A[i, j ] _= A[j, i] return false return true

Algorithm 10: ALGORITHM InsertionSort(A[0..n 1]) //Sorts a given array by 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] j i 1 while j 0 and A[j ]> v do A[j + 1]A[j ] j j 1 A[j + 1]v Algorithm 11: ALGORITHM InsertSort2(A[0..n 1]) for i 1 to n 1 do j i 1 while j 0 and A[j ]>A[j + 1] do swap(A[j ], A[j + 1]) j j 1 Algorithm 12: ALGORITHM LomutoPartition(A[l..r]) //Partitions subarray by Lomutos algorithm using first element as pivot //Input: A subarray A[l..r] of array A[0..n 1], defined by its left and right // indices l and r (l r) //Output: Partition of A[l..r] and the new position of the pivot pA[l] s l for i l + 1 to r do if A[i]<p s s + 1; swap(A[s], A[i]) swap(A[l], A[s]) return s

Potrebbero piacerti anche