68 lines
1.5 KiB
Java
68 lines
1.5 KiB
Java
|
|
public class selectionsort {
|
|
|
|
final static int[] data = {23,11,13,44,14,24,22,12,21,32,41,43,31,33,34,42};
|
|
|
|
public void selectionSort2(int[] x) {
|
|
for (int i=0; i<x.length-1; i++) {
|
|
int minIndex = i; // Index of smallest remaining value.
|
|
for (int j=i+1; j<x.length; j++) {
|
|
if (x[minIndex] > x[j]) {
|
|
minIndex = j; // Remember index of new minimum
|
|
}
|
|
}
|
|
if (minIndex != i) {
|
|
//... Exchange current element with smallest remaining.
|
|
int temp = x[i];
|
|
x[i] = x[minIndex];
|
|
x[minIndex] = temp;
|
|
|
|
System.out.println("step: "+i+" "+minIndex+" number:"+temp+" "+x[i]);
|
|
|
|
printdata();
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
public void selectionSort(int[] x) {
|
|
for (int i=0; i<x.length-1; i++) {
|
|
for (int j=i+1; j<x.length; j++) {
|
|
if (x[i] > x[j]) {
|
|
//... Exchange elements
|
|
int temp = x[i];
|
|
x[i] = x[j];
|
|
x[j] = temp;
|
|
|
|
System.out.println("step: "+i+" "+j+" number:"+temp+" "+x[i]);
|
|
|
|
printdata();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public void printdata()
|
|
{
|
|
String s = new String("{");
|
|
|
|
for(int i=0; i<data.length; i++)
|
|
{
|
|
s += data[i];
|
|
s += ",";
|
|
}
|
|
|
|
s += "}";
|
|
System.out.println(s);
|
|
}
|
|
|
|
/**
|
|
* @param args
|
|
*/
|
|
public static void main(String[] args) {
|
|
new selectionsort().selectionSort2(data);
|
|
|
|
}
|
|
|
|
}
|