Searchin & Sorting Pseudo Codes

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

ICS COURSE PREPARATION

1. Linear/Sequen-al Search

func-on linearSearch(array, target)


for each item in array
if item equals target
return the index of item
return not found

2. Selec-on Sort

func-on selec-onSort(array)
for i from 0 to length of array - 1
set minIndex to i
for j from i + 1 to length of array
if array[j] < array[minIndex]
set minIndex to j
swap array[i] with array[minIndex]

3. Binary Search

func-on binarySearch(array, target)


set low to 0
set high to length of array - 1

while low <= high


set mid to (low + high) / 2
if array[mid] equals target
return mid
else if array[mid] < target
set low to mid + 1
else
set high to mid - 1

return not found

4. External Merge Sort

func-on externalMergeSort(file)
divide file into smaller sub-files
for each sub-file
sort using any internal sor-ng method
merge sorted sub-files into one large file

5. Hashing

func-on hashing(key, hashSize)


hash = hashFunc-on(key)
return hash % hashSize

6. Quick Sort

func-on quickSort(array, low, high)


if low < high
set pivotIndex to par--on(array, low, high)
quickSort(array, low, pivotIndex - 1)
quickSort(array, pivotIndex + 1, high)

func-on par--on(array, low, high)


set pivot to array[high]
set i to low - 1
for j from low to high - 1
if array[j] <= pivot
increment i
swap array[i] with array[j]
swap array[i + 1] with array[high]
return i + 1

You might also like