乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      數(shù)據(jù)結(jié)構(gòu)(二)——排序

       印度阿三17 2019-02-02

       

      如上圖,若兩個5排序之后交換了位置就是不穩(wěn)定的,沒有交換位置就是穩(wěn)定排序

       

      1.選擇排序

        

       

        冒泡是相鄰的兩個交換,選擇法是首元素與最小的交換。

       1 void xuanzhepaixu(int* my_array, int len)
       2 {
       3     for (int i = 0; i < len - 1;   i) {
       4         for (int j = i   1; j < len;   j) {
       5             if (my_array[i] > my_array[j]) {// 交換次數(shù)多,不如記錄下表位置效率高
       6                 int temp = my_array[i];
       7                 my_array[i] = my_array[j];
       8                 my_array[j] = temp;
       9             }
      10         }
      11 
      12     }
      13 }

       

       

       1 #include <stdio.h>
       2 #include <stdlib.h>
       3 #include <string.h>
       4 
       5 // 選擇法排序
       6 void xuanzhepaixu(int* my_array, int len)
       7 {
       8     int min = 0;
       9     for (int i = 0; i < len - 1;   i) {
      10         min = i;
      11         for (int j = i   1; j < len;   j) {
      12             if (my_array[min] > my_array[j]) {
      13                 min = j;// 保存最小元素的位置
      14             }
      15         }
      16         if ( min != i ) {
      17             int temp = my_array[i];
      18             my_array[i] = my_array[min];
      19             my_array[min] = temp;
      20         }
      21     }
      22 }
      23 
      24 void my_print_array(int* my_array, int len)
      25 {
      26     for (int i = 0; i < len;   i) {
      27         printf("]", my_array[i]);
      28     }
      29     printf("\n");
      30 }
      31 
      32 int main()
      33 {
      34     int my_array[] = {10, 6, 7, 4, 9, 8, 5, 1, 3, 2};
      35     int len = sizeof(my_array) / sizeof(int);
      36 
      37     xuanzhepaixu(my_array, len);
      38 
      39     my_print_array(my_array, len);
      40 
      41     system("pause");
      42     return 0;
      43 }

       

      2.冒泡排序

       

       1 void maopaopaixu(int* my_array, int len)
       2 {
       3     for (int i = 0; i < len;   i) {
       4         for (int j = 1; j < len;   j) {
       5             if ( my_array[j] > my_array[j - 1] ) {
       6                 int temp = my_array[j];
       7                 my_array[j] = my_array[j - 1];
       8                 my_array[j - 1] = temp;
       9             }
      10         }
      11     }
      12 }

       

      冒泡算法的優(yōu)化,在待排序數(shù)據(jù)處于一種趨于有序的情況,可以減少判斷次數(shù),比如:1,2,3,4,7,5,6

       1 void maopaopaixu(int* my_array, int len)
       2 {
       3     bool flag = false;
       4     for (int i = 0; i < len && !flag;   i) {
       5         flag = true;
       6         for (int j = 1; j < len;   j) {
       7             if ( my_array[j] > my_array[j - 1] ) {
       8                 int temp = my_array[j];
       9                 my_array[j] = my_array[j - 1];
      10                 my_array[j - 1] = temp;
      11                 flag = false;
      12             }
      13         }
      14     }
      15 }

       

      3.插入排序

        默認(rèn)對兩個序列進(jìn)行操作:有序序列,無序序列。

        可以將無序序列分為兩個部分

       

      void insert_paixu(int* my_array, int len)
      {
          int temp = 0;// 存儲基準(zhǔn)數(shù)
          int index = 0; // 存儲坑的位置
      
          for (int i = 1; i < len;   i) {
              temp = my_array[i];
              index = i;
              for (int j = i - 1; j >= 0; --j) {// 從后往前遍歷
                  if (temp < my_array[j]) {
                      my_array[j   1] = my_array[j];
                      index = j;
                  }
                  else break;//最后一個保存的是最大的元素,此語句可以減少判斷
              }
              my_array[index] = temp;
          }
      }

       

      4.希爾排序


        先分組,再對每組進(jìn)行插入排序。希爾排序是把記錄按下標(biāo)的一定增量分組,對每組使用直接插入排序;隨著增量逐漸減少,每組包含的關(guān)鍵詞越來越多,當(dāng)增量減至1時,整個文件恰被分為一組,算法便終止;

        

        本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊一鍵舉報(bào)。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評論

        發(fā)表

        請遵守用戶 評論公約

        類似文章 更多