Last night a long time, there are always a few data a bit wrong, the original array is from 1 to save, did not see clearly ...
1 /*2 Please design the merge sorting algorithm function void mergesort (int a[],int n), on a[1]. A[n] in ascending order. 3 and test the efficiency of sorting at different data sizes. 4 */5#include"Arrayio.h"6 #defineN 10000/*n is the size of the data, because there are only 500,000 numbers in the Data1.txt, so it is necessary to set the N value to n<=500000*/7 8 /*please add this function to complete and test*/9 Ten voidMergeintA[],intUintMintv) One{/*a[u the ordered segment. M],A[M+1..V] Merge to A[u. V]*/ A intI, J, K; - ints[i+1]; -i = u, j = m +1, k =u; the while(I <= m && J <=v) - { - if(A[i] <=A[j]) - { +t[k++] = a[i++]; - } + Else A { att[k++] = a[j++]; - } - } - while(I <=m) -t[k++] = a[i++]; - while(J <=v) int[k++] = a[j++]; - for(i = u; I <= v; i++) toA[i] =T[i]; + } - the * /*----A trip to merge------*/ $ voidMergepass (intA[],intNintlen)Panax Notoginseng{/*a trip to A[1..N] with a length of Len and a return*/ - inti =1; the while(i+2*len-1<=N) + { AMerge (A,i, i + Len-1, i +2* Len-1); thei = i +2*Len; + } - if(i+len-1<N) $Merge (A,i, I +len-1, n); $ } - - /*----Merge sort------*/ the voidMergeSort (intA[],intN) - {Wuyi intLen =1; the while(len<=N) - { Wu Mergepass (A, n, Len); -Len =2*Len; About } $ } - - /*recursive implementation of merge sort*/ - voidMERGESORTDC (intA[],intLowintHigh ) A { + intmid; the if(low<High ) - { $Mid = (low + high)/2; the Mergesortdc (A,low, mid); theMERGESORTDC (A,mid +1, high); the merge (A, low,mid, high); the } - } in the intMain () the { About inta[n+1],n;/*data stored in A[1]...a[n]*/ theprintf"data initialization ... \ n"); theN=readdata (A,n,"Data1.txt");/*reads n integers from data1.txt into the array a,n the number of data actually read in*/ theprintf"%d data in sort ... \ n", n); +MergeSort (A,n);/*Mergesortdc (a,1,n);*/ -SaveData (A,n,"D:\\out.txt");/*sorting results are stored in the OUT.txt file*/ theprintf"The sort ends and the sorted results are saved in the OUT.txt file. \ n");Bayi return 0; the}
1#include <stdio.h>2#include <stdlib.h>3#include <time.h>4 #defineMAX 5000005 6 /*reading data from a file into array a*/7 intReadData (intA[],intNChar*F)/*function returns the number of data successfully read into*/8 {9FILE *FP;Ten inti; OneFp=fopen (F,"R"); A if(Fp==null)return 0; - Else - { the for(i=0; I<n &&!feof (FP); i++) -FSCANF (FP,"%d",&a[i]); - fclose (FP); - returni; + } - } + A /*disk function*/ at voidSaveData (intA[],intNChar*f) - { -FILE *FP; - inti; -Fp=fopen (F,"W"); - if(fp==null) printf ("file setup failed! "); in Else - { to for(i=0; i<n;i++) +{if(i%Ten==0) fprintf (FP,"%c",'\ n'); -fprintf (FP,"%8d", A[i]); the } * fclose (FP); $ }Panax Notoginseng } - the + /*an integer array with an output length of n*/ A voidOutputintA[],intN) the{inti; +printf"\ n The contents of the array are: \ n"); - for(i=0; i<n;i++) ${if(i%Ten==0) printf ("\ n"); $printf"%7d", A[i]); - } -printf"\ n"); the}
Recursive algorithm of merging sort and non-recursion