Unit 1 Algorithms: Efficiency, Analysis, and Order

Class Code: AMC621 Instructor: Huilan Chang

◼️ Algorithm


Goal: learn techniques for solving problems using a computer.

🔹 ****Search problem

Problem: Is the key $x$ in the array $S$ of $n$ keys? Inputs (parameters): array of keys $S$ indexed from 0 to $n-1$, and a key $x$. Output: a location of $x$ in $S$ (-1 if $x$ is not in $S$).

Pseudocode for sequential search Search (list $S$, key $x$) $l=0$; while ( $l≤n-1$ and $S[l]≠x$) $l++$; if ($l\geq n$) $l=-1$; return $l$

Programming (Python) Sequential search: https://colab.research.google.com/drive/1h8773WRFhHTSIxCotcn5co-MhKigFymg?usp=sharing

Note. In Pseudocode, we can write “exchange $x$ and $y$” rather than “temp$=x$; $x=y$; $y=$ temp”.

▪️Search problem on sorted array

Problem: Is the key $x$ in the sorted arrays of $n$ keys Inputs (parameters): sorted array $S[0 .. n-1]$ and a key $x$. Outputs: a location of $x$ in $S$ ( “not found” if $x$ is not in $S$)

Algorithm: Binary Search Example. $n=8$, $x=5$, $S=[1,3,4,6,7,9,11,13]$. Initially low $=0$, high $=7$. The possible positions of $x$: low .. highstepmidcomparisonpossible positions of $x$initial$0..7$1$\lfloor\frac{0+7}{2}\rfloor=3$$S[3]=6>x$$0..2$ (high $=$ mid$-1$)2$\lfloor\frac{0+2}{2}\rfloor=1$$S[1]=3<x$$2..2$ (low $=$ mid$+1$)3$\lfloor\frac{2+2}{2}\rfloor=2$$S[2]=4<x$$3..2$ (low $=$ mid$+1$)4low $=3>$ high $=2$return “not found”

▪️Two versions of pseudocode for binary search (兩種寫法):

Iterative version (迭代版) https://colab.research.google.com/drive/1G7J_C0H91HhjXIKne5wwtLzp7U1joI2E?usp=sharing BinarySearch($S[0 .. n-1]$, value)

low = 0 high = n-1 while (low <= high) mid = ⌊(low + high)/2⌋ if (S[mid] > value) high = mid - 1 else if (S[mid] < value) low = mid + 1 else return mid return "not found"