合并排序C++简介

210 阅读3分钟

Merge sort C++

合并排序C++简介

基于分而治之算法的排序技术称为合并排序,它将给定的输入数组分成两半,对分成的两半调用相同的合并排序函数,对分成的两半进行排序,然后将排序后的两半合并为一个单一的排序数组,用于对每个分成的数组进行排序的函数称为mergeSort()函数,用于合并两个数组。合并排序技术的时间复杂度为O(n log n),空间复杂度为O(n)。在本专题中,我们将学习C++的合并排序。

在C++中定义merge()函数的语法如下。

merge(input_array, left, middle, right)

其中input_array是待排序的元素数组,left表示数组中的左边索引,middle表示数组中的中间索引,right表示数组中的右边索引。

mergeSort(input_array, left, right)

而input_array是待排序的元素数组,左表示数组中的下限,右表示数组中的上限。

C++中merge()函数和mergeSort()函数的工作原理

  • 合并排序的工作从找到中间点开始,它将给定的输入数组分成两部分。
  • 然后,我们将在给定的输入数组的前半部分调用mergeSort()函数,对数组进行排序。
  • 然后,我们将对给定输入数组的后半部分调用mergeSort()函数对数组进行排序。
  • 然后,我们将调用merge()函数来合并第二和第三步的两个排序数组。
  • merge()函数返回一个单一的排序数组。

合并排序C++的例子

下面是下面提到的例子

例子#1

C++程序演示了合并排序技术,通过实现merge()函数和mergeSort()函数对一个给定的输入数组进行排序,然后将结果数组作为输出显示在屏幕上。

#include <iostream>
using namespace std;
//defining the merge function to merge the two sorted halves of the given input array
void merge(int array[], int left, int middle, int right)
{
//dividing the given array into two halves
int fh = middle - left + 1;
int sh = right - middle;
//creating two temporary arrays
int t1[fh], t2[sh];
//copying the data from the two halves into two temporary arrays
for (int m = 0; m < fh; m++)
t1[m] = array[left + m];
for (int n = 0; n < sh; n++)
t2[n] = array[middle + 1 + n];
//merging the temporary arrays into a single array
int a = 0;
int b = 0;
int c = left;
while (a < fh && b < sh) {
if (t1[a] <= t2[b]) {
array[c] = t1[a];
a++;
}
else {
array[c] = t2[b];
b++;
}
c++;
}
while (a < fh) {
array[c] = t1[a];
a++;
c++;
}
while (b < sh) {
array[c] = t2[b];
b++;
c++;
}
}
//sorting each of the divided arrays using mergeSort() function
void mergeSort(int array[],int left,int right){
if(left>=right){
return;
}
int middle =left+ (right-left)/2;
mergeSort(array,left,middle);
mergeSort(array,middle+1,right);
merge(array,left,middle,right);
}
// function to print the resulting array
void printArray(int Array[], int size)
{
for (int d = 0; d < size; d++)
cout << Array[d] << " ";
}
// defining the main function
int main()
{
int array[] = { 20, 50, 10, 5, 25, 45 };
int array_size = sizeof(array) / sizeof(array[0]);
cout << "The elements of the input array before sorting are: \n";
printArray(array, array_size);
mergeSort(array, 0, array_size - 1);
cout << "\nThe elements of the input array after sorting are: \n";
printArray(array, array_size);
return 0;
}

上述程序的输出在下面的快照中显示。

Merge sort C++ output 1

在上面的程序中,我们已经找到了中间点,它将给定的输入数组分成了两部分。然后我们定义了merge()、mergeSort()和printArray()函数。然后,我们定义了主函数,在这个函数中,我们将给定的数组进行分割,然后调用mergeSort()函数对每个分割的数组进行排序,再调用merge()函数对排序后的数组进行合并,最后调用printArray()函数将结果数组作为输出显示在屏幕上。

例子#2

C++程序演示合并排序技术,通过实现merge()函数和mergeSort()函数对一个给定的输入数组进行排序,然后在屏幕上显示结果数组的输出。

#include <iostream>
using namespace std;
//defining the merge function to merge the two sorted halves of the given input array
void merge(int array[], int left, int middle, int right)
{
//dividing the given array into two halves
int fh = middle - left + 1;
int sh = right - middle;
//creating two temporary arrays
int t1[fh], t2[sh];
//copying the data from the two halves into two temporary arrays
for (int m = 0; m < fh; m++)
t1[m] = array[left + m];
for (int n = 0; n < sh; n++)
t2[n] = array[middle + 1 + n];
//merging the temporary arrays into a single array
int a = 0;
int b = 0;
int c = left;
while (a < fh && b < sh) {
if (t1[a] <= t2[b]) {
array[c] = t1[a];
a++;
}
else {
array[c] = t2[b];
b++;
}
c++;
}
while (a < fh) {
array[c] = t1[a];
a++;
c++;
}
while (b < sh) {
array[c] = t2[b];
b++;
c++;
}
}
//sorting each of the divided arrays using mergeSort() function
void mergeSort(int array[],int left,int right){
if(left>=right){
return;
}
int middle =left+ (right-left)/2;
mergeSort(array,left,middle);
mergeSort(array,middle+1,right);
merge(array,left,middle,right);
}
// function to print the resulting array
void printArray(int Array[], int size)
{
for (int d = 0; d < size; d++)
cout << Array[d] << " ";
}
// defining the main function
int main()
{
int array[] = { 100, 50, 20, 80, 10, 40 };
int array_size = sizeof(array) / sizeof(array[0]);
cout << "The elements of the input array before sorting are: \n";
printArray(array, array_size);
mergeSort(array, 0, array_size - 1);
cout << "\nThe elements of the input array after sorting are: \n";
printArray(array, array_size);
return 0;
}

上述程序的输出显示在下面的快照中。

output 2

在上面的程序中,我们已经找到了中间点,它将给定的输入数组分成了两部分。然后我们定义了merge()、mergeSort()和printArray()函数。然后我们定义了主函数,在该函数中,我们对给定的数组进行分割,然后调用mergeSort()函数对每个分割的数组进行排序,再调用merge()函数对排序后的数组进行合并,最后调用printArray()函数将结果数组作为输出显示在屏幕上。