编辑:OP澄清了这个问题,这可能与OP无关,但可能对未来的访问者有用。
请记住下次对你的问题的评论。就这一次,我会帮你的,因为你是新来的。通过做一个简单的谷歌搜索,我发现了下面代码的最大部分
this site
. 我添加了
selectionSort
方法我自己适应您的参数。请注意,堆栈溢出不是一个代码编写服务(即使人们经常提供代码,但这不是应答中的一个要求)。
public void selectionSort(int a[], int n)
{
recurSelectionSort(a, n, 0);
}
// Recursive selection sort. n is size of a[] and index
// is index of starting element.
static void recurSelectionSort(int a[], int n, int index)
{
// Return when starting and size are same
if (index == n)
return;
// calling minimum index function for minimum index
int k = minIndex(a, index, n-1);
// Swapping when index nd minimum index are not same
if (k != index){
// swap
int temp = a[k];
a[k] = a[index];
a[index] = temp;
}
// Recursively calling selection sort function
recurSelectionSort(a, n, index + 1);
}
// Return minimum index
static int minIndex(int a[], int i, int j)
{
if (i == j)
return i;
// Find minimum of remaining elements
int k = minIndex(a, i + 1, j);
// Return minimum of current and remaining.
return (a[i] < a[k])? i : k;
}