Skip to main content
Code Review

Return to Answer

replaced http://meta.codereview.stackexchange.com/ with https://codereview.meta.stackexchange.com/
Source Link

The book's implementation

Normally, asking for a review of code written by others is off-topic review of code written by others is off-topic for Code Review. However, given the simplicity of the code, the context of your question (requesting a comparison with your own working implementation), and the fact that it's a book that you're learning from, I feel compelled to comment.

I think that your version is much better! Your book seems to be promoting some questionable programming practices.

  • Inconsistent whitespace: The indentation widths are not the same. Also, the if condition has arr[i+1], which is written as arr[i + 1] in the statements below.
  • Misleading for loop: The loop header makes it look like a simple loop in which i is incremented by i with each iteration. That's a lie! In fact, i gets reset to -1 whenever a swap occurs. What looks like a simple algorithm that makes one linear pass through the array is actually an O(SIZE2) algorithm.
  • Magic number: What is the significance of -1? It's a roundabout way to reset i to 0, once the i++ in the loop's epilogue is taken into account.
  • Weird loop limit: The last iteration through the loop, when i = SIZE - 1, is guaranteed to have no effect, since if ((i < SIZE - 1) && ...) will always fail.

A less deceptive way to express the book's algorithm would be:

 int i;
 do
 {
 for (i = 0; i < SIZE - 1; i++)
 {
 if (arr[i] > arr[i + 1])
 {
 int swap = arr[i];
 arr[i] = arr[i + 1];
 arr[i + 1] = swap;
 break; /* Start over at i = 0 */
 }
 }
 } while (i < SIZE - 1);

The book's implementation

Normally, asking for a review of code written by others is off-topic for Code Review. However, given the simplicity of the code, the context of your question (requesting a comparison with your own working implementation), and the fact that it's a book that you're learning from, I feel compelled to comment.

I think that your version is much better! Your book seems to be promoting some questionable programming practices.

  • Inconsistent whitespace: The indentation widths are not the same. Also, the if condition has arr[i+1], which is written as arr[i + 1] in the statements below.
  • Misleading for loop: The loop header makes it look like a simple loop in which i is incremented by i with each iteration. That's a lie! In fact, i gets reset to -1 whenever a swap occurs. What looks like a simple algorithm that makes one linear pass through the array is actually an O(SIZE2) algorithm.
  • Magic number: What is the significance of -1? It's a roundabout way to reset i to 0, once the i++ in the loop's epilogue is taken into account.
  • Weird loop limit: The last iteration through the loop, when i = SIZE - 1, is guaranteed to have no effect, since if ((i < SIZE - 1) && ...) will always fail.

A less deceptive way to express the book's algorithm would be:

 int i;
 do
 {
 for (i = 0; i < SIZE - 1; i++)
 {
 if (arr[i] > arr[i + 1])
 {
 int swap = arr[i];
 arr[i] = arr[i + 1];
 arr[i + 1] = swap;
 break; /* Start over at i = 0 */
 }
 }
 } while (i < SIZE - 1);

The book's implementation

Normally, asking for a review of code written by others is off-topic for Code Review. However, given the simplicity of the code, the context of your question (requesting a comparison with your own working implementation), and the fact that it's a book that you're learning from, I feel compelled to comment.

I think that your version is much better! Your book seems to be promoting some questionable programming practices.

  • Inconsistent whitespace: The indentation widths are not the same. Also, the if condition has arr[i+1], which is written as arr[i + 1] in the statements below.
  • Misleading for loop: The loop header makes it look like a simple loop in which i is incremented by i with each iteration. That's a lie! In fact, i gets reset to -1 whenever a swap occurs. What looks like a simple algorithm that makes one linear pass through the array is actually an O(SIZE2) algorithm.
  • Magic number: What is the significance of -1? It's a roundabout way to reset i to 0, once the i++ in the loop's epilogue is taken into account.
  • Weird loop limit: The last iteration through the loop, when i = SIZE - 1, is guaranteed to have no effect, since if ((i < SIZE - 1) && ...) will always fail.

A less deceptive way to express the book's algorithm would be:

 int i;
 do
 {
 for (i = 0; i < SIZE - 1; i++)
 {
 if (arr[i] > arr[i + 1])
 {
 int swap = arr[i];
 arr[i] = arr[i + 1];
 arr[i + 1] = swap;
 break; /* Start over at i = 0 */
 }
 }
 } while (i < SIZE - 1);
added 49 characters in body
Source Link
200_success
  • 145.6k
  • 22
  • 190
  • 479

The book's implementation

Normally, asking for a review of code written by others is off-topic for Code Review. However, given the simplicity of the code, the context of your question (requesting a comparison with your own working implementation), and the fact that it's a book that you're learning from, I feel compelled to comment.

YourI think that your version is much better! Your book seems to be promoting some questionable programming practices.

  • Inconsistent whitespace: The indentation widths are not the same. The Also, the if condition has arr[i+1], which is written as arr[i + 1] in the statements below.
  • Misleading for loop: The loop header makes it look like a simple loop in which i is incremented by i with each iteration. That's a lie! In fact, i gets reset to -1 whenever a swap occurs. What looks like a simple algorithm that makes one linear pass through the array is actually an O(SIZE2) algorithm.
  • Magic number: What is the significance of -1? It's a roundabout way to reset i to 0, once the i++ in the loop's epilogue is taken into account.
  • Weird loop limit: The last iteration through the loop, when i = SIZE - 1, is guaranteed to have no effect, since if ((i < SIZE - 1) && ...) will always fail.

A less deceptive way to express the book's algorithm would be:

 int i;
 do
 {
 for (i = 0; i < SIZE - 1; i++)
 {
 if (arr[i] > arr[i + 1])
 {
 int swap = arr[i];
 arr[i] = arr[i + 1];
 arr[i + 1] = swap;
 break; /* Start over at i = 0 */
 }
 }
 } while (i < SIZE - 1);

The book's implementation

Normally, asking for a review of code written by others is off-topic for Code Review. However, given the simplicity of the code, the context of your question (requesting a comparison with your own working implementation), and the fact that it's a book that you're learning from, I feel compelled to comment.

Your book seems to be promoting some questionable programming practices.

  • Inconsistent whitespace: The indentation widths are not the same. The if condition has arr[i+1], which is written as arr[i + 1] in the statements below.
  • Misleading for loop: The loop header makes it look like a simple loop in which i is incremented by i with each iteration. That's a lie! In fact, i gets reset to -1 whenever a swap occurs. What looks like a simple algorithm that makes one linear pass through the array is actually an O(SIZE2) algorithm.
  • Magic number: What is the significance of -1? It's a roundabout way to reset i to 0, once the i++ in the loop's epilogue is taken into account.
  • Weird loop limit: The last iteration through the loop, when i = SIZE - 1, is guaranteed to have no effect, since if ((i < SIZE - 1) && ...) will always fail.

A less deceptive way to express the book's algorithm would be:

 int i;
 do
 {
 for (i = 0; i < SIZE - 1; i++)
 {
 if (arr[i] > arr[i + 1])
 {
 int swap = arr[i];
 arr[i] = arr[i + 1];
 arr[i + 1] = swap;
 break; /* Start over at i = 0 */
 }
 }
 } while (i < SIZE - 1);

The book's implementation

Normally, asking for a review of code written by others is off-topic for Code Review. However, given the simplicity of the code, the context of your question (requesting a comparison with your own working implementation), and the fact that it's a book that you're learning from, I feel compelled to comment.

I think that your version is much better! Your book seems to be promoting some questionable programming practices.

  • Inconsistent whitespace: The indentation widths are not the same. Also, the if condition has arr[i+1], which is written as arr[i + 1] in the statements below.
  • Misleading for loop: The loop header makes it look like a simple loop in which i is incremented by i with each iteration. That's a lie! In fact, i gets reset to -1 whenever a swap occurs. What looks like a simple algorithm that makes one linear pass through the array is actually an O(SIZE2) algorithm.
  • Magic number: What is the significance of -1? It's a roundabout way to reset i to 0, once the i++ in the loop's epilogue is taken into account.
  • Weird loop limit: The last iteration through the loop, when i = SIZE - 1, is guaranteed to have no effect, since if ((i < SIZE - 1) && ...) will always fail.

A less deceptive way to express the book's algorithm would be:

 int i;
 do
 {
 for (i = 0; i < SIZE - 1; i++)
 {
 if (arr[i] > arr[i + 1])
 {
 int swap = arr[i];
 arr[i] = arr[i + 1];
 arr[i + 1] = swap;
 break; /* Start over at i = 0 */
 }
 }
 } while (i < SIZE - 1);
Source Link
200_success
  • 145.6k
  • 22
  • 190
  • 479

The book's implementation

Normally, asking for a review of code written by others is off-topic for Code Review. However, given the simplicity of the code, the context of your question (requesting a comparison with your own working implementation), and the fact that it's a book that you're learning from, I feel compelled to comment.

Your book seems to be promoting some questionable programming practices.

  • Inconsistent whitespace: The indentation widths are not the same. The if condition has arr[i+1], which is written as arr[i + 1] in the statements below.
  • Misleading for loop: The loop header makes it look like a simple loop in which i is incremented by i with each iteration. That's a lie! In fact, i gets reset to -1 whenever a swap occurs. What looks like a simple algorithm that makes one linear pass through the array is actually an O(SIZE2) algorithm.
  • Magic number: What is the significance of -1? It's a roundabout way to reset i to 0, once the i++ in the loop's epilogue is taken into account.
  • Weird loop limit: The last iteration through the loop, when i = SIZE - 1, is guaranteed to have no effect, since if ((i < SIZE - 1) && ...) will always fail.

A less deceptive way to express the book's algorithm would be:

 int i;
 do
 {
 for (i = 0; i < SIZE - 1; i++)
 {
 if (arr[i] > arr[i + 1])
 {
 int swap = arr[i];
 arr[i] = arr[i + 1];
 arr[i + 1] = swap;
 break; /* Start over at i = 0 */
 }
 }
 } while (i < SIZE - 1);
Post Made Community Wiki by 200_success
lang-c

AltStyle によって変換されたページ (->オリジナル) /