This question is similar to ( link ), but not quite what I'm looking for. Here is the selection I wrote below, it is in Java.
public void sort() {
int smallIndex;
for(int i = 0; i < arr.length; i++) {
smallIndex = i;
for (int j = i + 1; j < arr.length; j++) {
if (arr[j] < arr[smallIndex]) {
smallIndex = j;
}
}
if(i < smallIndex)
swap(i, smallIndex);
}
}
Here is my swap method.
public void swap(int a, int b) {
int temp = arr[a];
arr[a] = arr[b];
arr[b] = temp;
}
I fill an array with 50,000 random elements between 0 and 999. The method sorts it in order. Each execution is about 1400 milliseconds (1300 - 1500).
Here is a selection from the book I was looking at.
public void sortBook() {
for(int i = 0; i < arr.length; i++) {
int minPos = minimumPosition(i);
swap(minPos, i);
}
}
Here is a minimal placement method.
public int minimumPosition(int from) {
int minPos = from;
for(int i = from + 1; i < arr.length; i++) {
if(arr[i] < arr[minPos]) {
minPos = i;
}
}
return minPos;
}
The swap method is the one I use on top. I consistently get about 700 milliseconds of runtime for this with 50,000 elements with a range of 0 - 999.
, sort() , sortBook(). , , , , , sort() , sortBook() . .
, sort() sortBook(). - , ?