1+ //
2+ // Created by ys on 2019年4月25日.
3+ //
4+ #ifndef DATASTRUCT_INSERTSORT_C
5+ #define DATASTRUCT_INSERTSORT_C
6+ 7+ 8+ #include "InsertSort.h"
9+ #include "../tools.h"
10+ 11+ //直接插入排序,插入操作
12+ void straipass (ElemType * SqList , ElemType key , int len )
13+ {
14+ SqList [0 ] = key ;
15+ int pos = len ;
16+ while (key < SqList [pos ])
17+ {
18+ SqList [pos + 1 ] = SqList [pos ];
19+ pos -- ;
20+ }
21+ SqList [pos + 1 ] = key ;
22+ }
23+ //直接插入排序
24+ void straiSort (ElemType * SqList , int len )
25+ {
26+ for (int i = 2 ; i < len + 1 ; ++ i ) {
27+ straipass (SqList , SqList [i ], i - 1 );
28+ }
29+ }
30+ void bstraipass (ElemType * SqList , ElemType key , int len )
31+ {
32+ SqList [0 ] = key ;
33+ int low = 1 , high = len ;
34+ int mid ;
35+ while (low <= high )
36+ {
37+ mid = (low + high ) / 2 ;
38+ if (SqList [mid ] < key )
39+ {
40+ low = mid + 1 ;
41+ } else
42+ {
43+ high = mid - 1 ;
44+ }
45+ }
46+ 47+ for (int i = len ; i > high + 1 ; -- i ) {
48+ SqList [i + 1 ] = SqList [i ];
49+ }
50+ SqList [high + 1 ] = key ;
51+ 52+ }
53+ //折半插入排序
54+ void binInsertSort (ElemType * SqList , int len )
55+ {
56+ for (int i = 2 ; i < len + 1 ; ++ i ) {
57+ bstraipass (SqList , SqList [i ], i - 1 );
58+ }
59+ }
60+ 61+ //二路插入排序
62+ void twoSort (ElemType * SqList , int len );
63+ 64+ //希尔排序
65+ 66+ void shellSort (ElemType * SqList , int len );
67+ 68+ void testSort ()
69+ {
70+ int start ,end ;
71+ const int N = 100 * 1024 ;
72+ ElemType * SqList = (int * )malloc (sizeof (int )* (N + 1 ));
73+ for (int i = 1 ; i <= N ; ++ i ) {
74+ SqList [i ] = randint (0 , 2 * N );
75+ }
76+ printf ("%d,%d,%d" ,SqList [1 ],SqList [2 ],SqList [3 ]);
77+ char * fname = "../data/unsort.txt" ;
78+ char * fSortname = "../data/sort.txt" ;
79+ 80+ printToFile (fname , SqList , N );
81+ 82+ 83+ start = clock ();
84+ binInsertSort (SqList , N );
85+ end = clock ();
86+ printf ("排序用时:%dms\n" , end - start );
87+ 88+ printToFile (fSortname , SqList , N );
89+ free (SqList );
90+ }
91+ 92+ 93+ #endif //DATASTRUCT_INSERTSORT_H
0 commit comments