颠倒我的MergeSort算法

时间:2015-04-22 11:04:31

标签: c# algorithm mergesort

基本上,我的代码中有一个合并排序,用于在文本文件中订购一组数字。我通过菜单给出了选项,允许用户按升序和降序对这些数字进行排序。目前我只有一种方式,我不能为我的生活弄清楚如何扭转它。它可能非常简单。这是我的代码:

方法

static public void mergemethod(int [] numbers, int left, int mid, int right)
    {
        int [] temp = new int[144];
        int i, left_end, num_elements, tmp_pos;
        left_end = (mid - 1);
        tmp_pos = left;
        num_elements = (right - left + 1);
        while ((left <= left_end) && (mid <= right))
        {
            if (numbers[left] <= numbers[mid])
                temp[tmp_pos++] = numbers[left++];
            else
                temp[tmp_pos++] = numbers[mid++];
        }
        while (left <= left_end)
            temp[tmp_pos++] = numbers[left++];
        while (mid <= right)
            temp[tmp_pos++] = numbers[mid++];
        for (i = 0; i < num_elements; i++)
        {
            numbers[right] = temp[right];
            right--;
        }

    }
    static public void sortmethod(int [] numbers, int left, int right)
    {
      int mid;
      if (right > left)
      {
        mid = (right + left) / 2;
        sortmethod(numbers, left, mid);
        sortmethod(numbers, (mid + 1), right);
        mergemethod(numbers, left, (mid+1), right);

      }
    }

使用它的地方。

private static void SortVolume(int sortingAD)
    {
        if (sortingAD == 1)
        {
            int[] numbers = Array.ConvertAll(Volume, int.Parse);
            int len = 144;
            Console.WriteLine("\nAscending order: To return, press any key");
            sortmethod(numbers, 0, len - 1);
            for (int i = 0; i < 144; i++)
                Console.WriteLine(numbers[i]);
        }
        else if (sortingAD == 2)
        {
            //This is where the reverse code will go(?)
        }

        Console.ReadKey();
        MainMenu(true);
    }

感谢。

0 个答案:

没有答案