Y
Y
Yozhik_v_tumane2018-05-31 00:52:43
recursion
Yozhik_v_tumane, 2018-05-31 00:52:43

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

1 answer(s)
K
Kushin, 2018-05-31
@Yozhik_v_tumane

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);
        }

The recursion for selection basically a piece superfluous. With its help, the merger is implemented much more successfully.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question