I'm trying to understand a sorting algorithm (selection sort, to be exact). I started out with this:(adsbygoogle = window.adsbygoogle || []).push({});

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?

**Physics Forums | Science Articles, Homework Help, Discussion**

Join Physics Forums Today!

The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

# Homework Help: Iterating through an array

**Physics Forums | Science Articles, Homework Help, Discussion**