Skip to main content
Code Review

Return to Answer

example
Source Link
CiaPan
  • 1.9k
  • 1
  • 12
  • 17

This is quite non-standard way of choosing a pivot value:

 int pivot = (array[beginning] + array[end]) / 2;

It may yield a value which does not exist in the array being sorted. That should not hurt, as the value usually should be between values of array[beginning] and array[end], so at worst your 3-way partitioning would become 2-way.

However, if both terms of a sum are positive and big (say, bigger than INT_MAX/2), they may add up to a value greater than the int type range limit, which will cause an arithmetic overflow and can yield pivot < 0. That can lead to a 1-way partitioning and an 'infinite recursion' which would end with a memory overflow. (Same scenario may happen for two negative values, big wrt. an absolute value, say less than -INT_MIN/2.)

It is much better to choose a pivot value from those existing in your array. That guarantees at least one item per recursion level goes to its final position, so the whole process will stop eventually.

It is also way easier in a general case, because arithmetics on integer indices is quite well-defined, which needn't apply to array items. Suppose, for example, sorting an array of strings – how would you compute a sum (array[beginning] + array[end]) if array[beginning] is "Los Angeles" while array[end] is "Tokyo"...? And how are you going to divide that sum by 2...?

This is quite non-standard way of choosing a pivot value:

 int pivot = (array[beginning] + array[end]) / 2;

It may yield a value which does not exist in the array being sorted. That should not hurt, as the value usually should be between values of array[beginning] and array[end], so at worst your 3-way partitioning would become 2-way.

However, if both terms of a sum are positive and big (say, bigger than INT_MAX/2), they may add up to a value greater than the int type range limit, which will cause an arithmetic overflow and can yield pivot < 0. That can lead to a 1-way partitioning and an 'infinite recursion' which would end with a memory overflow. (Same scenario may happen for two negative values, big wrt. an absolute value, say less than -INT_MIN/2.)

It is much better to choose a pivot value from those existing in your array. That guarantees at least one item per recursion level goes to its final position, so the whole process will stop eventually.

This is quite non-standard way of choosing a pivot value:

 int pivot = (array[beginning] + array[end]) / 2;

It may yield a value which does not exist in the array being sorted. That should not hurt, as the value usually should be between values of array[beginning] and array[end], so at worst your 3-way partitioning would become 2-way.

However, if both terms of a sum are positive and big (say, bigger than INT_MAX/2), they may add up to a value greater than the int type range limit, which will cause an arithmetic overflow and can yield pivot < 0. That can lead to a 1-way partitioning and an 'infinite recursion' which would end with a memory overflow. (Same scenario may happen for two negative values, big wrt. an absolute value, say less than -INT_MIN/2.)

It is much better to choose a pivot value from those existing in your array. That guarantees at least one item per recursion level goes to its final position, so the whole process will stop eventually.

It is also way easier in a general case, because arithmetics on integer indices is quite well-defined, which needn't apply to array items. Suppose, for example, sorting an array of strings – how would you compute a sum (array[beginning] + array[end]) if array[beginning] is "Los Angeles" while array[end] is "Tokyo"...? And how are you going to divide that sum by 2...?

fixing my mistake
Source Link
CiaPan
  • 1.9k
  • 1
  • 12
  • 17

This is quite non-standard way of choosing a pivot value:

 int pivot = (array[beginning] + array[end]) / 2;

It may yield a value which does not exist in the array being sorted. That should not hurt, as the value usually should be between values of array[beginning] and array[end], so at worst your 3-way partitioning would become 2-way.

However, if both terms of a sum are positive and big (say, bigger than INT_MAX/2), they may add up to a value greater than the int type range limit, which will cause an arithmetic overflow and can yield pivot < 0. That can lead to a 1-way partitioning and an 'infinite recursion' which would end with a memory overflow. (Same scenario may happen for two negative values, big wrt. an absolute value, say less than -INT_MIN/2.)

It is much better to choose a pivot value from those existing in your array. That guarantees at least one item per recursion level goes to its final position, so the whole process will stop eventually.

This is quite non-standard way of choosing a pivot value:

 int pivot = (array[beginning] + array[end]) / 2;

It may yield a value which does not exist in the array being sorted. That should not hurt, as the value usually should be between values of array[beginning] and array[end], so at worst your 3-way partitioning would become 2-way.

However, if both terms of a sum are positive and big (say, bigger than INT_MAX/2), they may add up to a value greater than the int type range limit, which will cause an arithmetic overflow and can yield pivot < 0. That can lead to a 1-way partitioning and an 'infinite recursion' which would end with a memory overflow. (Same scenario may happen for two negative values, big wrt. an absolute value, say less than INT_MIN/2.)

It is much better to choose a pivot value from those existing in your array. That guarantees at least one item per recursion level goes to its final position, so the whole process will stop eventually.

This is quite non-standard way of choosing a pivot value:

 int pivot = (array[beginning] + array[end]) / 2;

It may yield a value which does not exist in the array being sorted. That should not hurt, as the value usually should be between values of array[beginning] and array[end], so at worst your 3-way partitioning would become 2-way.

However, if both terms of a sum are positive and big (say, bigger than INT_MAX/2), they may add up to a value greater than the int type range limit, which will cause an arithmetic overflow and can yield pivot < 0. That can lead to a 1-way partitioning and an 'infinite recursion' which would end with a memory overflow. (Same scenario may happen for two negative values, big wrt. an absolute value, say less than -INT_MIN/2.)

It is much better to choose a pivot value from those existing in your array. That guarantees at least one item per recursion level goes to its final position, so the whole process will stop eventually.

added 205 characters in body
Source Link
CiaPan
  • 1.9k
  • 1
  • 12
  • 17

This is quite non-standard way of choosing a pivot value:

 int pivot = (array[beginning] + array[end]) / 2;

It may yield a value which does not exist in the array being sorted. That should not hurt, as the value usually should be between values of array[beginning] and array[end], so at worst your 3-way partitioning would become 2-way.

However, if both terms of a sum are positive and big (say, bigger than INT_MAX/2), they may add up to a value greater than the int type range limit, which will cause an arithmetic overflow and can yield pivot < 0. That can lead to a 1-way partitioning and an 'infinite recursion' which would end with a memory overflow. (Same scenario may happen for two negative values, big wrt. an absolute value, say less than INT_MIN/2.)

It is much better to choose a pivot value from those existing in your array. That guarantees at least one item per recursion level goes to its final position, so the whole process will stop eventually.

This is quite non-standard way of choosing a pivot value:

 int pivot = (array[beginning] + array[end]) / 2;

It may yield a value which does not exist in the array being sorted. That should not hurt, as the value usually should be between values of array[beginning] and array[end], so at worst your 3-way partitioning would become 2-way.

However, if both terms of a sum are positive and big (say, bigger than INT_MAX/2), they may add up to a value greater than the int type range limit, which will cause an arithmetic overflow and can yield pivot < 0. That can lead to a 1-way partitioning and an 'infinite recursion' which would end with a memory overflow. (Same scenario may happen for two negative values, big wrt. an absolute value, say less than INT_MIN/2.)

This is quite non-standard way of choosing a pivot value:

 int pivot = (array[beginning] + array[end]) / 2;

It may yield a value which does not exist in the array being sorted. That should not hurt, as the value usually should be between values of array[beginning] and array[end], so at worst your 3-way partitioning would become 2-way.

However, if both terms of a sum are positive and big (say, bigger than INT_MAX/2), they may add up to a value greater than the int type range limit, which will cause an arithmetic overflow and can yield pivot < 0. That can lead to a 1-way partitioning and an 'infinite recursion' which would end with a memory overflow. (Same scenario may happen for two negative values, big wrt. an absolute value, say less than INT_MIN/2.)

It is much better to choose a pivot value from those existing in your array. That guarantees at least one item per recursion level goes to its final position, so the whole process will stop eventually.

Source Link
CiaPan
  • 1.9k
  • 1
  • 12
  • 17
Loading
lang-java

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