Debian Codegraph @

bubble.sh

raw symbols SCIP
1 #!/bin/bash
2 # bubble.sh: Bubble sort, of sorts.
3
4 # Recall the algorithm for a bubble sort. In this particular version...
5
6 # With each successive pass through the array to be sorted,
7 #+ compare two adjacent elements, and swap them if out of order.
8 # At the end of the first pass, the "heaviest" element has sunk to bottom.
9 # At the end of the second pass, the next "heaviest" one has sunk next to bottom.
10 # And so forth.
11 # This means that each successive pass needs to traverse less of the array.
12 # You will therefore notice a speeding up in the printing of the later passes.
13
14
15 exchange()
16 {
17 # Swaps two members of the array.
18 local temp=${Countries[$1]} # Temporary storage
19 #+ for element getting swapped out.
20 Countries[$1]=${Countries[$2]}
21 Countries[$2]=$temp
22
23 return
24 }
25
26 declare -a Countries # Declare array,
27 #+ optional here since it's initialized below.
28
29 # Is it permissable to split an array variable over multiple lines
30 #+ using an escape (\)?
31 # Yes.
32
33 Countries=(Netherlands Ukraine Zaire Turkey Russia Yemen Syria \
34 Brazil Argentina Nicaragua Japan Mexico Venezuela Greece England \
35 Israel Peru Canada Oman Denmark Wales France Kenya \
36 Xanadu Qatar Liechtenstein Hungary)
37
38 # "Xanadu" is the mythical place where, according to Coleridge,
39 #+ Kubla Khan did a pleasure dome decree.
40
41
42 clear # Clear the screen to start with.
43
44 echo "0: ${Countries[*]}" # List entire array at pass 0.
45
46 number_of_elements=${#Countries[@]}
47 let "comparisons = $number_of_elements - 1"
48
49 count=1 # Pass number.
50
51 while [ "$comparisons" -gt 0 ] # Beginning of outer loop
52 do
53
54 index=0 # Reset index to start of array after each pass.
55
56 while [ "$index" -lt "$comparisons" ] # Beginning of inner loop
57 do
58 if [ ${Countries[$index]} \> ${Countries[`expr $index + 1`]} ]
59 # If out of order...
60 # Recalling that \> is ASCII comparison operator
61 #+ within single brackets.
62
63 # if [[ ${Countries[$index]} > ${Countries[`expr $index + 1`]} ]]
64 #+ also works.
65 then
66 exchange $index `expr $index + 1` # Swap.
67 fi
68 let "index += 1" # Or, index+=1 on Bash, ver. 3.1 or newer.
69 done # End of inner loop
70
71 # ----------------------------------------------------------------------
72 # Paulo Marcel Coelho Aragao suggests for-loops as a simpler altenative.
73 #
74 # for (( last = $number_of_elements - 1 ; last > 0 ; last-- ))
75 ## Fix by C.Y. Hunt ^ (Thanks!)
76 # do
77 # for (( i = 0 ; i < last ; i++ ))
78 # do
79 # [[ "${Countries[$i]}" > "${Countries[$((i+1))]}" ]] \
80 # && exchange $i $((i+1))
81 # done
82 # done
83 # ----------------------------------------------------------------------
84
85
86 let "comparisons -= 1" # Since "heaviest" element bubbles to bottom,
87 #+ we need do one less comparison each pass.
88
89 echo
90 echo "$count: ${Countries[@]}" # Print resultant array at end of each pass.
91 echo
92 let "count += 1" # Increment pass count.
93
94 done # End of outer loop
95 # All done.
96
97 exit 0