合并排序 - 一个数组中的四个排序部分

时间:2015-12-07 19:46:47

标签: c++ sorting mergesort

我有一个数组,其中有4个排序部分。例如

int array[20] = {1,4,7,8,10,2,3,6,8,11,1,2,7,8,9,3,4,9,10,13}

我需要做的是对前2个排序部分(1,4,7,8,10和2,3,6,8,11)使用合并排序,然后对第2个排序部分(1,2, 7,8,9和3,4,9,10,13)。然后我需要将这两个排序的部分合并为一个排序的数组。

我尝试使用这些代码,但是有些问题。

void Merge(int *array, int *aux, int left, int right)
{
    int middleIndex = (left + right) / 2;
    int leftIndex = left;
    int rightIndex = middleIndex + 1;
    int auxIndex = left;
    while (leftIndex <= middleIndex && rightIndex <= right)
    {
        if (array[leftIndex] >= array[rightIndex])
        {
            aux[auxIndex] = array[leftIndex++];
        }
        else
        {
            aux[auxIndex] = array[rightIndex++];
        }
        auxIndex++;
    }
    while (leftIndex <= middleIndex)
    {
        aux[auxIndex] = array[leftIndex++];
        auxIndex++;
    }
    while (rightIndex <= right)
    {
        aux[auxIndex] = array[rightIndex++];
        auxIndex++;
    }
}

知道如何修改它,或者写得更好?感谢

3 个答案:

答案 0 :(得分:1)

当我的C#mergesort算法出现问题时,我提到了这篇文章: http://www.bluesharktutorials.com/2013/08/merge-sort-algorithm-with-c-program-code.html

如果你读到这个并且仍然有问题,请告诉我,但这应该对你有所帮助。

答案 1 :(得分:1)

        import java.util.Arrays;

        public class Main {

            public static void main(String[] args) {

                int[]  sortArr1 =  merge(new int[] {1 , 4 , 7 , 8 , 10} , new int[] {2 , 3 , 6 , 8 , 11} );
                System.out.println("Merging of sorted array  " +Arrays.toString(sortArr1));

                int[]  sortArr2 =  merge(new int[] {1 , 2 , 7 , 8 , 9} , new int[] {3 , 4 , 9 , 10 , 13} );
                System.out.println("Merging of sorted array  " +Arrays.toString(sortArr2));

                int[]  finalSort = merge(sortArr1 , sortArr2);
                System.out.println("Merging of sorted array  " +Arrays.toString(finalSort));

            }

            public static int[] merge(int[] arr1 , int[] arr2) {

                int[] sort = new int[arr1.length + arr2.length];         
                int j = 0;
                int k = 0;

                for(int i = 0 ; i < sort.length ; i++ ) {
                    if(j <= (arr1.length - 1)  && k <= (arr2.length - 1)) {
                        if(arr1[j] > arr2[k]) {
                            sort[i] = arr2[k++];
                        }else {
                            sort[i] = arr1[j++];
                        }
                    }else if(j <= (arr1.length - 1)) {
                        sort[i] = arr1[j++];
                    }else if(k <= (arr2.length - 1)){
                        sort[i] = arr2[k++];
                    }
                }

                return sort;
            }

        }

程序输出为:

       Merging of sorted array  [1, 2, 3, 4, 6, 7, 8, 8, 10, 11]
       Merging of sorted array  [1, 2, 3, 4, 7, 8, 9, 9, 10, 13]
       Merging of sorted array  [1, 1, 2, 2, 3, 3, 4, 4, 6, 7, 7, 8, 8, 8, 9, 9, 10, 10, 11, 13]

答案 2 :(得分:0)

  

知道如何修改它,或者写得更好?感谢

由于范围已经排序,并且您知道范围的开始和结束位置,请使用std::inplace_merge

#include <algorithm>
#include <iostream>
#include <iterator>

int array[20] = {1,4,7,8,10,2,3,6,8,11,1,2,7,8,9,3,4,9,10,13};

using namespace std;

void MergeSort(int *arr, int start1, int start2, int start3, 
               int start4, int size)
{
   std::inplace_merge(arr + start1, arr + start2, arr + start3);
   std::inplace_merge(arr + start3, arr + start4, arr + size);
   std::inplace_merge(array, array + start3, arr + size);
}

int main()
{
   MergeSort(array, 0, 5, 10, 15, 20);
   std::copy(array, array + 20, ostream_iterator<int>(cout, " "));
}

Live Example

相关问题