• 排序


    写几个常用的排序:

    1)冒泡排序

    2)选择排序

    3)插入排序

    4)归并排序

    5)快速排序

    6)希尔排序

    具体实现:

    1. 冒泡排序

    [cpp] view plaincopy
     
    1. //冒泡排序  
    2. //i: start -> end - 1  
    3. //每一次两两比较array[i]与array[i+1],大的上浮,小下沉  
    4. //比较次数来源于如果array[0]是最大值,则需要size - 1次才能放到正确的位置  
    5. template<class Type>  
    6. void BubbleSort(Type* array,int start,int end){  
    7.     for(int i = 0;i < end - start;i++)  
    8.         for(int j = 0;j < end - start;j++){  
    9.             if(array[j] > array[j + 1])  
    10.                 swap(array[j],array[j + 1]);  
    11.         }  
    12. }  



    2. 选择排序

    [cpp] view plaincopy
     
    1. //选择排序  
    2. //每一次都从为选出值的数组中选取最小的放在当前排放的位置处  
    3. template<class Type>  
    4. void SelectionSort(Type* array,int start,int end){  
    5.     for(int i = 0;i < end - start;i++)  
    6.         for(int j = i + 1;j < end - start + 1;j++){  
    7.             if(array[i] > array[j])  
    8.                 swap(array[i],array[j]);  
    9.         }  
    10. }  



    3. 插入排序

    [cpp] view plaincopy
     
    1. //插入排序  
    2. //从i: start -> end  
    3. //在i之前的有序的数组中找到正确的位置插入  
    4. template<class Type>  
    5. void InsertionSort(Type* array,int start,int end){  
    6.     for(int i = 1;i < end - start + 1;i++)  
    7.         for(int j = 0;j < i;j++){  
    8.             if(array[j] > array[i]){  
    9.                 int temp = array[i];  
    10.                 for(int k = i;k > j;k--)  
    11.                     array[k] = array[k - 1];  
    12.                 array[j] = temp;    //此时array[j]是最大的了  
    13.                 break;  
    14.             }  
    15.         }  
    16. }  



    4. 归并排序

    [cpp] view plaincopy
     
    1. //归并排序  
    2. //每次Merge都是将start -> end 划分为start -> middle -1  && middle -> end两部分,吧两个部分中最小的  
    3. //一半放到新的start -> middle - 1中去,其余的未塞入的将start -> end 填满  
    4. //MergeSort 就是讲一长串数组划分为两半,用递归再划分两半的两半... 自底向上排序完成  
    5. //此外:Merge完成后,array: start -> middle -1 这前半部分的最大值 小于 array: middle -> end这后半部分的最小值  
    6. template<class Type>  
    7. void Merge(Type* a,int start,int middle,int end){  
    8.     Type *temp = new Type[end - start + 1]; //保留这轮排序后的数组  
    9.     int index = 0;  
    10.     bool *isChosen = new bool[end - start + 1]; //某个元素是否被选出  
    11.     for(int i = 0;i < end - start + 1;i++)  
    12.         isChosen[i] = false;  
    13.     int indexA = start; //前半段a[start] -->  a[middle]  
    14.     int indexB = middle; // 后半段 a[middle + 1] --> a[end - 1]  
    15.     while(indexA < middle && indexB <= end){  
    16.         if(a[indexA] < a[indexB]){  
    17.             temp[index++] = a[indexA];  
    18.             isChosen[indexA++ - start] = true;  
    19.         }else{  
    20.             temp[index++] = a[indexB];  
    21.             isChosen[indexB++ - start] = true;  
    22.         }  
    23.     }  
    24.     //剩余的都填满temp[]  
    25.     for(int i = 0;i < end - start + 1;i++)  
    26.         if(!isChosen[i])  
    27.             temp[index++] = a[start + i];  
    28.     for(int i = 0;i < end - start + 1;i++)  
    29.         a[start + i] = temp[i];  
    30.   
    31.     delete []temp;  
    32.     delete []isChosen;  
    33. }  
    34. template<class Type>  
    35. void MergeSort(Type* array,int start,int end){  
    36.     if(end - start == 1){  //如果只剩两个元素了那就简单比一下  
    37.         if(array[start] > array[end])  
    38.             swap(array[start],array[end]);  
    39.     }  
    40.     else if(end - start >= 2){ //只剩余了少于三个元素,就不进行分割比较了  
    41.         int middle = (start + end) / 2;  
    42.         MergeSort(array,start,middle);  
    43.         MergeSort(array,middle + 1,end);  
    44.         Merge(array,start,middle + 1,end);  
    45.     }  
    46. }  



    5. 快速排序

    [cpp] view plaincopy
     
    1. //快速排序  
    2. //就是找第一位a[start]为基准为,一轮Partition之后,a[start]值的位置变为q  
    3. //q位置前都小于他,后面都大于他,然后就q所分割的两部分再进行快排  
    4. template<class Type>  
    5. int Partition(Type *a,int start,int end){  
    6.     Type x = a[start];  
    7.     int i = start;  
    8.     for(int j = start + 1;j <= end;j++){  
    9.         if(a[j] <= x){  
    10.             i++;  
    11.             swap(a[i],a[j]);  
    12.         }  
    13.     }  
    14.     swap(a[i],a[start]); //把基准的值放在中间,则左边都小于他,右边都大于他  
    15.     return i;  
    16. }  
    17. template<class Type>  
    18. void QuickSort(Type *array,int start,int end){  
    19.     if(start < end){  
    20.         int q = Partition(array,start,end); //分割成两部分  
    21.         QuickSort(array,start,q - 1);  
    22.         QuickSort(array,q + 1,end);  
    23.     }  
    24. }  



    6. 希尔排序

    [cpp] view plaincopy
     
    1. //希尔排序  
    2. //步长要小于数组元素数量,不然没意义跳过  
    3. //通过不同的步进,选取相隔一样步进的元素组成组,如步进为3时则a[0],a[3],a[6]...为一组  
    4. //由最初的步进长度,作为起始步进逐步减少到步进为一,变为普通的全数组的插入排序  
    5. template<class Type>  
    6. void ShellPass(Type* array,int start,int end,int d){  
    7.     for(int i = 0;i < d;i++){  
    8.         for(int j = start + i + d;j < end - start + 1;j += d){  
    9.             for(int k = start + i;k < j;k += d){  
    10.                 if(array[k] > array[j]){  
    11.                     Type temp = array[j];  
    12.                     for(int l = j;l > k;l -= d)  
    13.                         array[l] = array[l - d];  
    14.                     array[k] = temp;  
    15.                 }  
    16.             }  
    17.         }  
    18.     }  
    19. }  
    20. template<class Type>  
    21. void ShellSort(Type* array,int start,int end){  
    22.     int d = 10;  
    23.     while(d > 0){  
    24.         d = (d + 1) / 2;  
    25.         ShellPass(array,start,end,d);  
    26.         if(d == 1)  
    27.             break;  
    28.     }  
    29. }  

    测试代码:

    [cpp] view plaincopy
     
    1. int main()  
    2. {  
    3.     int a[] = {1,4,3,6,9,12,12,1,2,43,3,2,1,3,12};  
    4.     int size = 15;  
    5.   
    6.         cout << "原数组: " << endl;  
    7.     for(int i = 0;i < size;i++)  
    8.         cout << a[i] << " ";  
    9.     cout << endl << endl;  
    10.   
    11.     int *array = new int[size];  
    12.   
    13.     for(int i = 0;i < size;i++)  
    14.         array[i] = a[i];  
    15.     cout << "BubbleSort: " << endl;  
    16.     BubbleSort(array,0,size - 1);  
    17.     for(int i = 0;i < size;i++)  
    18.         cout << array[i] << " ";  
    19.     cout << endl << endl;  
    20.   
    21.     for(int i = 0;i < size;i++)  
    22.         array[i] = a[i];  
    23.     cout << "SelectionSort: " << endl;  
    24.     SelectionSort(array,0,size - 1);  
    25.     for(int i = 0;i < size;i++)  
    26.         cout << array[i] << " ";  
    27.     cout << endl << endl;  
    28.   
    29.     for(int i = 0;i < size;i++)  
    30.         array[i] = a[i];  
    31.     cout << "InsertionSort: " << endl;  
    32.     InsertionSort(array,0,size - 1);  
    33.     for(int i = 0;i < size;i++)  
    34.         cout << array[i] << " ";  
    35.     cout << endl << endl;  
    36.   
    37.     for(int i = 0;i < size;i++)  
    38.         array[i] = a[i];  
    39.     cout << "MergeSort: " << endl;  
    40.     MergeSort(array,0,size - 1);  
    41.     for(int i = 0;i < size;i++)  
    42.         cout << array[i] << " ";  
    43.     cout << endl << endl;  
    44.   
    45.     for(int i = 0;i < size;i++)  
    46.         array[i] = a[i];  
    47.     cout << "QuickSort: " << endl;  
    48.     QuickSort(array,0,size - 1);  
    49.     for(int i = 0;i < size;i++)  
    50.         cout << array[i] << " ";  
    51.     cout << endl << endl;  
    52.   
    53.     for(int i = 0;i < size;i++)  
    54.         array[i] = a[i];  
    55.     cout << "ShellSort: " << endl;  
    56.     ShellSort(array,0,size - 1);  
    57.     for(int i = 0;i < size;i++)  
    58.         cout << array[i] << " ";  
    59.     cout << endl << endl;  
    60.   
    61.     return 0;  
    62. }  


    测试输出:

  • 相关阅读:
    Building workspace has encountered a proble
    Eclipse异常关闭,IDE Exception Handler has encountered a problem An internal error has occurred
    jsp中写java代码的方法
    如何在jsp里面写java代码
    jsp中在java里面怎么调文本框里面的值?
    Typescript基本认识
    运行flutter run 报错Could not determine the dependencies of task ':app:compileDebugJavaWithJavac'.
    H5+原生webview实现APP的JavascriptBridge的使用
    vue mounted里使用window.onresize报错问题
    关于elmentui 抽屉 el-drawer 的slot插入的内容无法通过ref访问的问题
  • 原文地址:https://www.cnblogs.com/xzh1993/p/4990529.html
Copyright © 2020-2023  润新知