-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path12_sorting.sh
66 lines (58 loc) · 1.33 KB
/
12_sorting.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
<<Documentation
NAME : V. Karthikeyan
DATE : 12.05.2021
DESCRIPTION : Write a script to sort a given number in ascending or descending order.
INPUT : bash 12_sorting.sh -d 1 2 3 4 5 6 9 8
OUTPU : Descending order of array is 9 8 6 5 4 3 2 1
Documentation
#!/bin/bash
if [ $# -eq 0 ] #if no argument is passed print the error msg and exit
then
echo "Please pass the choice"
exit
fi
arr=($@) #assign the arguments into array 'arr'
len=${#arr[*]} #length of the array
opt=${arr[0]} #the option ascending "-a" or descending "-d"
case $opt in
-a)
for i in `seq 1 $len` # "1 $len" to start from numbers leaving opt value
do
for j in `seq $i $len`
do
if [[ ${arr[i]} -gt ${arr[j]} ]] #if arr[i] > arr[j] , swap them
then
temp=${arr[$i]}
arr[$i]=${arr[$j]}
arr[$j]=$temp
fi
done
done
echo -n "Ascending order of array is "
for i in `seq 1 $len`
do
echo -n "${arr[$i]} "
done
echo
;;
-d)
for i in `seq 1 $len`
do
for j in `seq $i $len`
do
if [[ ${arr[i]} -lt ${arr[j]} ]] #if arr[i] < arr[j] ,swap them
then
temp=${arr[$i]}
arr[$i]=${arr[$j]}
arr[$j]=$temp
fi
done
done
echo -n "Descending order of array is "
for i in `seq 1 $len`
do
echo -n "${arr[$i]} "
done
echo
;;
esac