#include<stdio.h>
#include<stdlib.h>
#define less(A,B) A<B
#define coswap if(less(A,B)){Item t = A;A = B;B = t}
void twosort(int a[],int b [] ,int N,int r)//Here, N is the number of elements to be sorted in each array, r is the number of arrays, and then the multiplication of these two numbers is equal to the total array length
{
//Because it is bottom-up, the number of elements to be compared each time is 2, but the part to be compared is 4 times. That is, this condition is met: the number of elements to be compared multiplied by the part to be compared is equal to the total number of elements of the array.
int one = 0; //Represents the first element coordinate of the first subsequence
int two = N;//Represents the coordinates of the first element of the second array
int thr = 0;//Subscript array b
int cno ;//Counter of the first subsequence
int cnt ;
for(int i=0;i<r;i++){
cnt = 0;
cno = 0;
printf("The first%d Coordinates of the second comparison:%d %d \n\n",i+1,one,two);
while(cnt<N && cno<N){
if(less(a[one],a[two])) {
b[thr++] = a[one++];
cno++;
}
else{
b[thr++] = a[two++];
cnt++;
}
}
if(cnt>=N)while(cno<N){
b[thr++] = a[one++];
cno++;
}else{
while(cnt<N){
b[thr++] = a[two++];
cnt++;
}
}
one = one+N;//It should be noted that each time the traversal is completed, the number of comparison elements corresponding to the subscript displacement of the array in the first part is N; the same is true for the array elements in the second part
two = one + N;//There is no need to pass the superscript and subscript of each subsequence as before.
for(int v=0;v<16;v++)printf(" %d ",b[v]);//This is convenient for inspection and can be deleted.
printf("\n");
}
}
void twofen(int a[],int b[],int K)
{
int r;
for(int N = 1;N<K;N*= 2){
r = (K/N)/2;
twosort(a,b,N,r);
for(int i=0;i<16;i++)a[i] = b[i];//b before the last sort is ordered
}
}
int main()
{
int a[16] = {1,5,3,7,2,6,4,9,11,10,22,15,16,13,25,19};
int b[17] = {0};
twofen(a,b,16);
}
#include<stdio.h>
#include<stdlib.h>
#define key(A) A
#define less(A,B) A<B
#define Number 29
int sort(int a[],int b[],int First,int Fnum,int Last,int Lnum)
{
int i = First;
while(Fnum>0 && Lnum > 0){//After the elements in a sequence are taken, another sequence element is added.
if(less(a[First],a[Last])){
b[i++] = a[First++];
Fnum--;
}else{
b[i++] = a[Last++];
Lnum--;
}
}
if(Fnum>0){
while(Fnum>0){
b[i++] = a[First++];
Fnum--;
}
}
else{
while(Lnum>0) {
b[i++] = a[Last++];
Lnum--;
}
}
}
void copy(int a[],int b[],int position)
{
int i = position;
int j = i;
while(b[j]){
a[i++] = b[j++];
}
}
int huafen(int a[],int b[],int N,int position)//N is the subscript of the maximum element value in the array, and position is the starting position of scanning.
{
int First;//The first coordinate of the previous array
int Last;//The first coordinate of the next array
int Fnum; //The number of elements that hold the first segment of the array
int Lnum;//The number of elements that hold the following array
int cnt = 1;//Counter
int flag = 0;//Judge whether First is assigned
int Llag = 0;//Judge whether First is assigned
int segments = 1;
for(int i=position;i<N;i++){
if(less(a[i],a[i+1]) && i != N-1){
cnt++;
}
else{
if(flag == 0 && i != N-1){
First = i-cnt+1;//Add 1 here because cnt starts from 1, while First and Last start from 0
Fnum = cnt;
printf("\nFirst == %d Fnum == %d\n",First,Fnum);
flag = 1;//Indicates that a value has been assigned
cnt = 1;//Restart counting
segments++;
}else{
Last = i-cnt + 1;
Lnum = cnt;
printf("\nLast == %d Lnum == %d\n",Last,Lnum);
cnt = 1;//Re count
Llag = 1;
segments++;
}
if(Llag == 1 && flag == 1){
sort(a,b,First,Fnum,Last,Lnum);
flag = 0;//After sorting, the flag value is set to zero
Llag = 0;
copy(a,b,First);
}
}
}
return segments;
}
void AddNature(int a[],int b[],int N,int position)
{
int k = huafen(a,b,N,position); //When the array is merged for the first time, the number of subsequence segments is obtained at the same time. According to the number of subsequence segments, you can know how many times the array needs to be cycled
int bour;
int i=2;//Used to index the array when outputting
int Num = 0; //Record how many scans are required
float segments = k;
while(segments>1){
segments = segments/2;
bour = segments;
if(segments > bour){//When the subsequence segments are odd, it is also prone to special cases, that is, the elements of the previous sequence are just smaller than those of the latter subsequence, which will be reduced virtually
//An arrangement was made.
bour++;
segments = bour;
}
Num++;
}
for(int i=0;i<Num-1;i++){
huafen(a,b,N,position);
}
}
//The above is the improved natural sorting array
//The following is an array for sorting 2^N elements
void twosort(int a[],int b[],int N,int r)//Here, N is the number of elements to be sorted in each array, r is the number of arrays, and then the multiplication of these two numbers is equal to the total array length
{
//Because it is bottom-up, the number of elements to be compared each time is 2, but the part to be compared is 4 times. That is, this condition is met: the number of elements to be compared multiplied by the part to be compared is equal to the total number of elements of the array.
int one = 0;
int two = N;
int thr = 0;
int cno ;
int cnt ;
for(int i=0;i<r;i++){
cnt = 0;
cno = 0;
printf("The first%d Coordinates of the second comparison:%d %d \n\n",i+1,one,two);
while(cnt<N && cno<N){
if(less(a[one],a[two])) {
b[thr++] = a[one++];
cno++;
}
else{
b[thr++] = a[two++];
cnt++;
}
}
if(cnt>=N)while(cno<N){
b[thr++] = a[one++];
cno++;
}else{
while(cnt<N){
b[thr++] = a[two++];
cnt++;
}
}
one = one+N;//It should be noted that each time the traversal is completed, the number of comparison elements corresponding to the subscript displacement of the array in the first part is N; the same is true for the array elements in the second part
two = one + N;//There is no need to pass the superscript and subscript of each subsequence as before.
for(int v=0;v<16;v++)printf(" %d ",b[v]);
printf("\n");
}
}
void twofen(int a[],int b[],int K)
{
int r;
for(int N = 1;N<K;N*= 2){
r = (K/N)/2;
twosort(a,b,N,r);
for(int i=0;i<16;i++)a[i] = b[i];//b before the last sort is ordered
}
}
int main()
{
int a[Number] = {20,1,10,3,6,9,5,36,78,96,23,19,64,28,99,45,12,5,24,44,56,23,32,54,76,54,49,39,67};
int b[Number] = {0};
int two = 1;
for(;two<Number;two = two*2){
}
two = two /2;
twofen(a,b,two);
AddNature(a,b,Number,two);
int i=0;
int k=0;
while(b[i])printf(" %d ",b[i++]);
//Because a is missing a copy, here we switch the positions of a and b
sort(b,a,0,two,two,Number - two);
printf("\n");
while(a[k])printf(" %d ",a[k++]);
}