OILS / benchmarks / compute / bubble_sort.sh View on Github | oilshell.org

55 lines, 38 significant
1#!/usr/bin/env bash
2#
3# Usage:
4# ./bubble_sort.sh <function name>
5
6set -o nounset
7set -o pipefail
8set -o errexit
9
10# Fix the lexicographical comparisons!!!
11LANG=C
12
13main() {
14 mapfile seq
15
16 #echo ${#seq[@]}
17
18 if test "$1" = 'int'; then
19 # Sort by integer value
20 local changed=T
21 while test -n "$changed"; do
22 changed=''
23 for (( i = 0; i < ${#seq[@]} - 1; ++i )); do
24 if (( seq[i] > seq[i+1] )); then
25 tmp=${seq[i+1]}
26 seq[i+1]=${seq[i]}
27 seq[i]=$tmp
28 changed=T
29 fi
30 done
31 done
32
33 else
34 # Sort by bytes
35 local changed=T
36 while test -n "$changed"; do
37 changed=''
38 for (( i = 0; i < ${#seq[@]} - 1; ++i )); do
39 # LANG=C required here
40 if [[ ${seq[i]} > ${seq[i+1]} ]]; then
41 tmp=${seq[i+1]}
42 seq[i+1]=${seq[i]}
43 seq[i]=$tmp
44 changed=T
45 fi
46 done
47 done
48 fi
49
50 for line in "${seq[@]}"; do
51 echo -n "$line"
52 done
53}
54
55main "$@"