//-----------------冒泡排序--------------------
void bubblesort()
{
int i, j,flag,temp;
for (i = 0; i <= 1999; i++)
{
flag = 0;
for (j = 0; j <= 1999 - i; j++)
{
if (b[j] > b[j + 1])
{
temp = b[j];
b[j] = b[j + 1];
b[j + 1] = temp;
flag = 1;
}
}
if (!flag) break;
}
}
//-----------------快速排序1--------------------
void quicksort1(int left,int right)
{
int i, j, t, temp;
if (left > right)
return;
i = left;
j = right;
temp = a[left];
while (i!=j)
{
while (i < j&&a[j] >= temp)
--j;
while (i < j&&a <= temp)
++i;
if (i
}
//-----------------快速排序2--------------------
int part(int left, int right)
{
int temp = a[left];
int pivot = a[left];
while (left < right)
{
while (left < right&&a[right] >= pivot) right–;
a[left] = a[right];
while (right > left&&a[left] <= pivot) left++;
a[right] = a[left];
}
a[left] = temp;
return left;
}
void quicksort(int left, int right)
{
if (left < right)
{
int flag = part(left, right);
quicksort(left, flag - 1);
quicksort(flag+1,right);
}
}
//-----------------直接插入排序--------------------
void insertsort()
{
int i, j, temp;
for (i = 1; i < 2000; i++)//a[0]作监视哨
{
temp = a;
j = i - 1;
while (a[j]>temp && j >= 0)
{
a[j + 1] = a[j];//后移
--j;
}
a[j + 1] = temp;
}
}
//-----------------希尔排序--------------------
void shellsort()
{
int i, j,temp,k;
int d;//间隔
for (d = 2000 / 2; d > 0;d/=2)
{
//直接插入排序
for (k = 0; k < d; ++k)
{
for (i = k+d; i < 2000; i += d)
{
temp = a;
j = i - d;
while (a[j]>temp && j >= k)
{
a[j + d] = a[j];
j -= d;
}
a[j + d] = temp;
}
}
}
}
//-----------------堆排序--------------------
void swap(int& x, int& y)
{
int temp = x;
x = y;
y = temp;
}
void HeapAdjust(int i,int n)
{
int j, temp;
temp = a;
j = 2 * i + 1;
while (j=a[j]) break;
a = a[j];
i = j;
j = 2 * i + 1;
}
a=temp;
}
void Heapsort()
{
int i;
for (i = 2000/ 2-1; i >= 0; i--)
{
HeapAdjust(i,2000);
}
for (i = 1999; i > 0; i--)
{
swap(a[0],a);
HeapAdjust(0,i-1);
}
}
//-----------------归并排序--------------------
void merge(int*left,int left_length,int *right,int right_length)
{
int i, j, k,m;
i = j = k = 0;
while (i
}
void mergesort(int* x, int n)
{
if (n >= 2)
{
int *left = x;
int left_length = n / 2;
int *right = x + left_length;
int right_length = n - left_length;
mergesort(left, left_length);
mergesort(right, right_length);
merge(left, left_length, right, right_length);
}
}
//-----------------初始化------------------
void init()
{
srand(((unsigned)time(0)));
for (int i = 0; i < 2000; i++)
a = rand()%10000;
}
//-----------------输出--------------------
void output(int *p)
{
for (int i = 0; i < 2000; i++)
cout <
}
int main()
{
double start, end;
init();
cout <<“排序前:” << endl;
output(a);
//-----------------冒泡排序-------------------- //-----------------快速排序-------------------- //-----------------直接插入排序----------------- //-----------------希尔排序-------------------- //-----------------堆排序-------------------- //-----------------归并排序-------------------- } /*
cout << “冒泡排序后:” << endl;
start = clock();
bubblesort();
end = clock();
output(b);
cout << “冒泡排序运行时间(ms):”
<< (double)(end - start) / CLOCKS_PER_SEC * 1000<
cout << “快速排序后:” << endl;
start = clock();
quicksort(0,1999);
end = clock();
output(a);
cout << “快速排序运行时间(ms):”
<< (double)(end - start) / CLOCKS_PER_SEC * 1000 << endl;
cout << “直接插入排序后:” << endl;
start = clock();
insertsort();
end = clock();
output(a);
cout << “直接插入排序运行时间(ms):”
<< (double)(end - start) / CLOCKS_PER_SEC * 1000 << endl;
init();
cout << “希尔排序后:” << endl;
start = clock();
shellsort();
end = clock();
output(a);
cout << “希尔排序运行时间(ms):”
<< (double)(end - start) / CLOCKS_PER_SEC * 1000 << endl;
init();
cout << “堆排序后:” << endl;
start = clock();
Heapsort();
end = clock();
output(a);
cout << “堆排序运行时间(ms):”
<< (double)(end - start) / CLOCKS_PER_SEC * 1000 << endl;
init();
cout << “归并排序后:” << endl;
start = clock();
mergesort(a,2000);
end = clock();
output(a);
cout << “归并排序运行时间(ms):”
<< (double)(end - start) / CLOCKS_PER_SEC * 1000 << endl; system("pause");
return 0;
//
#include
//void sort(int arr[],int length);
void maosort(int arr[],int length);
int main()
{
int arr[] = {12,45,67,123,456,5,43,22,11,-9};
int length = sizeof(arr)/sizeof(arr[0]);
//sort(arr,length);
maosort(arr,length);
int k;
for(k = 0;k < length;k++){
printf(“%d “,arr[k]);
}
printf(”\n”);
return 0;
}选择排序
选择排序
/
void sort(int arr[],int length)
{
int i,j;
for(i = 0;i < length-1;i++)
{
for(j = i+1;j < length;j++){
if(arr[i]>arr[j]){
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
}
/
冒泡排序
*/
void maosort(int arr[],int length)
{
int i,j;
for(i = 0;i < length-1;i++)
{
for(j = 0;j < length-i-1;j++){
if(arr[j]>arr[j+1]){
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}