Просмотр исходного кода

上传文件至 '随机数排序C++'

lisihan 4 месяцев назад
Родитель
Сommit
bc8445e17f

+ 60 - 0
随机数排序C++/algo.cpp

@@ -0,0 +1,60 @@
+#include"algo.h"
+#include<stdlib.h>
+#include<math.h>
+#include<time.h>
+void QuickSort(int* arr, int left, int right)
+{
+    if (left >= right) 
+    {
+        return;
+    }
+    int position = PartSort(arr, left, right);
+    QuickSort(arr, left, position - 1);
+    QuickSort(arr, position + 1, right);
+}
+
+
+int  PartSort(int* arr, int left, int right)
+{
+    int key = arr[left];
+
+    while (left < right) {
+        while (left < right && arr[right] >= key) 
+        {
+            right--;
+        }
+        swap(arr, left, right);
+
+        while (left < right && arr[left] <= key) 
+        {
+            left++;
+        }         
+        swap(arr, left, right);
+    }
+
+    return left;
+}
+
+
+void swap(int* arr, int left, int right)
+{
+    int temp = 0;
+
+    temp = arr[right];
+    arr[right] = arr[left];
+    arr[left] = temp;
+}
+
+
+int* RandomArrayCreate(int Arraylength)
+{
+    int static arr[10];
+
+    srand((unsigned int)time(NULL));
+    for (int i = 0; i < Arraylength; i++)
+    {
+        arr[i] = 1 + rand() % 100;
+    }
+
+    return arr;
+}

+ 8 - 0
随机数排序C++/algo.h

@@ -0,0 +1,8 @@
+#pragma once
+#ifndef ALGO_H
+#define ALGO_H
+void QuickSort(int* arr/*O*/, int left/*I*/, int right/*I*/);
+int  PartSort(int* arr/*IO*/, int left/*I*/, int right/*I*/);
+void swap(int* arr/*IO*/, int left/*I*/, int right/*I*/);
+int* RandomArrayCreate(int Arraylength/*I*/);
+#endif

+ 36 - 0
随机数排序C++/io.cpp

@@ -0,0 +1,36 @@
+#include"io.h"
+#include<stdio.h>
+#include<assert.h>
+#include<iostream>
+
+void OutPut(int* arr, int Arraylength)
+{
+    for (int i = 0; i < Arraylength; i++)
+    {
+        if(arr[i]<10)
+        {
+            std::cout << " "<<arr[i] << " ";
+        }
+        else
+        {
+            std::cout << arr[i] << " ";
+        }
+    }
+
+    std::cout << std::endl;
+    
+}
+
+
+void InPutArrayLength(int* Arraylength)
+{
+    std::cout << "请输入您想要获得的随机数组长度:"<< std::endl;
+
+    // 05/21/2024 Li SiHan Added Start
+    //函数assert用于确认数组长度输入的合法性
+    //确认输入为数字
+    assert(scanf_s("%d",Arraylength)!=0);
+    //确认输入为正数
+    assert(*Arraylength >= 0);
+    // 05/21/2024 Li SiHan Added End
+}

+ 28 - 0
随机数排序C++/io.h

@@ -0,0 +1,28 @@
+#pragma once
+#include<iostream>
+#ifndef IO_H
+#define IO_H
+void OutPut(int* arr/*I*/, int Arraylength/*I*/);
+
+void InPutArrayLength(int *Arraylength/*O*/);
+
+//05/23/2024 Li SiHan Added Start
+//用于遍历数组并输出的模板函数声明及函数体
+template<typename ObjectTraversed>
+void foreach(ObjectTraversed  obj, int Arraylength)
+{
+    for (int i = 0; i < Arraylength; i++)
+    {
+        if (*obj < 10)
+        {
+            std::cout << " " << *obj << " ";
+        }
+        else
+        {
+            std::cout << *obj << " ";
+        }
+        obj += 1;
+    }
+}
+//05/23/2024 Li SiHan Added End
+#endif  

+ 48 - 0
随机数排序C++/main.cpp

@@ -0,0 +1,48 @@
+/*HEAD  lisihan_homework/随机数排序/main.c */
+ /*==============================================================================
+ 2024年度面向对象程序设计作业
+================================================================================
+ File description:
+
+  这是随机数排序的程序。
+    提交者:李思翰
+    邮 箱:1711871773@qq.com
+
+ ================================================================================
+ Date               Name             Description of Change
+ 05/19/2024         Li SiHan         Created
+ 05/21/2024         Li SiHan         Added Code comments and Assert function
+ 05/23/2024         Li SiHan         Change C to C++ and Added OutPut Template function and Code comments
+ $HISTORY$
+ ================================================================================
+ */
+#include<stdio.h>
+#include"algo.h"
+#include"io.h"
+int main()
+{
+    //对于数组的长度这一变量进行声明及初始化
+    int Arraylength=0; 
+
+    //用户对数组长度这一变量进行赋值
+    InPutArrayLength(&Arraylength); 
+   
+    //生成并返回所需长度的随机数组
+    int* arr = RandomArrayCreate(Arraylength); 
+    //输出当前所生成的数组
+    OutPut(arr,Arraylength); 
+
+    //对数组进行快速排序
+    QuickSort(arr,0, Arraylength-1); 
+     
+    //05/23/2024 Li SiHan Added Start
+    //用模板函数输出排序后的数组
+    foreach(arr,Arraylength);
+    //05/23/2024 Li SiHan Added End
+
+    return 0;
+}
+
+ 
+ 
+