Non-recursive algorithm merge sort of

Original link: http://www.cnblogs.com/arcfat/archive/2012/12/06/2805701.html

8645 merge sort (non-recursive algorithm)

Time limit: 1000MS Memory Limit: 1000K

Questions: Programming language title: Unlimited

 

Description

Results achieved merge sort (non-recursive algorithm) by the function, and outputs the sorted per trip

 

Input

The first line: the keyboard input to be sorted key number n of 
the second row: the n-th input key to be sorted, the data separated by spaces

Output

Each line of output results sorted per trip, separated by a space between the data

 

Sample Input

10
5 4 8 0 9 3 2 6 7 1

 

Sample Output

4 5 0 8 3 9 2 6 1 7
0 4 5 8 2 3 6 9 1 7
0 2 3 4 5 6 8 9 1 7
0 1 2 3 4 5 6 7 8 9

 

// The following is the code AC

#include<iostream>
using namespace std;

Process void (n-int);
void mergesort (ori int [], int n-);
void newMerge (ori int [], int tmpArray [], int S, n-int); // no time to complete the merge, with the function merging two sorted array adjacent
void merge (int ori [], int tmpArray [], int left, int mid, int right); // merge two rows of arrays are sorted into tmpArray the
void Output ( int op [], int n) ; // for output array

int main()
{
  int n;
  cin >> n;
  Process(n);
  return 0;
}

Process void (n-int)
{
  int ori [n-+. 1];
  for (int I =. 1; I <= n-; I ++)
    CIN >> ori [I];
  IF (n->. 1) // If more than one the elements to be ranked, to merge
    mergeSort (ori, n); // call the original mergesort
  the else
    the Output (ori, n-);
}

mergesort void (ori int [], int n-)
{
  int = S. 1, tmpArray [n-+. 1]; // S is the length of time each way merge two arrays, an array (array length that may last less than S)
  the while (S <n-)
  {
    newMerge (ori, tmpArray, S, n-);
    S = 2 *;
  }
}

newMerge void (ori int [], int tmpArray [], int S, n-int)
{
  // If not complete the merge, this function with merging two sorted arrays adjacent
  int I =. 1;
  the while (I < = n - 2 * s + 1 ) // use i + 2s - 1 <= n can be derived
  {
    the Merge (ori, tmpArray, I, S I + -. 1, I 2 * S + -. 1);
    I = + s * 2;
  }
  IF (I <= n-- s) // i.e., the last two in the back of insufficient length s of
    the Merge (ori, tmpArray, I, I s + -. 1, n-);
  the else // That is integrated into the end, only an insufficient length of s, and simply copied directly into tmpArray [] in
    for (; I <= n-; I ++)
      tmpArray [I] = ori [I];

  // This completes trip merge
  // back again copy ori [] in, to code here Wallpaper
  for (int = 0 K; K <= n-; ++ K)
    ori [K] = tmpArray [K];

  the Output (ori, n-);
}

the Merge void (ori int [], int tmpArray [], int left, int MID, int right)
{
  // merge two rows of arrays are sorted to tmpArray [], and then back to the copy ori [] of
  int tmpCnt = left; // temporary counter
  int rightStart = mid + 1; // mid actually leftEnd
  the while (left <= MID && Rightstart <= right)
  {
    IF (ori [left] <= ori [Rightstart])
      tmpArray [tmpCnt ++] = ori [left ++];
    the else
      tmpArray [tmpCnt ++] = ori [Rightstart ++];
  }
  // following whom plurality who put directly into tmpArray [] in
  the while (left <= MID)
    tmpArray [tmpCnt ++] = ori [left ++ ];

  the while (Rightstart <= right)
    tmpArray [tmpCnt ++] = ori [Rightstart ++];

}

void Output(int op[],int n)
{
  for(int i = 1; i <= n; ++i)
  cout << op[i] << " ";
  cout << endl;
}

Reproduced in: https: //www.cnblogs.com/arcfat/archive/2012/12/06/2805701.html

Guess you like

Origin blog.csdn.net/weixin_30703911/article/details/94789594