Skip to main content
Code Review

Return to Answer

Commonmark migration
Source Link

Too many swaps

##Too many swaps## TheThe "standard" selection sort finds the minimum element and swaps the first element with the minimum element once per loop. In total there should be N swaps. Your version of selection sort can swap up to (N^2)/2 times.

Unnecessary variable

I'm not sure why you use the variable result and return it. You can just operate on the array that is passed in (list) directly. When I first read your code I thought you were making a copy of the array, because you had this extraneous variable.

The "standard" selection sort

Here is an example of a "standard" selection sort:

public static void selectionSort(int[] list)
{
 int len = list.length;
 for (int i = 0; i < len - 1; i++) {
 int smallestIndex = i;
 int smallestValue = list[i];
 for (int j = i + 1; j < len; j++) {
 if (list[j] < smallestValue) {
 smallestIndex = j;
 smallestValue = list[j];
 }
 }
 if (smallestIndex != i) {
 list[smallestIndex] = list[i];
 list[i] = smallestValue;
 }
 }
}

##Too many swaps## The "standard" selection sort finds the minimum element and swaps the first element with the minimum element once per loop. In total there should be N swaps. Your version of selection sort can swap up to (N^2)/2 times.

Unnecessary variable

I'm not sure why you use the variable result and return it. You can just operate on the array that is passed in (list) directly. When I first read your code I thought you were making a copy of the array, because you had this extraneous variable.

The "standard" selection sort

Here is an example of a "standard" selection sort:

public static void selectionSort(int[] list)
{
 int len = list.length;
 for (int i = 0; i < len - 1; i++) {
 int smallestIndex = i;
 int smallestValue = list[i];
 for (int j = i + 1; j < len; j++) {
 if (list[j] < smallestValue) {
 smallestIndex = j;
 smallestValue = list[j];
 }
 }
 if (smallestIndex != i) {
 list[smallestIndex] = list[i];
 list[i] = smallestValue;
 }
 }
}

Too many swaps

The "standard" selection sort finds the minimum element and swaps the first element with the minimum element once per loop. In total there should be N swaps. Your version of selection sort can swap up to (N^2)/2 times.

Unnecessary variable

I'm not sure why you use the variable result and return it. You can just operate on the array that is passed in (list) directly. When I first read your code I thought you were making a copy of the array, because you had this extraneous variable.

The "standard" selection sort

Here is an example of a "standard" selection sort:

public static void selectionSort(int[] list)
{
 int len = list.length;
 for (int i = 0; i < len - 1; i++) {
 int smallestIndex = i;
 int smallestValue = list[i];
 for (int j = i + 1; j < len; j++) {
 if (list[j] < smallestValue) {
 smallestIndex = j;
 smallestValue = list[j];
 }
 }
 if (smallestIndex != i) {
 list[smallestIndex] = list[i];
 list[i] = smallestValue;
 }
 }
}
added 126 characters in body
Source Link
JS1
  • 28.9k
  • 3
  • 41
  • 83

##Too many swaps## The "standard" selection sort finds the minimum element and swaps the first element with the minimum element once per loop. In total there should be N swaps. Your version of selection sort can swap up to (N^2)/2 times.

Unnecessary copy?variable

I'm not sure why you are making a copy ofuse the arrayvariable result and return it. Most sort functions sort You can just operate on the actual array that is passed in instead of returning(list) directly. When I first read your code I thought you were making a copy of the array, because you had this extraneous variable.

The "standard" selection sort

Here is an example of a "standard" selection sort:

public static void selectionSort(int[] list)
{
 int len = list.length;
 for (int i = 0; i < len - 1; i++) {
 int smallestIndex = i;
 int smallestValue = list[i];
 for (int j = i + 1; j < len; j++) {
 if (list[j] < smallestValue) {
 smallestIndex = j;
 smallestValue = list[j];
 }
 }
 if (smallestIndex != i) {
 list[smallestIndex] = list[i];
 list[i] = smallestValue;
 }
 }
}

##Too many swaps## The "standard" selection sort finds the minimum element and swaps the first element with the minimum element once per loop. In total there should be N swaps. Your version of selection sort can swap up to (N^2)/2 times.

Unnecessary copy?

I'm not sure why you are making a copy of the array. Most sort functions sort the actual array passed in instead of returning a copy.

The "standard" selection sort

Here is an example of a "standard" selection sort:

public static void selectionSort(int[] list)
{
 int len = list.length;
 for (int i = 0; i < len - 1; i++) {
 int smallestIndex = i;
 int smallestValue = list[i];
 for (int j = i + 1; j < len; j++) {
 if (list[j] < smallestValue) {
 smallestIndex = j;
 smallestValue = list[j];
 }
 }
 if (smallestIndex != i) {
 list[smallestIndex] = list[i];
 list[i] = smallestValue;
 }
 }
}

##Too many swaps## The "standard" selection sort finds the minimum element and swaps the first element with the minimum element once per loop. In total there should be N swaps. Your version of selection sort can swap up to (N^2)/2 times.

Unnecessary variable

I'm not sure why you use the variable result and return it. You can just operate on the array that is passed in (list) directly. When I first read your code I thought you were making a copy of the array, because you had this extraneous variable.

The "standard" selection sort

Here is an example of a "standard" selection sort:

public static void selectionSort(int[] list)
{
 int len = list.length;
 for (int i = 0; i < len - 1; i++) {
 int smallestIndex = i;
 int smallestValue = list[i];
 for (int j = i + 1; j < len; j++) {
 if (list[j] < smallestValue) {
 smallestIndex = j;
 smallestValue = list[j];
 }
 }
 if (smallestIndex != i) {
 list[smallestIndex] = list[i];
 list[i] = smallestValue;
 }
 }
}
Source Link
JS1
  • 28.9k
  • 3
  • 41
  • 83

##Too many swaps## The "standard" selection sort finds the minimum element and swaps the first element with the minimum element once per loop. In total there should be N swaps. Your version of selection sort can swap up to (N^2)/2 times.

Unnecessary copy?

I'm not sure why you are making a copy of the array. Most sort functions sort the actual array passed in instead of returning a copy.

The "standard" selection sort

Here is an example of a "standard" selection sort:

public static void selectionSort(int[] list)
{
 int len = list.length;
 for (int i = 0; i < len - 1; i++) {
 int smallestIndex = i;
 int smallestValue = list[i];
 for (int j = i + 1; j < len; j++) {
 if (list[j] < smallestValue) {
 smallestIndex = j;
 smallestValue = list[j];
 }
 }
 if (smallestIndex != i) {
 list[smallestIndex] = list[i];
 list[i] = smallestValue;
 }
 }
}
lang-java

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