#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void BubbleSort1 (int n, int *array)/*little > big*/
{
int I, J;
for (i=0; i<n-1; i++)
{
for (j=n-1; j>i; j--)
{
if (Array[j] < array[j-1])
{
int temp = Array[j];
ARRAY[J] = array[j-1];
ARRAY[J-1] = temp;
}
}
}
}
void BubbleSort2 (int n, int *array)
{
int I, j, Flag=1; /*flag=1 said the need to continue bubbling * *
for (i=0; i<n-1 && flag; i++)
{
Flag = 0;
for (j=n-1; j>i; j--)
{
if (Array[j] < array[j-1])
{
int temp = Array[j];
ARRAY[J] = array[j-1];
ARRAY[J-1] = temp;
flag = 1;
}
}
}
}
void Selectsort (int n, int *array)
{
int I, j, Min;
for (i=0; i<n-1; i++)
{
min = i;
For (j=i+1 j<n; j + +)
{
if (Array[min] > Array[j])
min = j;
}
int temp = Array[min];
Array[min] = Array[i];
Array[i] = temp;
}
}
void Insertsort (int n, Int*array)
{
int i, J;
for (I=1 i<n; i++)
{
if (Array[i] < array[i-1]) /* whether to insert */
{
int key = array[i]; //Sentry
for (j = i-1;j>=0 && Array[j] > Key j--)
{
array[j+1] = array[j];
&NBSP;&NBSP;&NBSP}
/* the Array[j]<=key at the end of the loop, insert key into j+1 */
array[j+1] = key;
}
}
}
/* Grouping Insert sort */
void shellsort (int n, int *array)
{
int i, J;
int increment;
for (INCREMENT=N/2 increment > 0; increment/= 2)
{
for (i=0; i<increment; i++) /* a set of sequences for insertion sort */
{
for (j=i+increment; j<n; j+=increment)
{
if (Array[j] < array[j-increment])
{
int key = Array[j];
int K;
for (k=j-increment k>=0 && array[k]>key; k-= increment)
{
array[k+increment] = array[k];
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP}
array[k+increment] = key;
&NBSP;&NBSP;&NBSP;&NBSP}
}
}
/* Partition method */
void QuickSort (int left, right int, int *array)
{
if (left>=right)
return ;
int I=left, j=right;
int Key=array[i];
while (i<j)
{
while (i<j && array[j]>=key)
j--;
array[i] = array[j];
while (i<j && array[i]<=key)
i++;
&NBSP;&NBSP;ARRAY[J] = Array[i];
}
array[i] = key;
quicksort (left, i-1, array);
quicksort (i+1, right, array);
}
/*ARRAY[START+1] ~ Array[end] has been satisfied with the definition of the heap, the adjustment makes Array[start] ~ Array[end] Meet the heap definition * *
void Heapadjust (int start, int end, int array[])
{
int i;
int temp = Array[start]; /* Generate the first blank * *
For (i=2*start+1 i<=end; i=2*i+1)/* The blank node for Each loop is array[(i-1)/2]*/
{
if (I<end && array[i] < array[i+1])/* Look for a larger value in the left and right children * *
i++;
if (Array[i] > Temp)
array[(I-1)/2] = Array[i];
Else
Break
}
array[(I-1)/2] = temp; /* Insert the original temp to the blank place * *
}
void Heapsort (int n, int array[])
{
int i;
For (i= (n-2)/2; i>=0; i--)/* Construction Big Top Heap * *
Heapadjust (i, n-1, array);
for (i=n-1; i>0; i--)
{
int t = array[i]; /* Swap the root node to the end of the array * *
Array[i] = array[0];
Array[0] = t;
Heapadjust (0, i-1, array); /* Re-adjust heap * *
}
}
/*ARRAY[S...M] and array[m+1...t] have their own order, merging makes array[s...t] orderly.
void Merge (int s, int m, int t, int *array)
{
int temp[t-s+1];
int i=s, j=m+1, k=0;
while (i<=m && j<=t)
{
if (Array[i] < array[j])
temp[k++] = array[i++];
Else
temp[k++] = array[j++];
}
while (I<=M)
temp[k++] = array[i++];
while (j<=t)
temp[k++] = array[j++];
for (I=s, k=0; i<=t && k<=t-s; i++, k++)
{
array[i] = temp[k];
&NBSP}
}
void Msort (int s, int t, int *array) /* recursive call */
{
if (s = = t)
return;
int m = (s+t)/2;
msort (S, M, array);
msort (m+1, T, array);
merge (S, M, T, array);
}
void MergeSort1 (int n, int *array)
{
msort (0, n-1, array);
}
void MergeSort2 (int n, int *array) /* non-recursive implementation merge sort */
{
int k, I;
for (k=1; 2*k<n; k *= 2) /* sets the length of the ordered sequence to be merged in each paragraph: 1,2,4,8,16......*/
{
for (i=0; i+k-1< N i + = 2*k) /* Consider the sequence of left and right two segments to be merged, [I+k-1] is the trailing element of the left sequence subscript */
{ &NBSP;/*[END=I+2*K-1] is the subscript at the end of the right sequence, and should not exceed the n-1*/
int end=i+2*k-1;
if (End > n-1)
end = n-1;
merge (i, i+k-1, end, array);
}
}
}
int main ()
{
Long start, stop;
int n;
printf ("The following comparison of several time complexity for NLOGN ranking algorithm efficiency, the other 3 inefficient sorting is not considered \ n");
printf ("Enter the quantity to be sorted (the int type indicates that more than 1 million may overflow on my machine): \ n");
scanf ("%d", &n);
int a[n], I;
for (i=0; i<n; i++)
A[i] = rand ()%n;
start = Clock ();
Shellsort (n, a);
stop = Clock ();
printf ("Hill sort%d data takes time:%ldms\n", N, (Stop-start) *1000/clocks_per_sec);
for (i=0; i<n; i++)
A[i] = rand ()%n;
start = Clock ();
Heapsort (n, a);
stop = Clock ();
printf ("Heap sort%d data takes time:%ldms\n", N, (Stop-start) *1000/clocks_per_sec);
for (i=0; i<n; i++)
A[i] = rand ()%n;
start = Clock ();
MergeSort1 (n, a);
stop = Clock ();
printf ("Recursive merge sort%d data takes time:%ldms\n", N, (Stop-start) *1000/clocks_per_sec);
for (i=0; i<n; i++)
A[i] = rand ()%n;
start = Clock ();
MergeSort2 (n, a);
stop = Clock ();
printf ("Non-recursive merge sort%d data takes time:%ldms\n", N, (Stop-start) *1000/clocks_per_sec);
for (i=0; i<n; i++)
A[i] = rand ()%n;
start = Clock ();
QuickSort (0, N-1, a);
stop = Clock ();
printf ("Quickly sort%d data takes time:%ldms\n", N, (Stop-start) *1000/clocks_per_sec);
/* for (i=0; i<n; i++)
{
printf ("%d", a[i]);
}
*/
return 0;
}