利用随机函数产生30000个随机整数,利用插入排序,起泡排序,选择排序,快速排序,

如题所述

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
void maopao(int *a, int n) //冒泡排序
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n - 1; j++)
{
if (a[j]>a[j + 1])
{
int tmp = a[j];
a[j] = a[j + 1];
a[j + 1] = tmp;
}
}
}
printf("冒泡排序后的顺序为:\n");
for (int i = 0; i < n; i++)
{
printf("%-3d", a[i]);
}
}
void charu(int *a, int n)//插入排序算法;
{
for (int i = 1; i < n; i++)
{
int temp = a[i];
/* printf("temp=%d\n", temp);*/
for (int j = i - 1; j >= 0; j--)
{
if (temp>a[j])
{
a[j + 1] = temp;
break;
}
else
{
a[j + 1] = a[j];
if (j == 0)
a[j] = temp;
}
}
}
printf("\n插入排序后的顺序为:\n");
for (int i = 0; i < n; i++)
{
printf("%-3d", a[i]);
}
}
void choose(int a[], int n)//从小到大;
{
int min;
for (int i = 0; i < n - 1; i++)
{
min = i;
for (int j = min + 1; j<n; j++)//一次循环比较;
{
if (a[min]>a[j])
{
min = j;
}
}
if (min != i)
{
int temp = a[i];
a[i] = a[min];
a[min] = temp;
}
}
printf("\n选择排序后的顺序为:\n");
for (int i = 0; i < n; i++)
{
printf("%-3d", a[i]);
}
printf("\n");
}
void main()
{
int m;
time_t s;
srand((unsigned int)& s);
int *p = (int *)malloc(sizeof (int)* 30000);//利用堆区空间;
printf("随机产生的20个数为;\n");
for (int i = 0; i < 30000; i++)
{
p[i] = rand() % 100;
printf("%-3d", p[i]);
}
printf("\n你想选择1冒泡排序OR 2插入排序 OR 3选择排序?");
scanf("%d", &m);
switch (m)
{
case 1:maopao(p, 30000); break;
case 2:charu(p, 30000); break;
case 3:choose(p, 30000); break;
}
free(p);
system("pause");
}
温馨提示:内容为网友见解,仅供参考
无其他回答
相似回答