Skip to content

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

Merged
merged 3 commits into from
Oct 17, 2018

Conversation

Ariana1729
Copy link
Contributor

More bash algos!

(( tmp = arr[(( j + 1 ))] ))
(( arr[(( j + 1 ))] = arr[j] ))

these look pretty crippled tho not sure how to make it clearer tbh

@berquist berquist self-assigned this Oct 13, 2018
@berquist berquist added Implementation This provides an implementation for an algorithm. (Code and maybe md files are edited.) Hacktoberfest The label for all Hacktoberfest related things! labels Oct 13, 2018
Copy link
Member

@berquist berquist left a 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
Copy link
Member

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah ok

@Butt4cak3 Butt4cak3 mentioned this pull request Oct 14, 2018
@jiegillet
Copy link
Member

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 ^^

@Liikt
Copy link

Liikt commented Oct 15, 2018

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

@Ariana1729
Copy link
Contributor Author

Hmm I was refering to this pull request for which one to choose

@berquist
Copy link
Member

@Liikt It's not how one would normally write bash, because we have the bad habit of shelling out (seq, calc) when bash internals should be used instead. Using [...] is also bad; that's the original Bourne shell, not the Bourne-again shell.

If this were pure sh, then yes, you would need to call out to other programs.

@berquist
Copy link
Member

@Ariana1729 I'll look over this again tonight my time.

@Liikt
Copy link

Liikt commented Oct 15, 2018

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

@berquist
Copy link
Member

berquist commented Oct 17, 2018

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 😃

@berquist berquist merged commit 9133a55 into algorithm-archivists:master Oct 17, 2018
@Ariana1729 Ariana1729 deleted the bubblesort_sh branch October 19, 2018 12:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Hacktoberfest The label for all Hacktoberfest related things! Implementation This provides an implementation for an algorithm. (Code and maybe md files are edited.)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants