C language learning (13) insertion sort, heap sort, merge sort

I have a deep understanding of the sorting method taught by the teacher. I haven’t had time to explain it all, but I am gradually improving it. I would like to encourage you!

#include <stdio.h>
#define MAX 10

typedef int ARR[MAX];
typedef int Elementype;
void swap(ARR arr,int i,int j);//Swap the value of the parameter
void insert(ARR arr);//Insert sort
void shell(ARR arr);// Hill sort
void print(ARR arr);//Print function
void heap(ARR arr);//Heap sort body
void heapadjust(ARR arr,int n,int m);//Heap sort initialization function

void mergesort2(ARR arr,int left,int right)//recursive merge sort

void swap(ARR arr,int i,int j)//交换函数
{
    int temp = arr[i];
    arr[i] = arr[j];
    arr[j] = temp;
}

void insert(ARR arr)//Insertion sort
{     int i,j;     for(i = 1;i < MAX;i++)     {         if(arr[i-1] > arr[i])//Judge the difference between this bit and the previous The size of one bit         {             Elementype temp = arr[i];//Record the position to be determined             for(j = i - 1;j >= 0;j--)             {                 if(arr[j] > temp)                     arr[j +1] = arr[j];//If the previous position is greater than the next position, overwrite the previous bit to the next bit, and loop this judgment                 else                      break;             }             arr[j + 1] = temp;//Until Not greater than the position to be judged, assign it to the next bit of this position             print(arr);         }     } }

















void shell(ARR arr)//Hill sorting, based on insertion sort, regroup the arrays in sequence, perform insertion sorting within the group, and then sort under fewer groups until the number of groups is one (group The number is 2^n+ or -1, that is, 1,3,5,7,9,15,17....)
{     int a[] = {1,3,5};//record the number of groups     int k = 2;     int step = a[k];//Use a different number of groups in the next loop     int i,j;     while(k >= 0)     {         for(i = step;i < MAX;i++)         {             if (arr [i - step] > arr[i])//If divided into five groups, the 0th and 5th positions are a group, sorted, the 1st and 6th positions are a group, sorted.... .             {                 Elementype temp = arr[i];                 for(j = i-step;j >= 0;j -= step)//The step size is the number of groups. Two elements in the same group are separated by step. Use insertion sort to determine Thoughts                 {                     if(arr[j] > temp)                     {















                        arr[j + step] = arr[j];
                    }
                    else
                        break;
                }
                arr[j+step] = temp;
            }
        }
        step = a[--k];//Carry out insertion sorting of the next group number
        print(arr );
    }
}

void heapadjust(ARR arr,int n,int m)
{     Elementype temp = arr[n];//Record the value of the parent     int i;     for(i = 2*n+1;i <= m;i = 2*i +1)     {         if(i < m && arr[i] < arr[i + 1])//Compare the size of the left and right subtrees             i++;         if(arr[i] < temp)//Compare the larger one with the parent             break ;         arr[n] = arr[i];//If the child is large, the child overwrites the parent         n = i;     }     arr[n] = temp;//Puts the smaller parent's value at the child's position }












void heap(ARR arr)
{     int i;     for(i = (MAX-2)/2;i >= 0;i--)//Initialization     {         heapadjust(arr,i,MAX-1);//Heap processing , eventually forming a large top heap     }     print(arr);     printf("**************\n");     for(i = MAX-1;i > 0;i--)     {         swap(arr,0,i);//Put the maximum value to the end         heapadjust(arr,0,i-1);//Reduce the range of the tree to form a large top heap again (excluding the maximum value)         print(arr);     } }













void meige(ARR arr,int left,int mid,int right)//Merge
{     int len ​​= right-left+1;     Elementype temp[len];     int k = 0;     int i = left;//of the previous group The first     int j = mid+1;//The first     while(i <= mid && j <= right)     {         if(arr[i] < arr[j])         {             temp[k++] = arr[i++];//Compare the first number, put the smaller one into the array, and then take the next number for comparison}         else         {         temp             [k++] = arr[j++];//For example, the first number is higher than the next one The first one in the group is small. After it is put into the array, i++ compares the latter one with the next group. Since the sorting within the group is completed, only the numbers between groups are sorted} }         while     (     i <= mid)     {         temp[ k++] = arr[i++];//Put the remaining numbers into the array     }




















    while(j <= right)
    {         temp[k++] = arr[j++];//Put the remaining numbers into the array     }     for(i = 0;i < len;i++)         arr[left+i] = temp[ i];//Put the temporary array into the original array         }




void mergesort1(ARR arr)//Non-recursive merge sort
{     int left,right,mid;     int i;     for(i = 1;i < MAX;i *= 2)         {         left = 0;         while(left + i < MAX)//The constraint is that at least one group can be formed in the array for sorting         {             mid = left+i-1;//i is the length of each group, mid is the tail of the first group             right = (mid+i )<(MAX-1)?(mid+i):(MAX-1);//right is the tail mark of the second group             merge(arr,left,mid,right);//Call the merge function to merge two consecutive The size arrangement between groups, the first time is the sorting of adjacent elements, the second time is the sorting of two groups, the sorting of groups             left = right+1;         }         print(arr);     } }














void mergesort2(ARR arr, int left, int right)//Recursive merge sort, the idea is to merge and sort within the group, and then merge and sort between groups { if (left == right)
{     return     ;         }     int     mid = (left+right)/2;     mergesort2(arr,left,mid);//The left half group is sorted within the group, and the sorting between elements is gradually called and the return value is called     mergesort2(arr, mid+1, right);     merge(arr,left,mid,right);//Call the merge sort function     print(arr); }










 

Guess you like

Origin blog.csdn.net/ls_dashang/article/details/81516014