Answer the question
In order to leave comments, you need to log in
Recursive selection sort, how to implement?
There is a selection sort algorithm using cycles. It is necessary to rewrite using recursion, tell me how.
public void SelectSort(int[] a, ref int sr, ref int obm)
{
int max;
int length = a.Length;
for (int i = 0; i < length - 1; i++)
{
max = i;
for (int j = i + 1; j < length; j++)
{
sr++;
if (a[j] > a[max])
{
max = j;
}
}
sr++;
if (max != i)
{
swap(ref a[i], ref a[max]);
obm++;
}
}
}
Answer the question
In order to leave comments, you need to log in
Somehow, just like that.
static void RecursionSelect(int[] arr, int length)
{
if(length==1)
return;
int max = int.MinValue;
int tmp;
int index = 0;
for(int i = 0;i<length-1;i++ )
{
if(arr[i]>max){
max = arr[i];
index = i;
}
}
if(max>arr[length-1])
{
tmp = arr[length-1];
arr[length - 1] = arr[index];
arr[index] = tmp;
}
RecursionSelect(arr, length-1);
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question