Bidirectional bubble sort using goto











up vote
0
down vote

favorite












I am aware of Dijkstra's paper on why goto is harmful, but I thought it would still be fun to make a bidirectional bubble sort using goto. So I did. I thought I'd share the code here. Cheers!



void bubbleSort(int arr, int size){
int counter = 0;
BottomUp: //This starts with the lowest array index and goes up to the highest
if(size == 1)
return;
counter += 1;
if(arr[counter] < arr[counter - 1])
swap(arr[counter], arr[counter - 1]);

if(counter < size - 1)
goto BottomUp;
counter += 1;
TopDown: //This starts from the highest array index and goes back down to the lowest
counter -= 1;
if(arr[counter] < arr[counter - 1])
swap(arr[counter], arr[counter - 1]);

if(counter > 1)
goto TopDown;
else{
counter = 0;
size -= 1;
goto BottomUp;
}
}









share|improve this question




















  • 2




    Have you read Knuth's work on how goto can be more elegant and efficient in certain contexts? I guess we'll find out how elegant your use of gotos is...
    – Graham
    12 hours ago












  • This can easily be rewritten to nested loops: for(;;) { BottomUp: do { ... } while (counter >= size - 1); counter += 1; TopDown: do { .. } while (counter <= 1); conter = 0; size -= 1; }. I am not against using goto if you really need them and I hate warnings from C++ compilers about passing variable initialization I am not using after the label, which makes using goto even harder for no real reason (other than stupidity of compiler throwing unnecessary warnings at me), but this is not good usage of goto.
    – firda
    12 hours ago






  • 3




    HAve you written the same algorthim without using goto. Then compared them to see which is more readable?
    – Martin York
    10 hours ago










  • @Martin York -- no, I just programmed this for fun.
    – TheMachoMuchacho
    10 hours ago






  • 2




    @firda That should be an answer, not a comment.
    – Zeta
    9 hours ago















up vote
0
down vote

favorite












I am aware of Dijkstra's paper on why goto is harmful, but I thought it would still be fun to make a bidirectional bubble sort using goto. So I did. I thought I'd share the code here. Cheers!



void bubbleSort(int arr, int size){
int counter = 0;
BottomUp: //This starts with the lowest array index and goes up to the highest
if(size == 1)
return;
counter += 1;
if(arr[counter] < arr[counter - 1])
swap(arr[counter], arr[counter - 1]);

if(counter < size - 1)
goto BottomUp;
counter += 1;
TopDown: //This starts from the highest array index and goes back down to the lowest
counter -= 1;
if(arr[counter] < arr[counter - 1])
swap(arr[counter], arr[counter - 1]);

if(counter > 1)
goto TopDown;
else{
counter = 0;
size -= 1;
goto BottomUp;
}
}









share|improve this question




















  • 2




    Have you read Knuth's work on how goto can be more elegant and efficient in certain contexts? I guess we'll find out how elegant your use of gotos is...
    – Graham
    12 hours ago












  • This can easily be rewritten to nested loops: for(;;) { BottomUp: do { ... } while (counter >= size - 1); counter += 1; TopDown: do { .. } while (counter <= 1); conter = 0; size -= 1; }. I am not against using goto if you really need them and I hate warnings from C++ compilers about passing variable initialization I am not using after the label, which makes using goto even harder for no real reason (other than stupidity of compiler throwing unnecessary warnings at me), but this is not good usage of goto.
    – firda
    12 hours ago






  • 3




    HAve you written the same algorthim without using goto. Then compared them to see which is more readable?
    – Martin York
    10 hours ago










  • @Martin York -- no, I just programmed this for fun.
    – TheMachoMuchacho
    10 hours ago






  • 2




    @firda That should be an answer, not a comment.
    – Zeta
    9 hours ago













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I am aware of Dijkstra's paper on why goto is harmful, but I thought it would still be fun to make a bidirectional bubble sort using goto. So I did. I thought I'd share the code here. Cheers!



void bubbleSort(int arr, int size){
int counter = 0;
BottomUp: //This starts with the lowest array index and goes up to the highest
if(size == 1)
return;
counter += 1;
if(arr[counter] < arr[counter - 1])
swap(arr[counter], arr[counter - 1]);

if(counter < size - 1)
goto BottomUp;
counter += 1;
TopDown: //This starts from the highest array index and goes back down to the lowest
counter -= 1;
if(arr[counter] < arr[counter - 1])
swap(arr[counter], arr[counter - 1]);

if(counter > 1)
goto TopDown;
else{
counter = 0;
size -= 1;
goto BottomUp;
}
}









share|improve this question















I am aware of Dijkstra's paper on why goto is harmful, but I thought it would still be fun to make a bidirectional bubble sort using goto. So I did. I thought I'd share the code here. Cheers!



void bubbleSort(int arr, int size){
int counter = 0;
BottomUp: //This starts with the lowest array index and goes up to the highest
if(size == 1)
return;
counter += 1;
if(arr[counter] < arr[counter - 1])
swap(arr[counter], arr[counter - 1]);

if(counter < size - 1)
goto BottomUp;
counter += 1;
TopDown: //This starts from the highest array index and goes back down to the lowest
counter -= 1;
if(arr[counter] < arr[counter - 1])
swap(arr[counter], arr[counter - 1]);

if(counter > 1)
goto TopDown;
else{
counter = 0;
size -= 1;
goto BottomUp;
}
}






c++ array sorting






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 12 hours ago









Solomon Ucko

899313




899313










asked 13 hours ago









TheMachoMuchacho

265




265








  • 2




    Have you read Knuth's work on how goto can be more elegant and efficient in certain contexts? I guess we'll find out how elegant your use of gotos is...
    – Graham
    12 hours ago












  • This can easily be rewritten to nested loops: for(;;) { BottomUp: do { ... } while (counter >= size - 1); counter += 1; TopDown: do { .. } while (counter <= 1); conter = 0; size -= 1; }. I am not against using goto if you really need them and I hate warnings from C++ compilers about passing variable initialization I am not using after the label, which makes using goto even harder for no real reason (other than stupidity of compiler throwing unnecessary warnings at me), but this is not good usage of goto.
    – firda
    12 hours ago






  • 3




    HAve you written the same algorthim without using goto. Then compared them to see which is more readable?
    – Martin York
    10 hours ago










  • @Martin York -- no, I just programmed this for fun.
    – TheMachoMuchacho
    10 hours ago






  • 2




    @firda That should be an answer, not a comment.
    – Zeta
    9 hours ago














  • 2




    Have you read Knuth's work on how goto can be more elegant and efficient in certain contexts? I guess we'll find out how elegant your use of gotos is...
    – Graham
    12 hours ago












  • This can easily be rewritten to nested loops: for(;;) { BottomUp: do { ... } while (counter >= size - 1); counter += 1; TopDown: do { .. } while (counter <= 1); conter = 0; size -= 1; }. I am not against using goto if you really need them and I hate warnings from C++ compilers about passing variable initialization I am not using after the label, which makes using goto even harder for no real reason (other than stupidity of compiler throwing unnecessary warnings at me), but this is not good usage of goto.
    – firda
    12 hours ago






  • 3




    HAve you written the same algorthim without using goto. Then compared them to see which is more readable?
    – Martin York
    10 hours ago










  • @Martin York -- no, I just programmed this for fun.
    – TheMachoMuchacho
    10 hours ago






  • 2




    @firda That should be an answer, not a comment.
    – Zeta
    9 hours ago








2




2




Have you read Knuth's work on how goto can be more elegant and efficient in certain contexts? I guess we'll find out how elegant your use of gotos is...
– Graham
12 hours ago






Have you read Knuth's work on how goto can be more elegant and efficient in certain contexts? I guess we'll find out how elegant your use of gotos is...
– Graham
12 hours ago














This can easily be rewritten to nested loops: for(;;) { BottomUp: do { ... } while (counter >= size - 1); counter += 1; TopDown: do { .. } while (counter <= 1); conter = 0; size -= 1; }. I am not against using goto if you really need them and I hate warnings from C++ compilers about passing variable initialization I am not using after the label, which makes using goto even harder for no real reason (other than stupidity of compiler throwing unnecessary warnings at me), but this is not good usage of goto.
– firda
12 hours ago




This can easily be rewritten to nested loops: for(;;) { BottomUp: do { ... } while (counter >= size - 1); counter += 1; TopDown: do { .. } while (counter <= 1); conter = 0; size -= 1; }. I am not against using goto if you really need them and I hate warnings from C++ compilers about passing variable initialization I am not using after the label, which makes using goto even harder for no real reason (other than stupidity of compiler throwing unnecessary warnings at me), but this is not good usage of goto.
– firda
12 hours ago




3




3




HAve you written the same algorthim without using goto. Then compared them to see which is more readable?
– Martin York
10 hours ago




HAve you written the same algorthim without using goto. Then compared them to see which is more readable?
– Martin York
10 hours ago












@Martin York -- no, I just programmed this for fun.
– TheMachoMuchacho
10 hours ago




@Martin York -- no, I just programmed this for fun.
– TheMachoMuchacho
10 hours ago




2




2




@firda That should be an answer, not a comment.
– Zeta
9 hours ago




@firda That should be an answer, not a comment.
– Zeta
9 hours ago










1 Answer
1






active

oldest

votes

















up vote
0
down vote













Other than the elephant in the room (this isn't a justified use of goto), I have some other nitpicks:




  • I'm uncomfortable passing an array as an argument. A pointer - sure; a reference to an array - sure. Since you're in C++ I'd go with the reference-to-array.

  • The various += 1 and -= 1 can simply be ++ and --.

  • Your else isn't necessary. You've already jumped prior to that line.






share|improve this answer





















    Your Answer





    StackExchange.ifUsing("editor", function () {
    return StackExchange.using("mathjaxEditing", function () {
    StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
    StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
    });
    });
    }, "mathjax-editing");

    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "196"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f207827%2fbidirectional-bubble-sort-using-goto%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    Other than the elephant in the room (this isn't a justified use of goto), I have some other nitpicks:




    • I'm uncomfortable passing an array as an argument. A pointer - sure; a reference to an array - sure. Since you're in C++ I'd go with the reference-to-array.

    • The various += 1 and -= 1 can simply be ++ and --.

    • Your else isn't necessary. You've already jumped prior to that line.






    share|improve this answer

























      up vote
      0
      down vote













      Other than the elephant in the room (this isn't a justified use of goto), I have some other nitpicks:




      • I'm uncomfortable passing an array as an argument. A pointer - sure; a reference to an array - sure. Since you're in C++ I'd go with the reference-to-array.

      • The various += 1 and -= 1 can simply be ++ and --.

      • Your else isn't necessary. You've already jumped prior to that line.






      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        Other than the elephant in the room (this isn't a justified use of goto), I have some other nitpicks:




        • I'm uncomfortable passing an array as an argument. A pointer - sure; a reference to an array - sure. Since you're in C++ I'd go with the reference-to-array.

        • The various += 1 and -= 1 can simply be ++ and --.

        • Your else isn't necessary. You've already jumped prior to that line.






        share|improve this answer












        Other than the elephant in the room (this isn't a justified use of goto), I have some other nitpicks:




        • I'm uncomfortable passing an array as an argument. A pointer - sure; a reference to an array - sure. Since you're in C++ I'd go with the reference-to-array.

        • The various += 1 and -= 1 can simply be ++ and --.

        • Your else isn't necessary. You've already jumped prior to that line.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 44 mins ago









        Reinderien

        932415




        932415






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f207827%2fbidirectional-bubble-sort-using-goto%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Ottavio Pratesi

            Tricia Helfer

            15 giugno