本文内容会根据博主所需进行更新,希望大家多多关照。
直接插入排序
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
void InsertSort( int r[])
{
int n = sizeof (r) / sizeof (r[0]);
for ( int i = 1; i < n; ++i)
{
for ( int j = i - 1; j >= 0; --j)
{
if (r[j+1] < r[j])
{
int s = r[j+1];
r[j+1] = r[j];
r[j] = s;
}
}
}
}
|
折半插入排序
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
void BinInsertSort( int r[])
{
int n = sizeof (r) / sizeof (r[0]);
for ( int i = 1; i < n; ++i)
{
int s = r[i];
int low = 0;
int high = i - 1;
while (low <= high)
{
int mid = (low + high) / 2; //mid位置为要找的数
if (s < r[mid])
high = mid - 1;
else
low = mid + 1;
}
for ( int j = i - 1; j >= high + 1; --j) //high+1即mid,执行数的后移,直到mid的数后移
r[j+1] = r[j];
r[high+1] = s; //mid位置就存放本身
}
}
|
希尔排序
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
void ShellSort( int r[])
{
int n = sizeof (r) / sizeof (r[0]);
int step = n / 2;
while (step >= 1)
{
for ( int i = step; i < n; ++i)
{
for ( int j = i - step; j >= 0; j -= step)
{
if (r[j+step] < r[j])
{
int s = r[j+step];
r[j+step] = r[j];
r[j] = s;
}
}
}
step /= 2;
}
}
|
直接选择排序
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
void SelectSort( int r[])
{
int n = sizeof (r) / sizeof (r[0]);
for ( int i = 0; i < n - 1; ++i)
{
int samll = i;
for ( int j = i + 1; j < n; ++j)
{
if (r[small] > r[j])
samll = j;
}
if (small != i)
{
int s = r[i];
r[i] = r[small];
r[small] = s;
}
}
}
|
堆排序
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
void HeapAdjust( int r[]; int i; int j) //调整堆
{
int child = 2 * i;
int s = r[i]; //s临时存放结点数据
while (child <= j)
{
if (child < j && r[child+1] > r[child]) //比较2个子树
++child;
if (s >= r[child]) //结点与大子树比较
break ;
r[child/2] = r[child]; //如果大子树比结点大,互换
child = 2 * child; //继续向子树检索
}
r[child/2] = s; //结点的数为最大的数
}
void HeapSort( int r[]) //建堆
{
int n = sizeof (r) / sizeof (r[0]);
for ( int i = n / 2 - 1; i >= 0; --i) //只有n/2-1前的下标才有子树
{
HeapAdjust(r, i, n - 1); //构造大顶堆,结点都比子树大,最后根节点为最大的数
}
for ( int i = n - 1; i > 0; --i)
{
//将当前堆顶元素与当前堆尾元素互换,即将最大的数移到末尾
int s = r[0];
r[0] = r[i];
r[i] = s;
HeapAdjust(r, 0, i -1); //将剩下的元素继续调整,最后变成由小到大的顺序
}
}
|
冒泡排序
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
void BubbleSort( int r[])
{
int n = sizeof (r) / sizeof (r[0]);
for ( int i = 0; i < n - 1; ++i)
{
for ( int j = 0; j < n - 1 - i; ++j)
{
if (r[j] > r[j+1])
{
int s = r[j];
r[j] = r[j+1];
r[j+1] = s;
}
}
}
}
|
快速排序
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
int Partition( int r[], int low, int high)
{
int pivotkey = r[low];
int i = low;
int j = high;
while (i < j)
{
while (i < j && r[j] > pivotkey) //从j往前找,找出第一个比pivotkey小的数
--j;
if (i < j)
{
r[i] = r[j];
++i;
}
while (i < j && r[i] < pivotkey) //从i往后找,找出第一个比pivotkey大的数
++i;
if (i < j)
{
r[j] = r[i];
--j;
}
}
r[j] = pivotkey; //完成最终交换
return j; //返回分界点,前面的数都比pivotkey小,后面的数都比pivokey大
}
void QuickSort( int r[], int low, int high) // 传数组、0和长度-1
{
if (low < high)
{
int pivot = Partition(r, low, high);
QuickSort(r, low, pivot - 1); //递归,前半部分继续进行快速排序
QuickSort(r, pivot + 1; high); //递归,后半部分继续进行快速排序
}
}
|
归并排序
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
void copyArray( int source[], int dest[], int len, int first)
{
int i;
int j = first;
for (i = 0; i < len; ++i)
{
dest[j] = source[i];
++j;
}
}
void merge( int a[], int left, int right)
{
int begin1 = left;
int mid = (left + right) / 2;
int begin2 = mid + 1;
int newArrayLen = right - left + 1;
int *b = ( int *) malloc (newArrayLen * sizeof ( int ));
int k = 0;
while (begin1 <= mid && begin2 <= right) //找出2组中比较后小的那个数按顺序放进b空间
{
if (a[begin1] <= a[begin2])
b[k++] = a[begin1++];
else
b[k++] = a[begin2++];
}
//把剩下的数放进b空间
while (begin1 <= mid)
b[k++] = a[begin1++];
while (begin2 <= right)
b[k++] = a[begin2++];
copyArray(b, a, newArrayLen, left); //把b空间的数写回原数组
free (b);
}
void MergeSort( int r[], int left, int right) //传数组、0和长度-1
{
int i;
//至少有两个元素才进行排序
if (left < right)
{
i = (left + right) / 2;
MergeSort(r, left, i); //前半部分递归
MergeSort(r, i + 1, right); //后半部分递归
merge(r, left, right); //10个数的比较顺序为[0,1][0,2][3,4][0,4][5,6][5,7][8,9][5,9][0,9]
}
}
|
桶排序
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
void insert(list< int > &bucket, int val)
{
auto iter = bucket.begin();
while (iter != bucket.end() && val >= *iter)
++iter;
//insert会在iter之前插入数据,这样可以稳定排序
bucket.insert(iter, val);
}
void BuckdeSort(vector< int > &arr)
{
int len = arr.size();
if (len <= 1)
return ;
int min = arr[0], max = min;
for ( int i = 1; i < len; ++i) //找出最小值和最大值
{
if (min > arr[i])
min = arr[i];
if (max < arr[i])
max = arr[i];
}
int k = 10; //桶的大小
//向上取整,例如[0,9]有10个数,就有(9-0)/k+1=1个桶
int bucketsNum = (max - min) / k + 1; //桶的个数
vector<list< int >> buckets(bucketsNum);
for ( int i = 0; i < len; ++i)
{
int value = arr[i];
//(value-min)/k就是在哪个桶里面
insert(buckets[(value-min)/k], value); //将数据放到各个桶里并排序
}
int index = 0;
for ( int i = 0; i < bucketsNum; ++i)
{
if (buckets[i].size())
{
for (auto &value : buckets[i])
arr[index++] = value;
}
}
}
|
到此这篇关于C++九种排序具体实现代码的文章就介绍到这了,更多相关C++ 排序内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://segmentfault.com/a/1190000022297110