Professional Documents
Culture Documents
Algorithm
Algorithmic solution
EXAMPLE: SORTING
Basic Idea:
To sort a list of numbers
Input:
A sequence of n numbers <a1, a2, …, an>
Output:
A sorted form of the input sequence
<b1, b2, …, bn> such that
bi ≤ bj whenever i < j
EXAMPLE:
SORTING(contd..)
Sorting algorithms:
• Selection sort?
problem • Insertion sort?
• Merge sort?
• …
algorithm
CurrentMax ← A[0]
for i ← 1 to n − 1 do
if A[i] > currentMax then
currentMax ← A[i]
return currentMax
Example Of Pseudocode
A Linear Search Algorithm
Basic Idea:
Traverse the list and compare each element of the list
with the element that we are searching.
Input:
A list of n elements and the element to be searched
Output
Returns Location of the searched element if found
If not found,
returns -1
Pseudo-Code Of Linear Search
Algorithm
Important Points Regarding
Algorithms
• Algorithms should be non-ambiguous
currentMax ← A[0] 1
for i ← 1 to n − 1 do 2n=1+1n+1(n-1)
return currentMax 1
• Best Case
• Lower bound on running time
• Worst Case
• Upper bound on running time
• Average Case
• Expected running time
Running Time
Input size vs Running Time
Generalizing Running Time
Comparing the growth of the running time as the input grows to the growth of known
functions.