user10497264 :
I am trying to write a selection sort algorithom. As part of the algorithom I need to switch 2 values in the array, I tryed it as follows: array[min] = array[i]; array[i] = array[min];
But I belive this wont work because array[min]
will already be =
to array[i]
. So how do I do this swap? bellow is my code.
static int[] array = {3, 2, 1, 4, 5, 6};
static int n = 5;
static int temp;
for (int i = 0; i<=5; i++) {
int min = 0;
for (int j = i+1; j<=n; j++) {
//System.out.println(j);
if (array[j]<array[min]) {
min = j;
}
if (min != i) {
array[min] = array[i];
array[i] = array[min];
}
}
}
CodeFromthe510 :
Need a temp mem location to store a value.
i.e.
temp = array[min]
array[min] = array[i]
array[i] = temp