-
-
Notifications
You must be signed in to change notification settings - Fork 359
Added bubblesort in bash #509
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added bubblesort in bash #509
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good. I also noticed some spacing/tab issues.
arr=("$@") | ||
(( len = ${#arr[@]} )) | ||
|
||
for i in $(seq 0 $(( len - 1 ))); do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't shell out when you can use a built-in:
for ((i = 0; i <= len - 1; i++)); do
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah ok
I just want to say that I really appreciate the bash submissions and reviews, I'm learning a lot and it makes me want to learn proper bash instead the usual SO copy/paste that I hate ^^ |
I don't think this is how one would typically write bash. #!/usr/bin/env bash
bubble_sort() {
local i
local j
local tmp
local len
local arr
arr=("$@")
len=${#arr[@]}
for i in $(seq 0 $(calc -e $len-1)); do
for j in $(seq 0 $(calc -e $len-2)); do
if [ ${arr[j]} -gt ${arr[j+1]} ]; then
tmp=${arr[j+1]}
arr[j+1]=${arr[j]}
arr[j]=$tmp
fi
done
done
echo ${arr[*]}
}
arr=(1 45 756 4569 56 3 8 5 -10 -4)
echo "Unsorted array: ${arr[*]}"
tmp=$(bubble_sort "${arr[@]}")
echo "Sorted array: ${tmp[*]}"```
This is how i would write bubblesort in bash |
Hmm I was refering to this pull request for which one to choose |
@Liikt It's not how one would normally write bash, because we have the bad habit of shelling out ( If this were pure |
@Ariana1729 I'll look over this again tonight my time. |
I mean sure. I just have never seen even bash written like that. But then again I can't say i have seen that much bash at all. I personally still prefer my style but I can agree that it is more sh than bash. What I found is that the current code looks a bit like lisp :D |
I don't have any problems with the array indexing, as long as its safe. I agree that it looks like Lisp, which is weird considering how different the two things are 😃 |
More bash algos!
these look pretty crippled tho not sure how to make it clearer tbh