-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquick_sort.sh
More file actions
42 lines (38 loc) · 858 Bytes
/
Copy pathquick_sort.sh
File metadata and controls
42 lines (38 loc) · 858 Bytes
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
#!/bin/bash
function quick() {
local list=("$@")
local listLen="${#list[@]}"
local position=0
if (( listLen == 2 ))
then
if (( list[0] > list[1] ))
then
read -r -a list <<<"${list[1]} ${list[0]}"
fi
elif (( listLen > 2 ))
then
for (( i=1; i < listLen; i++ ))
do
if (( list[i] <= list[0] ))
then
(( position++ ))
temp=${list[${i}]}
list[${i}]=${list[${position}]}
list[${position}]=${temp}
fi
done
temp=${list[0]}
list[0]=${list[${position}]}
list[${position}]=${temp}
read -r -a list <<<"$( quick "${list[@]:0:((position))}") ${list[${position}]} $(quick "${list[@]:((position + 1)):((listLen - position))}")"
fi
echo "${list[@]}"
}
if [[ $# -eq 0 ]]
then
echo -n "Enter list of numbers: "
read -r -a array
else
array=("$@")
fi
printf "%s sorted is %s\n" "${array[*]}" "$(quick "${array[@]}")"