Skip to main content
Commonmark migration
Source Link

#Small nitpicks

Small nitpicks

  • The for loop can be started from 1, as the iteration with 0 doesn't actually do anything.
  • Prefer to reduce the scope of variables if possible. This allows better reasoning for the compiler/optimizer, and makes the code more readable because the variable declaration is nearer to the actual usage.
  • The check arr[index_sorted] >= 0 in the while loop should probably be index_sorted >= 0u (after all, Element might not be comparable to integers), but this is true in all cases (-1 for an unsigned integer type is equal to that type's maximum value). However, even checking this becomes unnecessary if the first iteration starts at 1.
  • The array elements could possibly be moved to their new locations (this might improve performance in some cases).

template<typename Element>
void insertion_sort(Element arr[], size_t size) {
    for (auto index = 1u; index < size; ++index) {
        auto temp = std::move(arr[index]);
        auto index_sorted = index - 1;
        while (arr[index_sorted] > temp) {
            arr[index_sorted + 1] = std::move(arr[index_sorted--]);
        }
        arr[index_sorted + 1] = std::move(temp);
    }
}

Technically, the size of the array could be deduced automatically by taking a reference (though it has an ugly syntax). However, by taking an explicit size parameter one can sort a partial array, so I'd prefer the flexibility of this approach.

#Small nitpicks

  • The for loop can be started from 1, as the iteration with 0 doesn't actually do anything.
  • Prefer to reduce the scope of variables if possible. This allows better reasoning for the compiler/optimizer, and makes the code more readable because the variable declaration is nearer to the actual usage.
  • The check arr[index_sorted] >= 0 in the while loop should probably be index_sorted >= 0u (after all, Element might not be comparable to integers), but this is true in all cases (-1 for an unsigned integer type is equal to that type's maximum value). However, even checking this becomes unnecessary if the first iteration starts at 1.
  • The array elements could possibly be moved to their new locations (this might improve performance in some cases).

template<typename Element>
void insertion_sort(Element arr[], size_t size) {
    for (auto index = 1u; index < size; ++index) {
        auto temp = std::move(arr[index]);
        auto index_sorted = index - 1;
        while (arr[index_sorted] > temp) {
            arr[index_sorted + 1] = std::move(arr[index_sorted--]);
        }
        arr[index_sorted + 1] = std::move(temp);
    }
}

Technically, the size of the array could be deduced automatically by taking a reference (though it has an ugly syntax). However, by taking an explicit size parameter one can sort a partial array, so I'd prefer the flexibility of this approach.

Small nitpicks

  • The for loop can be started from 1, as the iteration with 0 doesn't actually do anything.
  • Prefer to reduce the scope of variables if possible. This allows better reasoning for the compiler/optimizer, and makes the code more readable because the variable declaration is nearer to the actual usage.
  • The check arr[index_sorted] >= 0 in the while loop should probably be index_sorted >= 0u (after all, Element might not be comparable to integers), but this is true in all cases (-1 for an unsigned integer type is equal to that type's maximum value). However, even checking this becomes unnecessary if the first iteration starts at 1.
  • The array elements could possibly be moved to their new locations (this might improve performance in some cases).

template<typename Element>
void insertion_sort(Element arr[], size_t size) {
    for (auto index = 1u; index < size; ++index) {
        auto temp = std::move(arr[index]);
        auto index_sorted = index - 1;
        while (arr[index_sorted] > temp) {
            arr[index_sorted + 1] = std::move(arr[index_sorted--]);
        }
        arr[index_sorted + 1] = std::move(temp);
    }
}

Technically, the size of the array could be deduced automatically by taking a reference (though it has an ugly syntax). However, by taking an explicit size parameter one can sort a partial array, so I'd prefer the flexibility of this approach.

added 151 characters in body
Source Link
hoffmale
  • 6.5k
  • 18
  • 41

#Small nitpicks

  • The for loop can be started from 1, as the iteration with 0 doesn't actually do anything.
  • Prefer to reduce the scope of variables if possible. This allows better reasoning for the compiler/optimizer, and makes the code more readable because the variable declaration is nearer to the actual usage.
  • The check arr[index_sorted] >= 0 in the while loop should probably be index_sorted >= 0u (after all, Element might not be comparable to integers), but this is true in all cases (-1 for an unsigned integer type is equal to that type's maximum value). However, even checking this becomes unnecessary if the first iteration starts at 1.
  • The array elements could possibly be moved to their new locations (this might improve performance in some cases).

template<typename Element>
void insertion_sort(Element arr[], size_t size) {
    for (auto index = 1u; index < size; ++index) {
        auto temp = arr[index];std::move(arr[index]);
        auto index_sorted = index - 1;
        while (arr[index_sorted] > temp) {
            arr[index_sorted + 1] = std::move(arr[index_sorted--];]);
        }
        arr[index_sorted + 1] = temp;std::move(temp);
    }
}

Technically, the size of the array could be deduced automatically by taking a reference (though it has an ugly syntax). However, by taking an explicit size parameter one can sort a partial array, so I'd prefer the flexibility of this approach.

#Small nitpicks

  • The for loop can be started from 1, as the iteration with 0 doesn't actually do anything.
  • Prefer to reduce the scope of variables if possible. This allows better reasoning for the compiler/optimizer, and makes the code more readable because the variable declaration is nearer to the actual usage.
  • The check arr[index_sorted] >= 0 in the while loop should probably be index_sorted >= 0u (after all, Element might not be comparable to integers), but this is true in all cases (-1 for an unsigned integer type is equal to that type's maximum value). However, even checking this becomes unnecessary if the first iteration starts at 1.

template<typename Element>
void insertion_sort(Element arr[], size_t size) {
    for (auto index = 1u; index < size; ++index) {
        auto temp = arr[index];
        auto index_sorted = index - 1;
        while (arr[index_sorted] > temp) {
            arr[index_sorted + 1] = arr[index_sorted--];
        }
        arr[index_sorted + 1] = temp;
    }
}

Technically, the size of the array could be deduced automatically by taking a reference (though it has an ugly syntax). However, by taking an explicit size parameter one can sort a partial array, so I'd prefer the flexibility of this approach.

#Small nitpicks

  • The for loop can be started from 1, as the iteration with 0 doesn't actually do anything.
  • Prefer to reduce the scope of variables if possible. This allows better reasoning for the compiler/optimizer, and makes the code more readable because the variable declaration is nearer to the actual usage.
  • The check arr[index_sorted] >= 0 in the while loop should probably be index_sorted >= 0u (after all, Element might not be comparable to integers), but this is true in all cases (-1 for an unsigned integer type is equal to that type's maximum value). However, even checking this becomes unnecessary if the first iteration starts at 1.
  • The array elements could possibly be moved to their new locations (this might improve performance in some cases).

template<typename Element>
void insertion_sort(Element arr[], size_t size) {
    for (auto index = 1u; index < size; ++index) {
        auto temp = std::move(arr[index]);
        auto index_sorted = index - 1;
        while (arr[index_sorted] > temp) {
            arr[index_sorted + 1] = std::move(arr[index_sorted--]);
        }
        arr[index_sorted + 1] = std::move(temp);
    }
}

Technically, the size of the array could be deduced automatically by taking a reference (though it has an ugly syntax). However, by taking an explicit size parameter one can sort a partial array, so I'd prefer the flexibility of this approach.

added 769 characters in body
Source Link
hoffmale
  • 6.5k
  • 18
  • 41

#Small nitpicks

  • The for loop can be started from 1, as the iteration with 0 doesn't actually do anything.
  • Prefer to reduce the scope of variables if possible. This allows better reasoning for the compiler/optimizer, and makes the code more readable because the variable declaration is nearer to the actual usage.
  • The check arr[index_sorted] >= 0 in the while loop should probably be index_sorted >= 0u (after all, Element might not be comparable to integers), but this is true in all cases (-1 for an unsigned integer type is equal to that type's maximum value). However, even checking this becomes unnecessary if the first iteration starts at 1.

template<typename Element>
void insertion_sort(Element arr[], size_t size) {
    for (auto index = 1u; index < size; ++index) {
        auto temp = arr[index];
        auto index_sorted = index - 1;
        while (arr[index_sorted] > temp) {
            arr[index_sorted + 1] = arr[index_sorted--];
        }
        arr[index_sorted + 1] = temp;
    }
}

Technically, the size of the array could be deduced automatically by taking a reference (though it has an ugly syntax). However, by taking an explicit size parameter one can sort a partial array, so I'd prefer the flexibility of this approach.

#Small nitpicks

  • The for loop can be started from 1, as the iteration with 0 doesn't actually do anything.
  • Prefer to reduce the scope of variables if possible. This allows better reasoning for the compiler/optimizer, and makes the code more readable because the variable declaration is nearer to the actual usage.
  • The check arr[index_sorted] >= 0 in the while loop should probably be index_sorted >= 0u (after all, Element might not be comparable to integers), but this is true in all cases (-1 for an unsigned integer type is equal to that type's maximum value). However, even checking this becomes unnecessary if the first iteration starts at 1.

template<typename Element>
void insertion_sort(Element arr[], size_t size) {
    for (auto index = 1u; index < size; ++index) {
        auto temp = arr[index];
        auto index_sorted = index - 1;
        while (arr[index_sorted] > temp) {
            arr[index_sorted + 1] = arr[index_sorted--];
        }
        arr[index_sorted + 1] = temp;
    }
}

#Small nitpicks

  • The for loop can be started from 1, as the iteration with 0 doesn't actually do anything.
  • Prefer to reduce the scope of variables if possible. This allows better reasoning for the compiler/optimizer, and makes the code more readable because the variable declaration is nearer to the actual usage.
  • The check arr[index_sorted] >= 0 in the while loop should probably be index_sorted >= 0u (after all, Element might not be comparable to integers), but this is true in all cases (-1 for an unsigned integer type is equal to that type's maximum value). However, even checking this becomes unnecessary if the first iteration starts at 1.

template<typename Element>
void insertion_sort(Element arr[], size_t size) {
    for (auto index = 1u; index < size; ++index) {
        auto temp = arr[index];
        auto index_sorted = index - 1;
        while (arr[index_sorted] > temp) {
            arr[index_sorted + 1] = arr[index_sorted--];
        }
        arr[index_sorted + 1] = temp;
    }
}

Technically, the size of the array could be deduced automatically by taking a reference (though it has an ugly syntax). However, by taking an explicit size parameter one can sort a partial array, so I'd prefer the flexibility of this approach.

added 769 characters in body
Source Link
hoffmale
  • 6.5k
  • 18
  • 41
Loading
Source Link
hoffmale
  • 6.5k
  • 18
  • 41
Loading