- #1
magnifik
- 360
- 0
I'm trying to understand a sorting algorithm (selection sort, to be exact). I started out with this:
public static void selectionSort1(int[] x) {
for (int i=0; i<x.length-1; i++) {
for (int j=1; j<x.length; j++) {
if (x > x[j]) {
//... Exchange elements
int temp = x;
x = x[j];
x[j] = temp;
}
}
}
}
but that didn't work so i made one slight modification by changing j to i + 1
public static void selectionSort1(int[] x) {
for (int i=0; i<x.length-1; i++) {
for (int j=i+1; j<x.length; j++) {
if (x > x[j]) {
//... Exchange elements
int temp = x;
x = x[j];
x[j] = temp;
}
}
}
}
i'm having trouble figuring out what's the difference between the codes?
public static void selectionSort1(int[] x) {
for (int i=0; i<x.length-1; i++) {
for (int j=1; j<x.length; j++) {
if (x > x[j]) {
//... Exchange elements
int temp = x;
x = x[j];
x[j] = temp;
}
}
}
}
but that didn't work so i made one slight modification by changing j to i + 1
public static void selectionSort1(int[] x) {
for (int i=0; i<x.length-1; i++) {
for (int j=i+1; j<x.length; j++) {
if (x > x[j]) {
//... Exchange elements
int temp = x;
x = x[j];
x[j] = temp;
}
}
}
}
i'm having trouble figuring out what's the difference between the codes?