|
@@ -1,5 +1,21 @@
|
|
|
|
+/*HEAD homework 1 HOMEWORK */
|
|
|
|
+/*==============================================================================
|
|
|
|
+2024年度随机数排序程序设计作业
|
|
|
|
+================================================================================
|
|
|
|
+File description:
|
|
|
|
+这是第一次作业演示的样板程序。
|
|
|
|
+提交者:徐子昂
|
|
|
|
+邮 箱: dutxza@163.com
|
|
|
|
+
|
|
|
|
+================================================================================
|
|
|
|
+Date Name Description of Change
|
|
|
|
+05/20/2024 Xu Ziang Created.
|
|
|
|
+$HISTORY$
|
|
|
|
+================================================================================
|
|
|
|
+*/
|
|
|
|
+
|
|
#include <iostream>
|
|
#include <iostream>
|
|
-#include <ctime> // 随机数种子头文件
|
|
|
|
|
|
+#include <ctime> // 闅忔満鏁扮�瀛愬ご鏂囦欢
|
|
using namespace std;
|
|
using namespace std;
|
|
|
|
|
|
void generate_rand(int Size/*I*/, int num[]/*I*/)
|
|
void generate_rand(int Size/*I*/, int num[]/*I*/)
|
|
@@ -9,7 +25,7 @@ void generate_rand(int Size/*I*/, int num[]/*I*/)
|
|
num[i] = rand() % 100 + 1;
|
|
num[i] = rand() % 100 + 1;
|
|
}
|
|
}
|
|
|
|
|
|
- cout << "生成的随机数组:" << endl;
|
|
|
|
|
|
+ cout << "鐢熸垚鐨勯殢鏈烘暟缁勶細" << endl;
|
|
for (int i = 0; i < Size; i++)
|
|
for (int i = 0; i < Size; i++)
|
|
{
|
|
{
|
|
cout << num[i] << " ";
|
|
cout << num[i] << " ";
|
|
@@ -20,13 +36,13 @@ void generate_rand(int Size/*I*/, int num[]/*I*/)
|
|
void sort_num(int Size/*I*/, int num[]/*I*/)
|
|
void sort_num(int Size/*I*/, int num[]/*I*/)
|
|
{
|
|
{
|
|
|
|
|
|
- for (int i = 0; i < Size - 1; i++) // 对比总轮数
|
|
|
|
|
|
+ for (int i = 0; i < Size - 1; i++) // 对比总轮数
|
|
{
|
|
{
|
|
- for (int j = 0; j < Size - i - 1; j++) // 每轮对比次数
|
|
|
|
|
|
+ for (int j = 0; j < Size - i - 1; j++) // 姣忚疆瀵规瘮娆℃暟
|
|
{
|
|
{
|
|
if (num[j] > num[j + 1])
|
|
if (num[j] > num[j + 1])
|
|
{
|
|
{
|
|
- // 交换元素
|
|
|
|
|
|
+ // 浜ゆ崲鍏冪礌
|
|
int temp = num[j];
|
|
int temp = num[j];
|
|
num[j] = num[j + 1];
|
|
num[j] = num[j + 1];
|
|
num[j + 1] = temp;
|
|
num[j + 1] = temp;
|
|
@@ -37,7 +53,7 @@ void sort_num(int Size/*I*/, int num[]/*I*/)
|
|
|
|
|
|
void result(int Size/*I*/, int num[]/*I*/)
|
|
void result(int Size/*I*/, int num[]/*I*/)
|
|
{
|
|
{
|
|
- cout << "排序后的数组:" << endl;
|
|
|
|
|
|
+ cout << "排序后的数组:" << endl;
|
|
for (int i = 0; i < Size; i++)
|
|
for (int i = 0; i < Size; i++)
|
|
{
|
|
{
|
|
cout << num[i] << " ";
|
|
cout << num[i] << " ";
|
|
@@ -47,19 +63,19 @@ void result(int Size/*I*/, int num[]/*I*/)
|
|
|
|
|
|
int main()
|
|
int main()
|
|
{
|
|
{
|
|
- // 设置随机数种子
|
|
|
|
- srand(time(NULL)); //防止每次生成随机数一样
|
|
|
|
|
|
+ // 设置随机数种子
|
|
|
|
+ srand(time(NULL)); //防止每次生成随机数一样
|
|
|
|
|
|
const int Size = 10;
|
|
const int Size = 10;
|
|
int num[Size];
|
|
int num[Size];
|
|
|
|
|
|
- // 1.生成随机数并查看
|
|
|
|
|
|
+ // 1.鐢熸垚闅忔満鏁板苟鏌ョ湅
|
|
generate_rand(Size, num);
|
|
generate_rand(Size, num);
|
|
|
|
|
|
- // 2.使用冒泡排序对随机数进行排序
|
|
|
|
|
|
+ // 2.浣跨敤鍐掓场鎺掑簭瀵归殢鏈烘暟杩涜�鎺掑簭
|
|
sort_num(Size, num);
|
|
sort_num(Size, num);
|
|
|
|
|
|
- // 3.输出排序结果
|
|
|
|
|
|
+ // 3.杈撳嚭鎺掑簭缁撴灉
|
|
result(Size, num);
|
|
result(Size, num);
|
|
|
|
|
|
system("pause");
|
|
system("pause");
|