LinearSearch(A, key)
- current ← 0
- while current < size and A[current] ≠ key
- current ← current + 1
- return current
Sort(A[], size)
- for current ← 0 to size-2
- smallest ← current
- for search ← current+1 to size-1
- if A[smallest] > A[search]
- smallest ← search
- if current ≠ smallest
- swap(A[current], A[smallest])
$\sum_{i=1}^{n-1}i$ | $ = \frac{(n-1)(n)}{2}$ |
$ = \frac{n^2 - n +1 }{2} $ | |
$ = \frac{1}{2}n^2 - \frac{1}{2}n + \frac{1}{2} $ | |
$ \approx n^2 $ |
BinarySearch(A, low, high, key)
- found ← false
- while low ≤ high and not found
- mid ← ( low + high) / 2
- if A[mid] = key
- found ← true
- else if A[mid] < key
- low ← mid+1
- else
- high ← mid-1
- if not found
- mid ← high+1
- return mid
Step | Items to Search | 1 | 1024 | 2 | 512 | 3 | 256 | 4 | 128 | 5 | 64 | 6 | 32 | 7 | 16 | 8 | 8 | 9 | 4 | 10 | 2 | 11 | 1 |
---|