zoukankan      html  css  js  c++  java
  • C++BubbleSort-01

     1 #include <iostream>
     2 #include <ctime>
     3 using namespace std;
     4 #define kARRAYCOUNT 20
     5 
     6 // 冒泡排序(次方法会修改外部的内容)升序
     7 void bubbleSort(int **array, int count)
     8 {
     9     if (!array || count <= 0 || count > kARRAYCOUNT)
    10         return;
    11 
    12     for (int i = 1; i < count; i++)
    13     {
    14         for (int j = 0; j < count - i; j++)
    15         {
    16             // 从小到大排序
    17             if ((*array)[j] > (*array)[j + 1])
    18             {
    19                 int temp = (*array)[j];
    20                 (*array)[j] = (*array)[j + 1];
    21                 (*array)[j + 1] = temp;
    22             }
    23         }
    24     }
    25 }
    26 
    27 void printfArray(int *array, int count)
    28 {
    29     if (!array || count <= 0 || count > kARRAYCOUNT)
    30         return;
    31 
    32     for (int index = 0; index < kARRAYCOUNT; index++)
    33     {
    34         cout << array[index] << " ";
    35     }
    36     cout << endl << "---End---" << endl;
    37 }
    38 
    39 void main()
    40 {
    41     int *array = new int[kARRAYCOUNT];
    42 
    43     srand(unsigned(time(0)));
    44 
    45     for (int index = 0; index < kARRAYCOUNT; index++)
    46     {
    47         array[index] = rand() % 50 + 20;
    48     }
    49 
    50     printfArray(array, kARRAYCOUNT);
    51     
    52     bubbleSort(&array, kARRAYCOUNT);
    53 
    54     printfArray(array, kARRAYCOUNT);
    55 
    56     getchar();
    57 }
  • 相关阅读:
    30天自制操作系统之-第四天-
    30天自制操作系统之-第三天-
    30天自制操作系统之-第二天-
    30天自制操作系统之-第一天-
    c语言之连接符
    c语言之函数指针应用
    dpdk之路-环境部署
    linux系统裁剪
    redux和mobx比较(二)
    redux和mobx比较(一)
  • 原文地址:https://www.cnblogs.com/fkunlam/p/4386516.html
Copyright © 2011-2022 走看看