问题
In the last hours I have tried to write the selection sort algorithm in Java without looking at finished code. I just read how the algorithm works (by words).
I won't copy paste the explanation but rather depict it (to check if I understood it):
-We have one unsorted array A and an empty array B (which has the same size as A)
-Now take the unsorted array A and find its smallest element. Smallest element found, now switch this element with the first element of the unsorted array A
-Put the first element (=smallest element of array A) into the array B
-Repeat till we are done with every element of A
I tried to code that in Java:
public class Selectionsort{
public static void main(String[] args) {
int[] myArray = {9,6,1,3,0,4,2};
int[] B = new int[myArray.length];
for(int i=0; i<myArray.length; i++) {
B[i]=findMIN(myArray, i);
}
}
static int findMIN(int[] A, int c) {
int x = A[c];
while(c<A.length) {
if(x>A[c]) {
x=A[c];
}
c++;
}
return x;
}
}
But I get a weird output:
0 0 0 0 0 2 2
回答1:
First, fix your findMIN
, you should return the index of the minimum element (and compare the element at the current value to the minimum). Like,
static int findMIN(int[] A, int c) {
int x = c;
for (; c < A.length; c++) {
if (A[c] < A[x]) {
x = c;
}
}
return x;
}
Then I would use a swap
method, like
static void swap(int[] A, int a, int b) {
if (a != b) {
int t = A[a];
A[a] = A[b];
A[b] = t;
}
}
Finally, tie it together like
public static void main(String[] args) {
int[] myArray = { 9, 6, 1, 3, 0, 4, 2 };
for (int i = 0; i < myArray.length; i++) {
swap(myArray, i, findMIN(myArray, i));
}
System.out.println(Arrays.toString(myArray));
}
And, I get
[0, 1, 2, 3, 4, 6, 9]
回答2:
Why doesn't my selection sort algorithm do what it's supposed to (java)?
Lets see whats happening:
c A[c] Returned X
-----------------------------------
0 9 0(Since minElement is 0 with index >= 0)
1 6 0(Since minElement is 0 with index >= 1)
2 1 0(Since minElement is 0 with index >= 2)
3 3 0(Since minElement is 0 with index >= 3)
4 0 0(Since minElement is 0 with index >= 4)
5 4 2(Since minElement is 2 with index >= 5)
6 2 2(Since minElement is 2 with index >= 6)
回答3:
"Smallest element found, now switch this element with the first element of the unsorted array A". So, you must switch the first element with the smallest element. You can use the code like this:
static int findMIN(int[] A, int c) {
int first = c; // The first element's id
int id = c; // An id of the smallest element
int x = A[c];
while(c<A.length) {
if(x>A[c]) {
x=A[c];
id = c;
}
c++;
}
// Switching the first element with the smallest element
int tmp = A[first];
A[first] = A[id];
A[id] = tmp;
return x;
}
It works perfectly. Also check @VidorVistrom's answer, if you don't understand why does your code work as it works.
回答4:
Your findMIN
function always returns min value of the myArray
starting from ith
index. You are getting first 5 values 0
because, myArray[4]
contains 0
. Then for the last two digits you are getting 2
because from myArray[5]
to myArray[6]
min value is 2
.
Just after finding min value in your findMIN
function you should swap this value with the initial index 'c' you passed into the function. In that case next time when you call findMIN
function it will exclude that min value you got earlier.
As you did not want to see any implementation I try to say the solution in words. Hope it helps you.
回答5:
You don't swap elements when you find the minimum. Try this fix
static int findMIN(int[] A, int c) {
int x = A[c];
int min_index = c;
for(int i=c; i<A.length; i++) {
if(x>A[i]) {
x=A[i];
min_index = i;
}
}
A[min_index] = A[c];
A[c] = A[min_index];
return x;
}
Also know that Selection Sort is an in place algorithm, so you don't need a separate empty array to sort.
回答6:
public class Selectionsort {
public static void main(String[] args) {
int[] myArray = {9, 6, 1, 3, 0, 4, 2};
for (int i = 0; i < myArray.length; i++) {
int pos = findMIN(myArray, i);
int element = myArray[pos];
//swap
int temp = myArray[i];
myArray[i] = element;
myArray[pos] = temp;
}
System.out.println(Arrays.toString(myArray));
}
//get the postion
static int findMIN(int[] A, int c) {
int x = A[c];
int position = c;
while (c < A.length) {
if (x > A[c]) {
x = A[c];
position = c;
}
c++;
}
return position;
}
}
来源:https://stackoverflow.com/questions/44990340/why-doesnt-my-selection-sort-algorithm-do-what-its-supposed-to-java