仅使用数组在c#中进行Strand排序

时间:2017-04-17 13:52:40

标签: c# arrays algorithm sorting

我有一个使用链排序的作业,我必须从初始数组中获取不断增长的数字序列,并将它们合并在代表我们结果的数组中(C#)

喜欢这个http://imgur.com/nQFzJw7

到目前为止,我做过类似的事情

public static int[] Str(int[] a)
    {
        int i, j, x ,temp,k=0,count=1;
        int size = a.Length;
        int len = a.Length;
        Strand = new int[size];
        Merged = new int[size];




        for (i = k; i < size; i++)
        {   
            x = a[i];
            Strand[0] = x;

            for (i = k; i < size; i++)      //checking if there's a bigger int than the first one
            {
                if (a[i] > x)
                {
                    x = a[i];

                }

            }

            for (i = k; i < len; i++)       
            {
                if (a[i] == x)     // checking if the max appears more than 1 time
                {

                    temp = a[i];
                    a[i] = a[len];
                    a[len] = temp;
                    len--;         //swaps the max numbers to the last position

                    Strand[count] = x;
                    count++;





                }

            }
            for (i = 0; i < count; i++) // cant find a way to put in the final merged and sorted array
            {

            }
            count = 1;
            k++;
        }

有什么建议吗?

1 个答案:

答案 0 :(得分:0)

您始终需要将第一个元素提取到strand

 strand[0] = a[0]
 count = 1

然后你需要将合适的元素提取到strand中,移动数组的其余部分

for i = 1 to size - 1
   if a[i] >= strand[count - 1]
        strand[count++] = a[i]
   else
        a[i - count] = a[i]
size = size - count

然后您需要合并当前strandmerged - 从MergeSort中查找合并过程

重复这些步骤,直到size变为0