Skip to content

Commit

Permalink
updating quick sort for bash to comply with test runs
Browse files Browse the repository at this point in the history
  • Loading branch information
tehtbl committed Oct 14, 2019
1 parent 00772b1 commit 6b6f904
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions archive/b/bash/quick-sort.sh
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,19 @@ qsort_iter() {
# compare function
compare_str() { [[ $1 < $2 ]]; }

# main
array=( "$@" )
ERROR="Usage: please provide a list of at least two integers to sort in the format \"1, 2, 3, 4, 5\""

#Validation to fit criteria
if [ "$#" != "1" ]; then echo $ERROR; exit 1; fi; #wrong input
if [[ ! "$1" =~ "," ]]; then echo $ERROR; exit 1; fi; #wrong format

array=($(echo $@ | tr ", " " "))

if [ "${array[0]}" == "" ]; then echo $ERROR; exit 1; fi; #empty input
if [ "${#array[@]}" == "1" ]; then echo $ERROR; exit 1; fi; #not a list

qsort_iter compare_str "${array[@]}"
echo "${qsort_ret[@]}"
arrayString=${qsort_ret[@]}
echo "${arrayString// /, }"

# example run:
# quicksort.sh 5 4 3 2 1 4 4
exit 0

0 comments on commit 6b6f904

Please sign in to comment.