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;
}
}
c++ array sorting
|
show 1 more comment
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;
}
}
c++ array sorting
2
Have you read Knuth's work on howgotocan be more elegant and efficient in certain contexts? I guess we'll find out how elegant your use ofgotos 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 usinggotoif you really need them and I hate warnings from C++ compilers about passing variable initialization I am not using after the label, which makes usinggotoeven harder for no real reason (other than stupidity of compiler throwing unnecessary warnings at me), but this is not good usage ofgoto.
– 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
|
show 1 more comment
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;
}
}
c++ array sorting
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
c++ array sorting
edited 12 hours ago
Solomon Ucko
899313
899313
asked 13 hours ago
TheMachoMuchacho
265
265
2
Have you read Knuth's work on howgotocan be more elegant and efficient in certain contexts? I guess we'll find out how elegant your use ofgotos 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 usinggotoif you really need them and I hate warnings from C++ compilers about passing variable initialization I am not using after the label, which makes usinggotoeven harder for no real reason (other than stupidity of compiler throwing unnecessary warnings at me), but this is not good usage ofgoto.
– 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
|
show 1 more comment
2
Have you read Knuth's work on howgotocan be more elegant and efficient in certain contexts? I guess we'll find out how elegant your use ofgotos 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 usinggotoif you really need them and I hate warnings from C++ compilers about passing variable initialization I am not using after the label, which makes usinggotoeven harder for no real reason (other than stupidity of compiler throwing unnecessary warnings at me), but this is not good usage ofgoto.
– 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
|
show 1 more comment
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
+= 1and-= 1can simply be++and--. - Your
elseisn't necessary. You've already jumped prior to that line.
add a comment |
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
+= 1and-= 1can simply be++and--. - Your
elseisn't necessary. You've already jumped prior to that line.
add a comment |
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
+= 1and-= 1can simply be++and--. - Your
elseisn't necessary. You've already jumped prior to that line.
add a comment |
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
+= 1and-= 1can simply be++and--. - Your
elseisn't necessary. You've already jumped prior to that line.
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
+= 1and-= 1can simply be++and--. - Your
elseisn't necessary. You've already jumped prior to that line.
answered 44 mins ago
Reinderien
932415
932415
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
2
Have you read Knuth's work on how
gotocan be more elegant and efficient in certain contexts? I guess we'll find out how elegant your use ofgotos 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 usinggotoif you really need them and I hate warnings from C++ compilers about passing variable initialization I am not using after the label, which makes usinggotoeven harder for no real reason (other than stupidity of compiler throwing unnecessary warnings at me), but this is not good usage ofgoto.– 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