First, the purpose of the experiment: Compare The execution efficiency of at least 4 sorting algorithms. The algorithms that have been learned: bubble sorting, select sort, insert sort,shell Sort, merge sort, quick sort, etc. Second, the experimental requirements: 1, choose at least 4 of the sorting algorithm, written as a separate function to call. 2, participate in the sequencing of data not less than the Four, require data files to store randomly generated data. 3. Callthe above function in the main () function, and output the time used by the sorting algorithm. Third, the problem description: Through the design of at least four kinds of sorting algorithms, the function is realized, the time required to complete at least the number of the randomly generated is calculated, and the efficiency of the algorithms is compared. Four, algorithm analysis: Random number Deposit file: function void randtofile (int *a); Generate random numbers through the rand () function, and then use the file pointer *FP to save the array of generated random numbers in the file data.txt. Read random number from file: function void readfromfile (int a[]); through the file pointer *FP Read the file Data.txt the array in is used for subsequent operations. Quick sort: function void quick_sort (int *a,int low,int high); using recursive thought, the array is divided into two parts by the base of the center, and the recursive method is used to divide each part of the partition, knowing that each part has only one element and sorts each unit. fast sequencing is not stable. Optimal condition algorithm time complexity O (nlog2n), worst o (n2) Insert sort: function void insert_sort (int *a,int n); Assuming the first of the array set n ( n>=2 n direct insert sort is stable. Algorithmic time complexity o (n2)--[n squared Bubble sort: function void bubble_sort (int *a,int n); In the set of numbers to be sorted, the total number in the range that is not currently in sequence, the top-down pairs of adjacent two numbers are compared and adjusted sequentially, so that the larger number to sink, smaller upward. That is, each time a comparison of two adjacent numbers finds that they are in the opposite order of order, they are interchanged. The bubbling sort is stable. Algorithm time complexity O (n2)--[n squared ] Hill sort: function void shell_sort (int *a,int n); The algorithm first sorts the set of numbers by an increment D into groups, each group of records of the subscript difference D. sorts all the elements in each group, then uses a smaller increment to do it, and then sorts them in each group. When the increment is reduced to 1 , the entire number to be sorted is divided into a group, and the sort is completed. The hill sort is not stable. Select sort: function void select_sort (int *a,int n); In the set of numbers to be sorted, select the smallest number to exchange with the first position, and then in the remaining number, find the smallest and second position of the number of exchanges, so loop to the penultimate number and the last number comparison. Choosing a sort is not stable. Algorithm complexity O (n2)--[n squared ] Main function: In the main function, first call the random number to generate and save the function of the file, in the call from the file read the data function, and then, by including the header file <time.h> call clock function Clocks () Records start execution time and end time, subtracting execution time and displaying on the monitor. Within the main function, you can generate random numbers of different numbers and different ranges by modifying the value of the constant variable N, which verifies the accuracy of the execution time. Five, the code: 1. Random number deposit file (void randtofile (int *a)) generates n random numbers less than n , deposited in array a void randtofile (int *a) { Srand ((unsigned) time (NULL)); Initialize random number for (int i=0;i<n;i++) { a[i]= (int) rand ()%N; } FILE *FP; if ((Fp=fopen ("Data.txt", "W")) ==null) { printf ("\nerror on Open data.txt!"); Getch (); Exit (1); } for (i=0;i<n;i++) fprintf (FP, "%1d\n", A[i]); Fclose (FP); } 2. Reading data from a file (void readfromfile (int a[])) void ReadFromFile (int a[]) { int i=0; FILE *FP; if ((Fp=fopen ("Data.txt", "R")) ==null) { printf ("file read error!"); Exit (1); } while (!feof (FP)) { FSCANF (FP, "%d", &a[i++]); } Fclose (FP); } 3. Quick Sort (void quick_sort (int *a,int low,int high)) void Quick_sort (int *a,int low,int High) { int Partition (int *a,int low,int high); int mid; if (Low { Mid=partition (A,low,high); Quick_sort (a,low,mid-1);/* recursive call * / Quick_sort (A,mid+1,high); } } int Partition (int *a,int low,int High) { int mid,temp; int i=low,j=high+1; Mid=a[low]; while (I < j) { while ((A[++i] < mid) &&i while (A[--J]>MID); if (i>=j) break; Temp=a[i]; A[I]=A[J]; A[j]=temp; } A[LOW]=A[J]; Return J; } 4. Insert sort (void insert_sort (int *a,int n)) void Insert_sort (int *a,int N) { int i,j,temp; for (i=1;i<n;i++) { temp=a[i];/* the current element, save it in the other variables first * / for (j=i-1;j>=0&&a[j]>temp;j--) { a[j+1]=a[j];/* one side to move the element * / } A[j+1]=temp; } } 5. Bubble sort (void bubble_sort (int *a,int n)) void Bubble_sort (int *a,int N) { int i,j,temp; for (i=0;i<n-1;i++) for (j=0;j<n-i;j++)/* note the upper and lower bounds of the loop * / if (a[j]>a[j+1]) { TEMP=A[J]; A[J]=A[+1]; A[j+1]=temp; } } 6. Hill sort (void shell_sort (int *a,int n)) void Shell_sort (int *a,int N) { int gap,i,j,temp; for (gap=n/2;gap>0;gap/=2)/* Sets the step size of the sort, the gap is halved per time * / { for (i=gap;i<n;i++) /* Navigate to each element * / { for (J=I-GAP; (j>=0) && (A[j]>a[j+gap]); j-=gap)/* Compare distance The size of the two elements of gap far, depending on the sort direction of how to Exchange * / { Temp=a[j];a[j]=a[j+gap];a[j+gap]=temp; } } } } 7. Select Sort (void select_sort (int *a,int n)) void Select_sort (int *a,int N) { int i,j,k,temp; for (i=0;i<n-1;i++) { K=i; for (j=i+1;j<n;j++) { if (A[k]>a[j])//a[k] if greater than A[j] will be the J assigned to K; K=j; } Temp=a[i]; A[I]=A[K]; A[k]=temp; } } 8. Main function void Main () { printf ("=============== The Time ==============\n" of various sorts ); int a[n]; int low=0; clock_t Begin,stop; Randtofile (a); ReadFromFile (a); Double Exetime_quick,exetime_insert,exetime_bubble,exetime_shell,exetime_select; Test Quick Sort Time Begin=clock ();// Start recording time Quick_sort (A,low,n); Stop=clock ();// time record completed Exetime_quick= (Double) (stop-begin)/clocks_per_sec; printf (" fast sorting %d integers takes %f seconds \ n", N,exetime_quick); Test Insert Sort Time Begin=clock (); Insert_sort (A,n); Stop=clock (); Exetime_insert= (Double) (stop-begin)/clocks_per_sec; printf (" insert sort %d integers time %f sec \ n", N,exetime_insert); test Bubble sort Time Begin=clock (); Bubble_sort (A,n); Stop=clock (); Exetime_bubble= (Double) (stop-begin)/clocks_per_sec; printf (" bubble sort %d integers time %f sec \ n", n,exetime_bubble); Test Hill sort Time Begin=clock (); Shell_sort (A,n); Stop=clock (); Exetime_shell= (Double) (stop-begin)/clocks_per_sec; printf (" Hill sort %d integers time %f sec \ n", N,exetime_shell); Test Select sort Time Begin=clock (); Select_sort (A,n); Stop=clock (); Exetime_select= (Double) (stop-begin)/clocks_per_sec; printf (" choose to sort %d integers time %f seconds \ n", n,exetime_select); printf ("Input a char to end!"); GetChar ();// wait for input characters, do not let the window close } Six, commissioning and operation: 1. the debug effect is as follows when generating 50000 random number as a parameter 2. When generating a 100000 random number as a parameter, the debug effect is as follows: 3. The generated random number deposit file data.txt is as follows: Vii. Summary of the experiment Through this algorithm experiment, first of all, I have a certain understanding of sorting algorithm classification, but also consolidate the basic principles of these algorithms, and then, in the programming of the design and analysis of the algorithm, the difficulty of this experiment let me feel the lack of their own theoretical knowledge reserves; I think the purpose of this experiment is not only to compare the efficiency of various sorting algorithms, but also to understand the thinking patterns within the algorithm, and to know that the basic principles of various sorting algorithms are more important than the efficiency of knowing them. |