-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinarySearch.sh
executable file
·68 lines (65 loc) · 1.6 KB
/
binarySearch.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#/bin/bash
# Algorithm 3 from Chapter 3 in the Rosen Discrete Structures textbook 7e.
# Given a file of integers it will load the values into an array and then
# perform a binary search. However, binary search requires the array to be
# sorted. So, this script is bundled with a bubble sort that runs before
# the binary search is performed.
# === INPUT VALIDATION ===
# They need to pass the file name as the second argument. It must contain the
# numbers to be processed, with \n delimiters.
file=$1
goal=$2
if ! [ -e "$file" ]; then
echo "$0: File provided, $file, does not exist!"
exit 1
fi
if [ -z "$2" ]; then
echo "$0: An integer to search for must be provided as the second argument to the script."
exit 1
fi
# === CODE ===
# Read the file into an array
n=0
a=()
while read -r line; do
echo "The next line in the file is ${line}."
a+=("$line")
((n++))
done < "$file"
echo "Done reading file, ${n} lines were read."
# Now do the bubble sort
i=0
while ((i < n)); do
j=0
echo "On iteration ${i}"
while ((j < n - i)); do
echo "${i}: On iteration ${j}"
if ((${a[j]} > ${a[j + 1]})); then
echo "${i}: Need to swap ${a[j]} and ${a[j + 1]}."
temp=${a[j]}
a[j]=${a[j+1]}
a[j + 1]=$temp
fi
((j++))
done
((i++))
done
# Now do the binary search
i=0
j=n
while ((i < j)); do
m="$(( ( i + j ) / 2 ))"
# Bash arithmetic is always integer arithmetic. There is no need for
# a floor() operation here.
echo "$m: Investigating ${a[m]} ..."
if ((goal > ${a[m]})); then
i="$((m + 1))"
else
j=$m
fi
done
if ((goal == ${a[i]})); then
echo "Found $goal at index $i"
else
echo "Cannot find goal ..."
fi