Bidirectional bubble sort using goto [on hold]











up vote
-4
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















put on hold as off-topic by Incomputable, Martin York, Snowhawk, 200_success, πάντα ῥεῖ Nov 17 at 23:17


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." – Incomputable, Martin York, Snowhawk, πάντα ῥεῖ

If this question can be reworded to fit the rules in the help center, please edit the 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
    Nov 16 at 19:44








  • 3




    HAve you written the same algorthim without using goto. Then compared them to see which is more readable?
    – Martin York
    Nov 16 at 21:52






  • 2




    @firda That should be an answer, not a comment.
    – Zeta
    Nov 16 at 22:47






  • 1




    goto is a tool that easily can be abused. Still there are quite a few useful use cases, e. g. exiting quickly nested loops. Sometimes, it can be useful for optimising some performance critical hotspots (before you start writing assembler code instead). It is just like an axe: you can kill people with, but you can chop the wood for your next barbecue as well...
    – Aconcagua
    Nov 17 at 1:21






  • 1




    I'm voting to close this question as off-topic because you are deliberately using goto, which violates the "Do you want this to be good code?" rule in the help center.
    – 200_success
    Nov 17 at 23:01















up vote
-4
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















put on hold as off-topic by Incomputable, Martin York, Snowhawk, 200_success, πάντα ῥεῖ Nov 17 at 23:17


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." – Incomputable, Martin York, Snowhawk, πάντα ῥεῖ

If this question can be reworded to fit the rules in the help center, please edit the 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
    Nov 16 at 19:44








  • 3




    HAve you written the same algorthim without using goto. Then compared them to see which is more readable?
    – Martin York
    Nov 16 at 21:52






  • 2




    @firda That should be an answer, not a comment.
    – Zeta
    Nov 16 at 22:47






  • 1




    goto is a tool that easily can be abused. Still there are quite a few useful use cases, e. g. exiting quickly nested loops. Sometimes, it can be useful for optimising some performance critical hotspots (before you start writing assembler code instead). It is just like an axe: you can kill people with, but you can chop the wood for your next barbecue as well...
    – Aconcagua
    Nov 17 at 1:21






  • 1




    I'm voting to close this question as off-topic because you are deliberately using goto, which violates the "Do you want this to be good code?" rule in the help center.
    – 200_success
    Nov 17 at 23:01













up vote
-4
down vote

favorite









up vote
-4
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 Nov 16 at 19:37









Solomon Ucko

914313




914313










asked Nov 16 at 18:37









TheMachoMuchacho

1185




1185




put on hold as off-topic by Incomputable, Martin York, Snowhawk, 200_success, πάντα ῥεῖ Nov 17 at 23:17


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." – Incomputable, Martin York, Snowhawk, πάντα ῥεῖ

If this question can be reworded to fit the rules in the help center, please edit the question.




put on hold as off-topic by Incomputable, Martin York, Snowhawk, 200_success, πάντα ῥεῖ Nov 17 at 23:17


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." – Incomputable, Martin York, Snowhawk, πάντα ῥεῖ

If this question can be reworded to fit the rules in the help center, please edit the 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
    Nov 16 at 19:44








  • 3




    HAve you written the same algorthim without using goto. Then compared them to see which is more readable?
    – Martin York
    Nov 16 at 21:52






  • 2




    @firda That should be an answer, not a comment.
    – Zeta
    Nov 16 at 22:47






  • 1




    goto is a tool that easily can be abused. Still there are quite a few useful use cases, e. g. exiting quickly nested loops. Sometimes, it can be useful for optimising some performance critical hotspots (before you start writing assembler code instead). It is just like an axe: you can kill people with, but you can chop the wood for your next barbecue as well...
    – Aconcagua
    Nov 17 at 1:21






  • 1




    I'm voting to close this question as off-topic because you are deliberately using goto, which violates the "Do you want this to be good code?" rule in the help center.
    – 200_success
    Nov 17 at 23:01














  • 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
    Nov 16 at 19:44








  • 3




    HAve you written the same algorthim without using goto. Then compared them to see which is more readable?
    – Martin York
    Nov 16 at 21:52






  • 2




    @firda That should be an answer, not a comment.
    – Zeta
    Nov 16 at 22:47






  • 1




    goto is a tool that easily can be abused. Still there are quite a few useful use cases, e. g. exiting quickly nested loops. Sometimes, it can be useful for optimising some performance critical hotspots (before you start writing assembler code instead). It is just like an axe: you can kill people with, but you can chop the wood for your next barbecue as well...
    – Aconcagua
    Nov 17 at 1:21






  • 1




    I'm voting to close this question as off-topic because you are deliberately using goto, which violates the "Do you want this to be good code?" rule in the help center.
    – 200_success
    Nov 17 at 23:01








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
Nov 16 at 19:44






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
Nov 16 at 19:44






3




3




HAve you written the same algorthim without using goto. Then compared them to see which is more readable?
– Martin York
Nov 16 at 21:52




HAve you written the same algorthim without using goto. Then compared them to see which is more readable?
– Martin York
Nov 16 at 21:52




2




2




@firda That should be an answer, not a comment.
– Zeta
Nov 16 at 22:47




@firda That should be an answer, not a comment.
– Zeta
Nov 16 at 22:47




1




1




goto is a tool that easily can be abused. Still there are quite a few useful use cases, e. g. exiting quickly nested loops. Sometimes, it can be useful for optimising some performance critical hotspots (before you start writing assembler code instead). It is just like an axe: you can kill people with, but you can chop the wood for your next barbecue as well...
– Aconcagua
Nov 17 at 1:21




goto is a tool that easily can be abused. Still there are quite a few useful use cases, e. g. exiting quickly nested loops. Sometimes, it can be useful for optimising some performance critical hotspots (before you start writing assembler code instead). It is just like an axe: you can kill people with, but you can chop the wood for your next barbecue as well...
– Aconcagua
Nov 17 at 1:21




1




1




I'm voting to close this question as off-topic because you are deliberately using goto, which violates the "Do you want this to be good code?" rule in the help center.
– 200_success
Nov 17 at 23:01




I'm voting to close this question as off-topic because you are deliberately using goto, which violates the "Do you want this to be good code?" rule in the help center.
– 200_success
Nov 17 at 23:01










1 Answer
1






active

oldest

votes

















up vote
1
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




























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    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
      1
      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
        1
        down vote










        up vote
        1
        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 Nov 17 at 7:32









        Reinderien

        1,175515




        1,175515















            Popular posts from this blog

            Morgemoulin

            Scott Moir

            Souastre