C++BubbleSort-01

來源:互聯網
上載者:User

標籤:

 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 }

 

C++BubbleSort-01

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.