Merge Sort - "Data Structures and Algorithms"

Hello everyone from CSDN, today, the content of Xiaoyalan is still the sorting of the data structure and algorithm column. Next, let us enter the world of merge sorting! ! !


merge sort

Merge sort (MERGE-SORT) is an effective sorting algorithm based on the merge operation, which is a very typical application of divide and conquer (Divide and Conquer). Combine the ordered subsequences to obtain a completely ordered sequence; that is, first make each subsequence in order, and then make the subsequence segments in order. Merging two sorted lists into one sorted list is called a two-way merge. Merge sort core steps:

 

void _MergeSort(int* a, int begin, int end, int* tmp)
{
	if (begin >= end)
	{
		return;
	}
	int mid = (begin + end) / 2;
	//[begin,mid] [mid+1,end]
	_MergeSort(a, begin, mid, tmp);
	_MergeSort(a, mid + 1, end, tmp);
	//归并两个区间
	int begin1 = begin;
	int begin2 = mid + 1;
	int end1 = mid;
	int end2 = end;
	int i = begin;
	while (begin1 <= end1 && begin2 <= end2)
	{
		if (a[begin1] < a[begin2])
		{
			tmp[i++] = a[begin1++];
		}
		else
		{
			tmp[i++] = a[begin2++];
		}
	}
	while (begin1 <= end1)
	{
		tmp[i++] = a[begin1++];
	}
	while (begin2 <= end2)
	{
		tmp[i++] = a[begin2++];
	}
	memcpy(a + begin, tmp + begin, sizeof(int) * (end - begin + 1));
}
//归并排序
void MergeSort(int* a, int n)
{
	int* tmp = (int*)malloc(sizeof(int) * n);
	_MergeSort(a, 0, n - 1, tmp);
	free(tmp);
}

 

 Test merge sort:

void TestMergeSort()
{
    int a[] = { 2,1,4,3,6,5,7,9,8,10 };
    PrintArray(a, sizeof(a) / sizeof(a[0]));
    MergeSort(a, sizeof(a) / sizeof(a[0]));
    PrintArray(a, sizeof(a) / sizeof(a[0]));
}

 

Summary of the characteristics of merge sort:

  1. The disadvantage of merging is that it requires O(N) space complexity, and the thinking of merging and sorting is more to solve the problem of external sorting in the disk.
  2. Time complexity: O(N*logN)
  3. Space complexity: O(N)
  4. Stability: Stable

Merge sort non-recursive

void MergeSortNonR(int* a, int n)
{
	int* tmp = (int*)malloc(sizeof(int) * n);
	if (tmp == NULL)
	{
		perror("malloc失败!!!");
		return;
	}
	int gap = 1;
	while (gap < n)
	{
		int j = 0;
		for (int i = 0; i < n; i += gap)
		{
			//每组的合并数据
			int begin1 = i;
			int end1 = i + gap - 1;
			int begin2 = i + gap;
			int end2 = i + 2 * gap - 1;
			while (begin1 <= end1 && begin2 <= end2)
			{
				if (a[begin1] < a[begin2])
				{
					tmp[j++] = a[begin1++];
				}
				else
				{
					tmp[j++] = a[begin2++];
				}
			}
			while (begin1 <= end1)
			{
				tmp[j++] = a[begin1++];
			}
			while (begin2 <= end2)
			{
				tmp[j++] = a[begin2++];
			}

		}
		memcpy(a, tmp, sizeof(int) * n);
		gap *= 2;
	}
	free(tmp);
}

But this code has a very serious out-of-bounds problem. Only when there is data to the power of 2, it will not out-of-bounds! ! !

Xiao Yalan prints several sets of data here to see more clearly:

 

 

void MergeSortNonR(int* a, int n)
{
	int* tmp = (int*)malloc(sizeof(int) * n);
	if (tmp == NULL)
	{
		perror("malloc失败!!!");
		return;
	}
	// 1  2  4 ....
	int gap = 1;
	while (gap < n)
	{
		int j = 0;
		for (int i = 0; i < n; i += 2 * gap)
		{
			// 每组的合并数据
			int begin1 = i;
			int end1 = i + gap - 1;
			int begin2 = i + gap;
			int end2 = i + 2 * gap - 1;

			printf("[%d,%d][%d,%d]\n", begin1, end1, begin2, end2);

			if (end1 >= n || begin2 >= n)
			{
				break;
			}

			// 修正
			if (end2 >= n)
			{
				end2 = n - 1;
			}

			while (begin1 <= end1 && begin2 <= end2)
			{
				if (a[begin1] < a[begin2])
				{
					tmp[j++] = a[begin1++];
				}
				else
				{
					tmp[j++] = a[begin2++];
				}
			}

			while (begin1 <= end1)
			{
				tmp[j++] = a[begin1++];
			}

			while (begin2 <= end2)
			{
				tmp[j++] = a[begin2++];
			}

			// 归并一组,拷贝一组
			memcpy(a + i, tmp + i, sizeof(int) * (end2 - i + 1));
		}
		printf("\n");
		gap *= 2;
	}
	free(tmp);
}

 Just fix it like this! ! !

 

There is a second solution to this out-of-bounds problem:

void MergeSortNonR(int* a, int n)
{
	int* tmp = (int*)malloc(sizeof(int) * n);

	// 1  2  4 ....
	int gap = 1;
	while (gap < n)
	{
		int j = 0;
		for (int i = 0; i < n; i += 2 * gap)
		{
			// 每组的合并数据
			int begin1 = i, end1 = i + gap - 1;
			int begin2 = i + gap, end2 = i + 2 * gap - 1;

			printf("修正前:[%d,%d][%d,%d]\n", begin1, end1, begin2, end2);

			if (end1 >= n)
			{
				end1 = n - 1;

				// 不存在区间
				begin2 = n;
				end2 = n - 1;
			}
			else if (begin2 >= n)
			{
				// 不存在区间
				begin2 = n;
				end2 = n - 1;
			}
			else if(end2 >= n)
			{
				end2 = n - 1;
			}

			printf("修正后:[%d,%d][%d,%d]\n", begin1, end1, begin2, end2);


			while (begin1 <= end1 && begin2 <= end2)
			{
				if (a[begin1] <= a[begin2])
				{
					tmp[j++] = a[begin1++];
				}
				else
				{
					tmp[j++] = a[begin2++];
				}
			}

			while (begin1 <= end1)
			{
				tmp[j++] = a[begin1++];
			}

			while (begin2 <= end2)
			{
				tmp[j++] = a[begin2++];
			}
		}
		printf("\n");

		memcpy(a, tmp, sizeof(int) * n);
		gap *= 2;
	}

	free(tmp);
}

 

 


Test various sorts

// 测试排序的性能对比
void TestOP()
{
	srand(time(0));
	const int N = 1000000;
	int* a1 = (int*)malloc(sizeof(int) * N);
	int* a2 = (int*)malloc(sizeof(int) * N);
	int* a3 = (int*)malloc(sizeof(int) * N);
	int* a4 = (int*)malloc(sizeof(int) * N);
	int* a5 = (int*)malloc(sizeof(int) * N);
	int* a6 = (int*)malloc(sizeof(int) * N);
	int* a7 = (int*)malloc(sizeof(int) * N);

	for (int i = 0; i < N; ++i)
	{
		a1[i] = rand();
		a2[i] = a1[i];
		a3[i] = a1[i];
		a4[i] = a1[i];
		a5[i] = a1[i];
		a6[i] = a1[i];
		a7[i] = a1[i];
	}
	int begin1 = clock();
	InsertSort(a1, N);
	int end1 = clock();

	int begin2 = clock();
	ShellSort(a2, N);
	int end2 = clock();

	int begin3 = clock();
	SelectSort(a3, N);
	int end3 = clock();

	int begin4 = clock();
	HeapSort(a4, N);
	int end4 = clock();

	int begin5 = clock();
	QuickSort(a5, 0, N - 1);
	int end5 = clock();

	int begin6 = clock();
	MergeSort(a6, N);
	int end6 = clock();

	int begin7 = clock();
	BubbleSort(a7, N);
	int end7 = clock();

	printf("InsertSort:%d\n", end1 - begin1);
	printf("ShellSort:%d\n", end2 - begin2);
	printf("SelectSort:%d\n", end3 - begin3);
	printf("HeapSort:%d\n", end4 - begin4);
	printf("QuickSort:%d\n", end5 - begin5);
	printf("MergeSort:%d\n", end6 - begin6);
	printf("BubbleSort:%d\n", end7 - begin7);


	free(a1);
	free(a2);
	free(a3);
	free(a4);
	free(a5);
	free(a6);
	free(a7);
}

 

 

 

All sorting source code:

Contents of Sort.h:

#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<stdbool.h>
#include<string.h>


void PrintArray(int* a, int n);


// Direct insertion sorting
void InsertSort(int* a, int n);

// Shell sorting
void ShellSort(int* a, int n);

// direct selection sorting
void SelectSort(int* a, int n);

// 堆排序
void AdjustDown(int* a, int n, int root);
void HeapSort(int* a, int n);

// Bubble sort
void BubbleSort(int* a, int n);

//快速排序
int PartSort1(int* a, int left, int right);
int PartSort2(int* a, int left, int right);
int PartSort3(int* a, int left, int right);
void QuickSort(int* a, int begin, int end);

void QuickSortNonR(int* a, int begin, int end);

//merge sort
void MergeSort(int* a, int n);

void MergeSortNonR(int* a, int n);

 Contents of Sort.c:

#include"Sort.h"
#include"Stack.h"
void PrintArray(int* a, int n)
{
    int i = 0;
    for (i = 0; i < n; i++)
    {
        printf("%d ", a[i]);
    }
    printf("\n");
}


//Direct insertion sort
void InsertSort(int* a, int n)
{     int i = 0;     for (i = 1; i < n; i++)     {         int end = i - 1;         int tmp = a[i];         while (end >= 0)         {             //The inserted data is smaller than the original data             if (a[end] > tmp)             {                 a[end + 1] = a[end];                 --end;             }             else             {                 break;             }         }         a[end + 1] = tmp;     } }





















//Shell sorting
void ShellSort(int* a, int n)
{     //1.gap>1, pre-sorting     //2.gap==1, direct insertion sorting     int gap = n;     while (gap > 1)     {         gap = gap / 3 + 1;         //+1 can guarantee that the last time must be 1         for (int i = 0; i < n - gap; i++)         {             int end = i;             int tmp = a[end + gap];             while (end >= 0)             {                 if (a[end] > tmp)                 {                     a[end + gap] = a[end];                     end = end - gap;                 }                 else                 {




















                    break;
                }
            }
            a[end + gap] = tmp;
        }
    }
}


//冒泡排序
void BubbleSort(int* a, int n)
{
    for (int j = 0; j < n; j++)
    {
        bool exchange = false;
        for (int i = 1; i < n - j; i++)
        {
            if (a[i - 1] > a[i])
            {
                int tmp = a[i];
                a[i] = a[i - 1];
                a[i - 1] = tmp;
                exchange = true;
            }
        }
        if (exchange == false)
        {
            break;
        }
    }
}


void Swap(int* a1, int* a2)
{
    int tmp = *a1;
    *a1 = *a2;
    *a2 = tmp;
}

//Direct selection sort
void SelectSort(int* a, int n)
{     int begin = 0;     int end = n - 1;     while (begin < end)     {         int maxi = begin;         int mini = begin;         for (int i = begin; i <= end; i++)         {             if (a[i] > a[maxi])             {                 maxi = i;             }             if (a[i] < a[mini])             {                 mini = i;             }         }         Swap(&a [begin], &a[mini]);         //If maxi and begin overlap, just fix it         if (begin ==maxi)         {





















            maxi = mini;
        }
        Swap(&a[end], &a[maxi]);
        ++begin;
        --end;
    }
}

//Down adjustment algorithm
void AdjustDown(int* a, int n, int parent)
{     //The default left child is small     int child = parent * 2 + 1;     while (child < n)//The child is within the range of the array     {         / /Select the larger one of the left and right children         //It may be wrong to assume that         //The left child does not exist, there must be no right child-complete binary tree         //The left child exists, there may be no right child         if (child + 1 < n && a[child + 1] > a[child])             // right child exists right child > left child             // can't write like this if (a[child + 1] > a[chid] && child + 1 < n )             // There is a risk of going out of bounds when writing this way because the elements in the array are first accessed and then compared to see if the right child exists         {             ++child;         }         //child is the older child         //I don’t care whether it is the left child or the right child 

















        if (a[child] > a[parent])
        {             Swap(&a[child], &a[parent]);             parent = child;             child = parent * 2 + 1;//The default is the left child         }         else         {             break;         }







    }
}
//HeapSort
void HeapSort(int* a, int n)
{     //Create a heap——adjust the heap downwards     int i = 0;     for (i = (n - 1 - 1) / 2; i >= 0; i--)     {         AdjustDown(a, n, i);     }     //Ascending order - build a large pile     int end = n - 1;     while (end > 0)     {         Swap(&a[0], &a[end] );         AdjustDown(a, end, 0);         --end;     } }














//三数取中
int GetMidIndex(int* a, int left, int right)
{
    int mid = (left + right) / 2;
    if (a[left] < a[mid])
    {
        if (a[mid] < a[right])
        {
            return mid;
        }
        else if (a[left] < a[right])
        {
            return right;
        }
        else
        {
            return left;
        }
    }
    else // a[left] > a[mid]
    {
        if (a[mid] > a[right])
        {
            return mid;
        }
        else if (a[left] > a[right])
        {
            return right;
        }
        else
        {
            return left;
        }
    }
}
// hoare
// [left, right]
int PartSort1(int* a, int left, int right)
{
    int midi = GetMidIndex(a, left, right);
    Swap(&a[left], &a[midi]);

    int keyi = left;
    while (left < right)
    {
        // 右边找小
        while (left < right && a[right] >= a[keyi])
        {
            --right;
        }

        // 左边找大
        while (left < right && a[left] <= a[keyi])
        {
            ++left;
        }

        Swap(&a[left], &a[right]);
    }

    Swap(&a[keyi], &a[left]);

    return left;
}


挖坑法
[left, right]
//int PartSort2(int* a, int left, int right)
//{
//    int midi = GetMidIndex(a, left, right);
//    Swap(&a[left], &a[midi]);
//
//    int key = a[left];
//    int hole = left;
//    while (left < right)
//    {
//        // 右边找小
//        while (left < right && a[right] >= key)
//        {
//            --right;
//        }
//
//        a[hole] = a[right];
//        hole = right;
//
//        // 左边找大
//        while (left < right && a[left] <= key)
//        {
//            ++left;
//        }
//
//        a[hole] = a[left];
//        hole = left;
//    }
//
//    a[hole] = key;
//
//    return hole;
//}
//
前后指针法
[left, right]
//int PartSort3(int* a, int left, int right)
//{
//    int midi = GetMidIndex(a, left, right);
//    Swap(&a[left], &a[midi]);
//
//    int prev = left;
//    int cur = left + 1;
//    int keyi = left;
//    while (cur <= right)
//    {
//        if (a[cur] < a[keyi] && ++prev != cur)
//        {
//            Swap(&a[prev], &a[cur]);
//        }
//
//        ++cur;
//    }
//
//    Swap(&a[prev], &a[keyi]);
//    keyi = prev;
//    return keyi;
//}
//快速排序
void QuickSort(int* a, int begin, int end)
{
    if (begin >= end)
    {
        return;
    }
    int keyi = PartSort1(a, begin, end);
    //[begin,keyi-1] keyi [keyi+1,end]
    QuickSort(a, begin, keyi - 1);
    QuickSort(a, keyi + 1, end);
}


//Quick sort non-recursive
void QuickSortNonR(int* a, int begin, int end)
{     Stack st;     StackInit(&st);     StackPush(&st, end);     StackPush(&st, begin);



    while (!StackEmpty(&st))
    {
        int left = StackTop(&st);
        StackPop(&st);

        int right = StackTop(&st);
        StackPop(&st);

        int keyi = PartSort1(a, left, right);

        // [left, keyi-1] keyi [keyi+1, right]

        if (keyi + 1 < right)
        {
            StackPush(&st, right);
            StackPush(&st, keyi + 1);
        }

        if (left < keyi - 1)
        {
            StackPush(&st, keyi - 1);
            StackPush(&st, left);
        }
    }

    StackDestroy(&st);
}


void _MergeSort(int* a, int begin, int end, int* tmp)
{
    if (begin >= end)
    {
        return;
    }
    int mid = (begin + end) / 2;
    //[begin,mid] [mid+1,end]
    _MergeSort(a, begin, mid, tmp);
    _MergeSort(a, mid + 1, end, tmp);
    //归并两个区间
    int begin1 = begin;
    int begin2 = mid + 1;
    int end1 = mid;
    int end2 = end;
    int i = begin;
    while (begin1 <= end1 && begin2 <= end2)
    {
        if (a[begin1] < a[begin2])
        {
            tmp[i++] = a[begin1++];
        }
        else
        {
            tmp[i++] = a[begin2++];
        }
    }
    while (begin1 <= end1)
    {
        tmp[i++] = a[begin1++];
    }
    while (begin2 <= end2)
    {
        tmp[i++] = a[begin2++];
    }
    memcpy(a + begin, tmp + begin, sizeof(int) * (end - begin + 1));
}
//归并排序
void MergeSort(int* a, int n)
{
    int* tmp = (int*)malloc(sizeof(int) * n);
    if (tmp == NULL)
    {
        perror("malloc失败!!!");
        return;
    }
    _MergeSort(a, 0, n - 1, tmp);
    free(tmp);
}

//merge sort non-recursive
void MergeSortNonR(int* a, int n)
{     int* tmp = (int*)malloc(sizeof(int) * n);

    // 1 2 4 ....
    int gap = 1;
    while (gap < n)
    {         int j = 0;         for (int i = 0; i < n; i += 2 * gap)         {             // for each group Merge data             int begin1 = i, end1 = i + gap - 1;             int begin2 = i + gap, end2 = i + 2 * gap - 1;





            printf("Before correction: [%d,%d][%d,%d]\n", begin1, end1, begin2, end2);

            if (end1 >= n)
            {
                end1 = n - 1;

                // There is no interval
                begin2 = n;
                end2 = n - 1;
            }
            else if (begin2 >= n)
            {                 // There is no interval                 begin2 = n;                 end2 = n - 1;             }             else if(end2 >= n)             {                 end2 = n - 1;             }







            printf("After correction: [%d,%d][%d,%d]\n", begin1, end1, begin2, end2);


            while (begin1 <= end1 && begin2 <= end2)
            {
                if (a[begin1] <= a[begin2])
                {
                    tmp[j++] = a[begin1++];
                }
                else
                {
                    tmp[j++] = a[begin2++];
                }
            }

            while (begin1 <= end1)
            {
                tmp[j++] = a[begin1++];
            }

            while (begin2 <= end2)
            {
                tmp[j++] = a[begin2++];
            }
        }
        printf("\n");

        memcpy(a, tmp, sizeof(int) * n);
        gap *= 2;
    }

    free(tmp);
}
//void MergeSortNonR(int* a, int n)
//{
//    int* tmp = (int*)malloc(sizeof(int) * n);
//    if (tmp == NULL)
//    {
//        perror("malloc失败!!!");
//        return;
//    }
//    // 1  2  4 ....
//    int gap = 1;
//    while (gap < n)
//    {
//        int j = 0;
//        for (int i = 0; i < n; i += 2 * gap)
//        {
//            // 每组的合并数据
//            int begin1 = i;
//            int end1 = i + gap - 1;
//            int begin2 = i + gap;
//            int end2 = i + 2 * gap - 1;
//
//            printf("[%d,%d][%d,%d]\n", begin1, end1, begin2, end2);
//
//            if (end1 >= n || begin2 >= n)
//            {
//                break;
//            }
//
//            // 修正
//            if (end2 >= n)
//            {
//                end2 = n - 1;
//            }
//
//            while (begin1 <= end1 && begin2 <= end2)
//            {
//                if (a[begin1] < a[begin2])
//                {
//                    tmp[j++] = a[begin1++];
//                }
//                else
//                {
// tmp[j++] = a[begin2++];
// }
// }
//
// while (begin1 <= end1)
// { // tmp[j++] = a[begin1++]; // } // / / while (begin2 <= end2) // { // tmp[j++] = a[begin2++]; // } // // // merge a group, copy a group // memcpy(a + i, tmp + i , sizeof(int) * (end2 - i + 1)); // } // printf("\n"); // gap *= 2; // } // free(tmp); //}
















Leetcode daily question - "912. Sorting arrays" 

There is a question on leetcode, you can use various sorts to test whether it can pass:

 

 Xiao Yalan tried the merge sort here, and it passed easily! ! !

void _MergeSort(int* a, int begin, int end, int* tmp)
{
	if (begin >= end)
	{
		return;
	}
	int mid = (begin + end) / 2;
	//[begin,mid] [mid+1,end]
	_MergeSort(a, begin, mid, tmp);
	_MergeSort(a, mid + 1, end, tmp);
	//归并两个区间
	int begin1 = begin;
	int begin2 = mid + 1;
	int end1 = mid;
	int end2 = end;
	int i = begin;
	while (begin1 <= end1 && begin2 <= end2)
	{
		if (a[begin1] < a[begin2])
		{
			tmp[i++] = a[begin1++];
		}
		else
		{
			tmp[i++] = a[begin2++];
		}
	}
	while (begin1 <= end1)
	{
		tmp[i++] = a[begin1++];
	}
	while (begin2 <= end2)
	{
		tmp[i++] = a[begin2++];
	}
	memcpy(a + begin, tmp + begin, sizeof(int) * (end - begin + 1));
}
//归并排序
void MergeSort(int* a, int n)
{
	int* tmp = (int*)malloc(sizeof(int) * n);
	if (tmp == NULL)
	{
		perror("malloc失败!!!");
		return;
	}
    
	_MergeSort(a, 0, n - 1, tmp);
	free(tmp);
}
int* sortArray(int* nums, int numsSize, int* returnSize){
  MergeSort(nums, numsSize);
  *returnSize = numsSize;
  return nums;
}

It can also be written like this, it is a version optimized between cells , which is relatively better, but this effect cannot be tested on leetcode:

//直接插入排序
void InsertSort(int* a, int n)
{
	int i = 0;
	for (i = 1; i < n; i++)
	{
		int end = i - 1;
		int tmp = a[i];
		while (end >= 0)
		{
			//插入的数据比原来的数据小
			if (a[end] > tmp)
			{
				a[end + 1] = a[end];
				--end;
			}
			else
			{
				break;
			}
		}
		a[end + 1] = tmp;
	}
}
void _MergeSort(int* a, int begin, int end, int* tmp)
{
	if (begin >= end)
	{
		return;
	}
    //小区间优化
    if(end-begin+1<10)
    {
        InsertSort(a+begin,end-begin+1);
        return;
    }
	int mid = (begin + end) / 2;
	//[begin,mid] [mid+1,end]
	_MergeSort(a, begin, mid, tmp);
	_MergeSort(a, mid + 1, end, tmp);
	//归并两个区间
	int begin1 = begin;
	int begin2 = mid + 1;
	int end1 = mid;
	int end2 = end;
	int i = begin;
	while (begin1 <= end1 && begin2 <= end2)
	{
		if (a[begin1] < a[begin2])
		{
			tmp[i++] = a[begin1++];
		}
		else
		{
			tmp[i++] = a[begin2++];
		}
	}
	while (begin1 <= end1)
	{
		tmp[i++] = a[begin1++];
	}
	while (begin2 <= end2)
	{
		tmp[i++] = a[begin2++];
	}
	memcpy(a + begin, tmp + begin, sizeof(int) * (end - begin + 1));
}

//归并排序
void MergeSort(int* a, int n)
{
	int* tmp = (int*)malloc(sizeof(int) * n);
	if (tmp == NULL)
	{
		perror("malloc失败!!!");
		return;
	}
	_MergeSort(a, 0, n - 1, tmp);
	free(tmp);
}
int* sortArray(int* nums, int numsSize, int* returnSize){
  MergeSort(nums,numsSize);
  *returnSize = numsSize;
  return nums;
}

 

 

 But for this question, you can't pass the sorting by direct insertion sorting and bubble sorting, and you will be prompted: time limit exceeded

 Unfortunately: the quick sort did not pass, and Xiao Yalan repeatedly tested it many times


Alright, this is the end of Xiao Yalan's merge and sort content for today, let's keep going! ! !

 

Guess you like

Origin blog.csdn.net/weixin_74957752/article/details/132084420