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

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

lisihan 6 месяцев назад
Родитель
Сommit
bba71e6be0
5 измененных файлов с 117 добавлено и 0 удалено
  1. 59 0
      随机数排序/algo.c
  2. 8 0
      随机数排序/algo.h
  3. 25 0
      随机数排序/io.c
  4. 6 0
      随机数排序/io.h
  5. 19 0
      随机数排序/main.c

+ 59 - 0
随机数排序/algo.c

@@ -0,0 +1,59 @@
+#include"algo.h"
+#include<stdlib.h>
+#include<math.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
随机数排序/algo.h

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

+ 25 - 0
随机数排序/io.c

@@ -0,0 +1,25 @@
+#include"io.h"
+#include<stdio.h>
+void OutPut(int* arr, int Arraylength)
+{
+    for (int i = 0; i < Arraylength; i++)
+    {
+        if(arr[i]<10)
+        {
+            printf(" %d ", arr[i]);
+        }
+        else
+        {
+            printf("%d ", arr[i]);
+        }
+    }
+    printf("\n");
+    
+}
+
+
+void InPutArrayLength(int* Arraylength)
+{
+    printf("%s","请输入您想要获得的随机数组长度:\n");
+    scanf_s("%d",Arraylength);
+}

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

@@ -0,0 +1,6 @@
+#pragma once
+#ifndef IO_H
+#define IO_H
+void OutPut(int* arr, int Arraylength);
+void InPutArrayLength(int *Arraylength);
+#endif  

+ 19 - 0
随机数排序/main.c

@@ -0,0 +1,19 @@
+#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);
+    OutPut(arr,Arraylength);
+}
+
+ 
+ 
+