This repository has been archived on 2024-01-06. You can view files and clone it, but cannot push or open issues or pull requests.
justhomework/ex1/ex1.cpp

129 lines
2 KiB
C++
Raw Normal View History

2021-10-03 02:28:37 +00:00
#include <stdio.h>
// #include <iostream>
#include <stdlib.h>
#include <time.h>
2021-10-03 04:23:31 +00:00
int LENGTH;
2021-10-29 08:51:54 +00:00
//数组长度
2021-10-05 06:05:19 +00:00
int RANDOM = 0;
2021-10-29 08:51:54 +00:00
//是否随机生成数组
2021-10-03 04:23:31 +00:00
int MODE = 0;
2021-10-29 08:51:54 +00:00
//1: 迭代
//2: 递归
2021-10-03 02:28:37 +00:00
2021-10-05 06:05:19 +00:00
// #define OUTPUT
2021-10-29 08:51:54 +00:00
//是否输出
2021-10-03 02:28:37 +00:00
2021-10-03 04:23:31 +00:00
int *a;
2021-10-03 02:28:37 +00:00
2021-10-03 04:23:31 +00:00
void swap(int *a, int *b)
2021-10-03 02:28:37 +00:00
{
int c = *a;
*a = *b;
*b = c;
}
2021-10-03 04:23:31 +00:00
void bobblesort1A(int a[], int n)
2021-10-03 02:28:37 +00:00
{
bool sorted = false;
2021-10-15 08:45:28 +00:00
while (sorted == false)
2021-10-03 04:23:31 +00:00
{
2021-10-15 08:45:28 +00:00
sorted = true;
for (int i = 1; i < n; i++)
2021-10-03 02:28:37 +00:00
{
2021-10-15 08:45:28 +00:00
if (a[i - 1] > a[i])
{
swap(&a[i - 1], &a[i]);
2021-10-03 02:28:37 +00:00
2021-10-15 08:45:28 +00:00
sorted = false;
}
2021-10-03 02:28:37 +00:00
}
2021-10-15 08:45:28 +00:00
n--;
2021-10-03 04:23:31 +00:00
}
2021-10-03 02:28:37 +00:00
}
2021-10-03 04:23:31 +00:00
void bobblesort1B(int a[], int n)
2021-10-03 02:28:37 +00:00
{
2021-10-03 04:23:31 +00:00
2021-10-03 02:28:37 +00:00
bool sorted = false;
2021-10-03 04:23:31 +00:00
sorted = true;
for (int i = 1; i < n; i++)
{
if (a[i - 1] > a[i])
2021-10-03 02:28:37 +00:00
{
2021-10-03 04:23:31 +00:00
swap(&a[i - 1], &a[i]);
2021-10-03 02:28:37 +00:00
2021-10-03 04:23:31 +00:00
sorted = false;
2021-10-03 02:28:37 +00:00
}
2021-10-03 04:23:31 +00:00
}
n--;
if (n == 1)
2021-10-03 02:28:37 +00:00
return;
2021-10-03 04:23:31 +00:00
bobblesort1B(a, n);
}
2021-10-05 06:05:19 +00:00
void (*bobblesort)(int a[], int n);
2021-10-03 04:23:31 +00:00
void init()
{
srand(time(NULL));
a = new int[LENGTH];
for (int i = 0; i < LENGTH; i++)
if (RANDOM == true)
a[i] = 1 + rand() % LENGTH;
else
a[i] = LENGTH - i;
if (MODE == 1)
2021-10-05 06:05:19 +00:00
bobblesort = bobblesort1A;
2021-10-03 04:23:31 +00:00
else if (MODE == 2)
2021-10-05 06:05:19 +00:00
bobblesort = bobblesort1B;
2021-10-03 02:28:37 +00:00
}
2021-10-03 04:23:31 +00:00
int main()
{
Begin:
2021-10-29 08:51:54 +00:00
printf("是否随机生成数组(0/1):");
2021-10-05 06:05:19 +00:00
scanf("%d", &RANDOM);
2021-10-03 04:23:31 +00:00
2021-10-29 08:51:54 +00:00
printf("输入数组长度:");
2021-10-03 04:23:31 +00:00
scanf("%d", &LENGTH);
2021-10-29 08:51:54 +00:00
printf("选择模式:1.迭代 2.递归");
2021-10-03 04:23:31 +00:00
scanf("%d", &MODE);
if ((MODE <= 0) || (MODE >= 3))
{
2021-10-29 08:51:54 +00:00
printf("错误输入\n");
2021-10-03 04:23:31 +00:00
goto Begin;
}
2021-10-03 02:28:37 +00:00
init();
clock_t start_t, end_t;
double total_t;
2021-10-03 04:23:31 +00:00
#ifdef OUTPUT
2021-10-03 02:28:37 +00:00
printf("Before\n");
2021-10-03 04:23:31 +00:00
for (int i = 0; i < LENGTH; i++)
printf("%d \n", a[i]);
#endif
2021-10-03 02:28:37 +00:00
start_t = clock();
2021-10-05 06:05:19 +00:00
bobblesort(a, LENGTH);
2021-10-03 02:28:37 +00:00
end_t = clock();
2021-10-03 04:23:31 +00:00
#ifdef OUTPUT
2021-10-03 02:28:37 +00:00
printf("After\n");
2021-10-03 04:23:31 +00:00
for (int i = 0; i < LENGTH; i++)
printf("%d \n", a[i]);
#endif
2021-10-03 02:28:37 +00:00
2021-10-29 08:51:54 +00:00
printf("运算时间: %.4f 秒\n", ((double)(end_t - start_t) / CLOCKS_PER_SEC));
2021-10-03 02:28:37 +00:00
system("pause");
2021-10-03 04:23:31 +00:00
goto Begin;
2021-10-03 02:28:37 +00:00
}