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.cpp

129 lines
1.9 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-03 02:28:37 +00:00
//<2F><><EFBFBD><EFBFBD><E9B3A4>
2021-10-05 06:05:19 +00:00
int RANDOM = 0;
2021-10-03 04:23:31 +00:00
//<2F>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
int MODE = 0;
2021-10-05 06:05:19 +00:00
//1: <20><><EFBFBD><EFBFBD>
//2: <20>ݹ<EFBFBD>
2021-10-03 02:28:37 +00:00
2021-10-05 06:05:19 +00:00
// #define OUTPUT
2021-10-03 04:23:31 +00:00
//<2F>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD>
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-03 04:23:31 +00:00
A:
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 (sorted == false)
2021-10-03 02:28:37 +00:00
goto A;
}
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-05 06:05:19 +00:00
printf("<EFBFBD>Ƿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>(0/1):");
scanf("%d", &RANDOM);
2021-10-03 04:23:31 +00:00
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>:");
scanf("%d", &LENGTH);
printf("ѡ<EFBFBD><EFBFBD>ģʽ:1.<2E><><EFBFBD><EFBFBD> 2.<2E>ݹ<EFBFBD>");
scanf("%d", &MODE);
if ((MODE <= 0) || (MODE >= 3))
{
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>\n");
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-03 04:23:31 +00:00
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD>: %.4f <20><>\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
}