-
Notifications
You must be signed in to change notification settings - Fork 0
/
bubble_sort.sh
executable file
·62 lines (53 loc) · 1.2 KB
/
bubble_sort.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
#!/bin/bash
NBS=$1
TMP_PATH="/tmp"
usage() {
echo "./${0##*/} add 'num_1 num_2 num_3 ...'"
echo "sample ./${0##*/} '10 7 4 3 1' for bubble sort from small to big number"
exit 2
}
bubble_sort() {
local sort_a=$1
local sort_b=""
local a_nb=""
local b_nb=""
local buf=""
sort_b=$((sort_a + 1))
a_nb=$(cat $TMP_PATH/$sort_a)
b_nb=$(cat $TMP_PATH/$sort_b)
if [[ "$a_nb" -gt "$b_nb" ]]; then
buf=$a_nb
echo "$b_nb" > $TMP_PATH/$sort_a
echo "$buf" > $TMP_PATH/$sort_b
fi
#a_nb=$(cat $TMP_PATH/$sort_a)
#b_nb=$(cat $TMP_PATH/$sort_b)
#echo "$TMP_PATH/$sort_a:$a_nb; $TMP_PATH/$sort_b:$b_nb"
}
sort() {
local nbs=$1
local i=1
local sort_nb=1
local sort_max=""
local result=""
for nb in $nbs; do
echo $nb > $TMP_PATH/$i
echo "i:$i $TMP_PATH/$i:$nb"
i=$((i+1))
done
# last one no need i++, so -1
i=$((i-1))
for((sort_nb=1;sort_nb<i;sort_nb++)); do
sort_max=$((i-sort_nb+1))
#echo "sort_max:$sort_max"
for ((sort_n=1;sort_n<sort_max;sort_n++)); do
bubble_sort "$sort_n"
done
done
for((sort_nb=1;sort_nb<=i;sort_nb++)); do
result=$(cat $TMP_PATH/$sort_nb)
echo "$result"
done
}
[[ -n "$NBS" ]] || usage
sort "$NBS"