Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 92aa554

Browse files
committed
插入排序
1 parent ecd9e7d commit 92aa554

File tree

6 files changed

+154
-4
lines changed

6 files changed

+154
-4
lines changed

‎CMakeLists.txt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ set(CMAKE_C_STANDARD 99)
44

55

66

7-
add_executable(dataStruct main.c tools.c tools.h Tree/BinTree.c Tree/BinTree.h Tree/HFTree.c Tree/HFTree.h Graph/AdjGraph.c Graph/AdjGraph.h Graph/AdjLinkedList.c Graph/AdjLinkedList.h Search/LinklistSearch.c Search/LinklistSearch.h)
7+
add_executable(dataStruct main.c tools.c tools.h Tree/BinTree.c Tree/BinTree.h Tree/HFTree.c Tree/HFTree.h Graph/AdjGraph.c Graph/AdjGraph.h Graph/AdjLinkedList.c Graph/AdjLinkedList.h Search/LinklistSearch.c Search/LinklistSearch.hSort/InsertSort.c Sort/InsertSort.h)

‎Search/LinklistSearch.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,11 @@ void testBSearch()
8585
for (int i = 1; i < N; ++i) {
8686
List[i] = i;
8787
}
88-
int key = 74;
88+
int key = 714;
8989

9090
int index = BinSearch(List, key, 1, N-1);
9191

92-
printf("查找的index为:%d\n", index);
92+
printf("查找的index为:%d\n", index);
9393
index = RBinSearch(List, key, 1, N-1);
9494
printf("递归查找的index为:%d\n", index);
9595

‎Sort/InsertSort.c‎

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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

‎Sort/InsertSort.h‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//
2+
// Created by ys on 2019年4月25日.
3+
//
4+
5+
#ifndef DATASTRUCT_INSERTSORT_H
6+
#define DATASTRUCT_INSERTSORT_H
7+
#define ElemType int
8+
//直接插入排序,插入操作
9+
void straipass(ElemType *SqList, ElemType key, int len);
10+
//直接插入排序
11+
void straiSort(ElemType *SqList, int len);
12+
13+
//折半插入排序
14+
void binInsertSort(ElemType *SqList, int len);
15+
16+
//二路插入排序
17+
void twoSort(ElemType *SqList, int len);
18+
19+
//希尔排序
20+
21+
void shellSort(ElemType *SqList, int len);
22+
23+
void bstraipass(ElemType *SqList, ElemType key, int len);
24+
25+
void testSort();
26+
#endif //DATASTRUCT_INSERTSORT_H

‎tools.c‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,3 +135,29 @@ void printDebug()
135135
}
136136

137137

138+
void printToFile(char *fname, ElemType *arr, int arrLen)
139+
{
140+
FILE *fp;
141+
fp = fopen(fname, "w");
142+
for (int j = 1; j <= arrLen ; ++j) {
143+
fprintf(fp, "%10d", arr[j]);
144+
if((j % 10)==0)
145+
{
146+
fprintf(fp, "\n");
147+
148+
}
149+
}
150+
fclose(fp);
151+
}
152+
153+
154+
void testRand()
155+
{
156+
for (int i = 1; i <= 20; ++i) {
157+
printf("%8d",randint(1,10000));
158+
if((i%10)==0)
159+
{
160+
printf("\n");
161+
}
162+
}
163+
}

‎tools.h‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#include "Tree/HFTree.h"
1111
#include "Graph/AdjLinkedList.h"
1212
#include "Graph/AdjGraph.h"
13-
13+
#include"Sort/InsertSort.h"
1414
#include "Search/LinklistSearch.h"
1515

1616

@@ -57,4 +57,9 @@ void readData();
5757
//冒泡排序
5858
void bubbleSort(int a[], int n);
5959

60+
61+
void printToFile(char *fname, ElemType *arr, int arrLen);
62+
63+
void testRand();
64+
6065
#endif //DATASTRUCT_TOOLS_H

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /