Merge sort of c-sort algorithm

Time:2021-4-6

This example for you to share the C # to achieve merge sort specific code, for your reference, the specific content is as follows

code:

//Merge sort (target array, starting position of sub table, ending position of sub table)
  private static void MergeSortFunction(int[] array, int first, int last)
  {
   try
   {
    If (first < last) // the length of the sub table is greater than 1, the following recursive processing will be performed
    {
     Int mid = (first + last) / 2; // location of sub table partition
     Mergesortfunction (array, first, mid); // recursively partition the left sub table
     Mergesortfunction (array, mid + 1, last); // recursively partition the right sub table
     Mergesortcore (array, first, mid, last); // orderly integrate the left and right sub tables (the core part of merge sort)
    }
   }
   catch (Exception ex)
   { }
  }
 
  //The core part of merge sort: merge two ordered left and right sub tables (distinguished by mid) into one ordered table
  private static void MergeSortCore(int[] array, int first, int mid, int last)
  {
   try
   {
    Int indexa = first; // starting position of left sub table
    Int indexb = mid + 1; // the starting position of the right sub table
    Int [] temp = New Int [last + 1]; // declare the array (temporarily store all ordered sequences of left and right sub tables): the length is equal to the sum of the lengths of the left and right sub tables.
    int tempIndex = 0;
    While (indexa < = mid & & indexb < = last) // traverses the left and right sub tables. If one of the sub tables has been traversed, it will jump out of the loop
    {
     If (array [indexa] < = array [indexb]) // at this time, the number of left sub tables < = the number of right sub tables
     {
      Temp [tempindex + +] = array [indexa + +]; // put the number of the left sub table into the temporary array and traverse the subscript of the left sub table++
     }
     Else // in this case, the number of left sub tables > the number of right sub tables
     {
      Temp [tempindex + +] = array [indexb + +]; // put the number of the right sub table into the temporary array and traverse the subscript of the right sub table++
     }
    }
    //After traversing one side of the sub table, jump out of the loop and put the remaining number of the other side of the sub table into the temporary storage array (orderly)
    while (indexA <= mid)
    {
     temp[tempIndex++] = array[indexA++];
    }
    while (indexB <= last)
    {
     temp[tempIndex++] = array[indexB++];
    }
 
    //Write the ordered sequence in the temporary storage array to the specified position of the target array to make the merged array segments orderly
    tempIndex = 0;
    for (int i = first; i <= last; i++)
    {
     array[i] = temp[tempIndex++];
    }
   }
   catch (Exception ex)
   { }
  }

The above is the whole content of this article, I hope to help you learn, and I hope you can support developer more.

Recommended Today

Lua language novice simple tutorial

1、 Foreword Lua is a lightweight and compact scripting language, which is written in standard C language and open in the form of source code. Its design purpose is to be embedded in the application, so as to provide flexible expansion and customization functions for the application. Lua can be applied in game development, independent […]