We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5
Algorithm 3: Finding Maximum Element:
Algorithm findMax(a, n) { max = a[1]; for i = 2 to n do if a[i] > max then max = a[i]; return max; }
Algorithm 4: Reversing an Array:
Algorithm reverseArray(a, n) { for i = 1 to n/2 do temp = a[i]; a[i] = a[n-i+1]; a[n-i+1] = temp; }
Algorithm 5: Counting Occurrences of an Element:
Algorithm countOccurrences(a, n, x) { count = 0; for i = 1 to n do if a[i] == x then count = count + 1; return count; } Algorithm 6: Bubble Sort: Algorithm bubbleSort(a, n) { for i = 1 to n-1 do for j = 1 to n-i do if a[j] > a[j+1] then temp = a[j]; a[j] = a[j+1]; a[j+1] = temp; }
Algorithm 7: Linear Search:
Algorithm linearSearch(a, n, key) { for i = 1 to n do if a[i] == key then return i; return -1; }
Algorithm 8: Insertion Sort:
Algorithm insertionSort(a, n) { for i = 2 to n do key = a[i]; j = i - 1; while j > 0 and a[j] > key do a[j + 1] = a[j]; j = j - 1; a[j + 1] = key; }
Algorithm 9: Binary Search (Iterative):
Algorithm binarySearch(a, n, key) { left = 1; right = n; while left <= right do mid = (left + right) / 2; if a[mid] == key then return mid; else if a[mid] < key then left = mid + 1; else right = mid - 1; return -1; }
Algorithm 10: Selection Sort:
Algorithm selectionSort(a, n) { for i = 1 to n-1 do minIndex = i; for j = i + 1 to n do if a[j] < a[minIndex] then minIndex = j; if minIndex != i then temp = a[i]; a[i] = a[minIndex]; a[minIndex] = temp; }
Algorithm 11: Merge Two Sorted Arrays:
Algorithm mergeArrays(a, b, n, m, c) { i = 1; j = 1; k = 1; while i <= n and j <= m do if a[i] < b[j] then c[k] = a[i]; i = i + 1; else c[k] = b[j]; j = j + 1; k = k + 1; while i <= n do c[k] = a[i]; i = i + 1; k = k + 1; while j <= m do c[k] = b[j]; j = j + 1; k = k + 1; } Algorithm 12: Compute Factorial (Iterative): Algorithm factorial(n) { result = 1; for i = 1 to n do result = result * i; return result; }