zoukankan      html  css  js  c++  java
  • Sort~

    Insertion sort, buble sort, selection sort, shell sort, quick sort, heap sort, merge sort, radix sort......

    Sort seems like a simple problem but fascinating.

    1 // SearchAlgorithm.cpp : 定义控制台应用程序的入口点。
    2  //
    3  
    4 #include "stdafx.h"
    5 #include <iostream>
    6 #include <vector>
    7 #include <math.h>
    8
    9  using namespace std;
    10
    11 void swap ( int &a , int &b )
    12 {
    13 int tmp = a;
    14 a = b;
    15 b = tmp;
    16 }
    17
    18
    19 void InsertionSort ( vector <int> &A )
    20 {
    21 for ( int i = 1 ; i < ( int )A.size() ; i++ )
    22 {
    23 int tmp = A [ i ];
    24 int position;
    25 for ( position = i ; position>0 && A [ position - 1 ] > tmp ; position-- )
    26 {
    27 A [ position ] = A [ position - 1 ];
    28 }
    29 A [ position ] = tmp;
    30 }
    31 }
    32
    33
    34 void BubleSort ( vector < int > &A )
    35 {
    36 for ( int i = A.size()-1 ; i >0 ; i-- )
    37 {
    38 for ( int j = 0 ; j < i ; j++ )
    39 {
    40 if ( A [ j ] > A [ j+1 ] )
    41 {
    42 swap ( A [ j ] , A [ j+1 ] );
    43 }
    44 }
    45 }
    46 }
    47
    48 vector < int > ShellFunction ( int max )
    49 {
    50 vector < int > Increment;
    51 int i = 0 , j = 0;
    52 int a = 9*( ( int ) pow ( 4 , ( double ) i ) )-9*( ( int ) pow ( 2 , ( double ) i ) )+1;
    53 int b = ( ( int ) pow ( 4 , ( double ) j ) )-3*( ( int ) pow ( 2 , ( double ) j ) )+1;
    54 while ( a < max || b< max )
    55 {
    56 if ( a <= 0 )
    57 {
    58 i++;
    59 }
    60 else if ( b <= 0 )
    61 {
    62 j++;
    63 }
    64 else
    65 {
    66 if ( b < a )
    67 {
    68 Increment.push_back ( b );
    69 j ++;
    70 }
    71 else
    72 {
    73 Increment.push_back ( a );
    74 i++;
    75 }
    76 }
    77 a = 9*( ( int ) pow ( 4 , ( double ) i ) )-9*( ( int ) pow ( 2 , ( double ) i ))+1;
    78 b = ( ( int ) pow ( 4 , ( double ) j ) )-3*( ( int ) pow ( 2 , ( double ) j ) )+1;
    79 }
    80
    81 return Increment;
    82 }
    83
    84 void ShellSort ( vector < int > &A )
    85 {
    86 vector < int > Increment;
    87
    88 Increment = ShellFunction ( ( int ) A.size () );
    89
    90 for ( int increment = Increment.size() - 1 ; increment >=0 ; increment -- )
    91 {
    92 for ( int i = Increment [ increment ] ; i < ( int ) A.size () ; i ++ )
    93 {
    94 int tmp = A [ i ];
    95 int j;
    96 for ( j = i ; j >= Increment [ increment ] ; j-= Increment [ increment ] )
    97 {
    98 if ( tmp < A [ j - Increment [ increment ] ] )
    99 {
    100 A [ j ] = A [ j - Increment [ increment ] ];
    101 }
    102 else
    103 {
    104 break;
    105 }
    106 }
    107 A [ j ] = tmp;
    108 }
    109 }
    110 }
    111
    112
    113 void SelectSort ( vector < int > &A )
    114 {
    115 for ( int i = A.size()-1 ; i >0 ; i-- )
    116 {
    117 int max = 0 ;
    118 for ( int j = 0 ; j <= i ; j++ )
    119 {
    120 if ( A [ max ] < A [ j ] )
    121 {
    122 max = j;
    123 }
    124 }
    125 swap ( A [ max ] , A [ i ] );
    126 }
    127 }
    128
    129 void Merge ( vector < int > &A , vector < int > &tmp , int lbegin , int rbegin , int rend )
    130 {
    131 int LeftBegin = lbegin;
    132 int RightBegin = rbegin;
    133 int LeftEnd = rbegin - 1;
    134 int RightEnd = rend;
    135 int LeftPosition = LeftBegin;
    136 int RightPosition = RightBegin;
    137 int MergePosition = LeftBegin;
    138
    139 while ( LeftPosition <= LeftEnd && RightPosition <= RightEnd )
    140 {
    141 if ( A [ LeftPosition ] < A [ RightPosition ] )
    142 {
    143 tmp [ MergePosition ] = A [ LeftPosition ];
    144 MergePosition ++;
    145 LeftPosition ++;
    146 }
    147 else
    148 {
    149 tmp [ MergePosition ] = A [ RightPosition ];
    150 MergePosition ++;
    151 RightPosition ++;
    152 }
    153 }
    154
    155 while ( LeftPosition <= LeftEnd )
    156 {
    157 tmp [ MergePosition ] = A [ LeftPosition ];
    158 MergePosition ++;
    159 LeftPosition ++;
    160 }
    161
    162 while ( RightPosition <= RightEnd )
    163 {
    164 tmp [ MergePosition ] = A [ RightPosition ];
    165 MergePosition ++;
    166 RightPosition ++;
    167 }
    168
    169 for ( int i = LeftBegin ; i <= RightEnd ; i++ )
    170 {
    171 A [ i ] = tmp [ i ];
    172 }
    173
    174 }
    175
    176 void MSort ( vector < int > &A , vector < int > &tmp , int begin , int end )
    177 {
    178 if ( end - begin >= 1 )
    179 {
    180 MSort ( A , tmp , begin , ( begin + end ) / 2 );
    181 MSort ( A , tmp , ( begin + end ) / 2+1 , end );
    182 Merge ( A , tmp , begin , ( begin + end ) / 2+1 , end );
    183 }
    184 }
    185
    186
    187 void MergeSort ( vector < int > &A )
    188 {
    189 if ( ( int )A.size() > 1 )
    190 {
    191 vector < int > tmp ( A.size() , 0 );
    192 MSort ( A , tmp , 0 , ( int )A.size() - 1 );
    193 }
    194 }
    195
    196
    197 int Median3 ( vector < int > &A , int left , int right )
    198 {
    199 if ( right > left )
    200 {
    201 int center = ( right + left ) / 2;
    202 if ( A [ left ] > A [ center ] )
    203 {
    204 swap ( A [ left ] , A [ center ] );
    205 }
    206
    207 if ( A [ center ] > A [ right ] )
    208 {
    209 swap ( A [ center ] , A [ right ] );
    210 }
    211
    212 if ( A [ left ] > A [ center ] )
    213 {
    214 swap ( A [ left ] , A [ center ] );
    215 }
    216
    217 swap ( A [ center ] , A [ right - 1 ] );
    218
    219 return A [ right - 1 ];
    220 }
    221 else
    222 {
    223 return A [ right ];
    224 }
    225 }
    226
    227
    228 void QSort ( vector < int > &A , int begin , int end )
    229 {
    230 if ( end - begin > 1 )
    231 {
    232 int pivot = Median3 ( A , begin , end ); //pick the pivot
    233 int i = begin , j = end -1 ;
    234 for ( ; ; )
    235 {
    236 while ( A [ ++i ] < pivot )
    237 {}
    238 while ( A [ --j ] > pivot )
    239 {}
    240 if ( i < j )
    241 {
    242 swap ( A [ i ] , A [ j ] );
    243 }
    244 else
    245 {
    246 break;
    247 }
    248 }
    249 swap ( A[ i ] , A [ end - 1 ] );
    250
    251 QSort ( A , begin , i -1 );
    252 QSort ( A , i+1 , end );
    253 }
    254 else if ( end - begin == 1 )
    255 {
    256 if ( A [ begin ] > A [ end ] )
    257 {
    258 swap ( A [ begin ] , A [ end ] );
    259 }
    260 }
    261 }
    262
    263
    264 void QuickSort ( vector < int > &A )
    265 {
    266 if ( ( int ) A.size() > 5 ) // if size is small, we prefer insertion sort
    267 {
    268 QSort ( A , 0 , ( int )A.size() - 1 );
    269 }
    270 else
    271 {
    272 InsertionSort ( A );
    273 }
    274 }
    275
    276
    277 void RadixSort ( vector < int > &A , int digits )
    278 {
    279 for ( int i = 0 ; i < digits ; i++ )
    280 {
    281 vector < vector < int > > bucket ( 10 );
    282 for ( int bucketnum = 0 ; bucketnum < ( int )bucket.size() ; bucketnum++ )
    283 {
    284 bucket [ bucketnum ] .clear();
    285 }
    286 for ( int j = 0 ; j < ( int ) A.size() ; j++ )
    287 {
    288 int m = ( int )( A [ j ] / pow ( 10 , ( double ) i ) );
    289 m = m % 10;
    290 bucket [ m ].push_back ( A [ j ] );
    291 }
    292
    293 for ( int digit= 0 , position = 0 ; digit < 10 && position < ( int )A.size(); digit++ )
    294 {
    295 if ( !bucket [ digit ].empty() )
    296 {
    297 for ( int num = 0 ; num < ( int ) bucket [ digit ].size() ; num++ )
    298 {
    299 A [ position ] = bucket [ digit ] [ num ];
    300 position ++;
    301 }
    302 }
    303 }
    304 }
    305 }
    306
    307 int _tmain(int argc, _TCHAR* argv[])
    308 {
    309 vector < int > v;
    310 for ( int i = 0 ; i < 100 ; i ++ )
    311 {
    312 v.push_back ( rand() % 100 );
    313 }
    314
    315 for ( int i = 0 ; i < ( int )v.size() ; i++ )
    316 {
    317 cout << v [ i ] << endl;
    318 }
    319
    320
    321 cout << endl;
    322 MergeSort( v );
    323 for ( int i = 0 ; i < ( int )v.size() ; i++ )
    324 {
    325 cout << v [ i ] << endl;
    326 }
    327 cout << "done";
    328
    329 cout << endl;
    330 QuickSort( v );
    331 for ( int i = 0 ; i < ( int )v.size() ; i++ )
    332 {
    333 cout << v [ i ] << endl;
    334 }
    335 cout << "done";
    336
    337 cout << endl;
    338 RadixSort( v , 2 );
    339 for ( int i = 0 ; i < ( int )v.size() ; i++ )
    340 {
    341 cout << v [ i ] << endl;
    342 }
    343 cout << "done";
    344
    345
    346 getchar();
    347
    348 return 0;
    349 }
    350
    351
  • 相关阅读:
    微信开发 提示 Redirect_uri(错误10003)
    Windows激活
    百度区块链的数字宠物狗——莱茨狗
    导出数据之CSV[PHP]
    Mongo安装与使用
    开发神器之phpstorm破解与日常使用
    php常用方法
    开发工具之Sublime编辑器
    文件搜索神器之everything
    docker之基本命令(1)
  • 原文地址:https://www.cnblogs.com/kking/p/1850507.html
Copyright © 2011-2022 走看看