3.选择排序
3.1 简单选择排序
实例
3 2 1 23 6 8 7 5 15 9
1 2 3 23 6 8 7 5 15 9
1 2 3 23 6 8 7 5 15 9
1 2 3 23 6 8 7 5 15 9
1 2 3 5 6 8 7 23 15 9
1 2 3 5 6 8 7 23 15 9
1 2 3 5 6 7 8 23 15 9
1 2 3 5 6 7 8 23 15 9
1 2 3 5 6 7 8 9 15 23
1 2 3 5 6 7 8 9 15 23
代码
SqList SelectSort(SqList L,int n)
{
//i 从 0 至 n-2 遍历
//j 从 i+1 至 n-1遍历,找到最小位置
// 最小位置与i位置数据交换
int min;
ElemType e;
for(int i =0;i<n-1;i++)
{
min = i;
for(int j = i+1;j<n;j++)
{
if(L.data[j] < L.data[min])
min = j;
}
if(min!=i)
{
e = L.data[i];
L.data[i] = L.data[min];
L.data[min] = e;
}
PrintList("select sort step",L);
}
return L;
}
结果
Input:
l1
3 2 1 23 6 8 7 5 15 9
l2
10 9 8 7 6 5 4 3 2 1
l3
1 2 3 4 5 6 7 8 9 10
====== Select Sort =====
select sort step
1 2 3 23 6 8 7 5 15 9
select sort step
1 2 3 23 6 8 7 5 15 9
select sort step
1 2 3 23 6 8 7 5 15 9
select sort step
1 2 3 5 6 8 7 23 15 9
select sort step
1 2 3 5 6 8 7 23 15 9
select sort step
1 2 3 5 6 7 8 23 15 9
select sort step
1 2 3 5 6 7 8 23 15 9
select sort step
1 2 3 5 6 7 8 9 15 23
select sort step
1 2 3 5 6 7 8 9 15 23
select sort step
1 9 8 7 6 5 4 3 2 10
select sort step
1 2 8 7 6 5 4 3 9 10
select sort step
1 2 3 7 6 5 4 8 9 10
select sort step
1 2 3 4 6 5 7 8 9 10
select sort step
1 2 3 4 5 6 7 8 9 10
select sort step
1 2 3 4 5 6 7 8 9 10
select sort step
1 2 3 4 5 6 7 8 9 10
select sort step
1 2 3 4 5 6 7 8 9 10
select sort step
1 2 3 4 5 6 7 8 9 10
select sort step
1 2 3 4 5 6 7 8 9 10
select sort step
1 2 3 4 5 6 7 8 9 10
select sort step
1 2 3 4 5 6 7 8 9 10
select sort step
1 2 3 4 5 6 7 8 9 10
select sort step
1 2 3 4 5 6 7 8 9 10
select sort step
1 2 3 4 5 6 7 8 9 10
select sort step
1 2 3 4 5 6 7 8 9 10
select sort step
1 2 3 4 5 6 7 8 9 10
select sort step
1 2 3 4 5 6 7 8 9 10
性能分析
空间效率 | 时间复杂度 | 稳定性 |
---|---|---|
O ( 1 ) O(1) O(1) | O ( n 2 ) O(n^2) O(n2) | 不稳定 |
3.2 堆排序
实例
3 2 1 23 6 8 7 5 15 9 堆排序
i = 5
adjust down
i = 4
adjust down
i = 3
adjust down
i = 2
adjust down
i = 1
adjust down
堆排序
i = 10
adjust down
i = 9
adjust down
i = 8
adjust down
i = 7
adjust down
i = 6
adjust down
i = 5
adjust down
i = 4
adjust down
i = 3
adjust down
i = 2
adjust down
代码
void AdjustDown(SqList &L,int k,int len)
{
L.data[0] = L.data[k];
for(int i = 2*k;i<=len;i*=2)
{
if(i<len&&L.data[i]<L.data[i+1])
i++;
if(L.data[0] >= L.data[i])
break;
else
{
L.data[k] = L.data[i];
k=i;
}
}
L.data[k] = L.data[0];
}
SqList BuildMaxHeap(SqList L,int len)
{
for(int i=len/2;i>0;i--)
{
AdjustDown(L,i,len);
}
return L;
}
SqList HeapSort(SqList L,int len)
{
ElemType e;
L=BuildMaxHeap(L,len);
for(int i=len;i>1;i--)
{
e = L.data[i];
L.data[i] = L.data[1];
L.data[1] = e;
AdjustDown(L,1,i-1);
PrintList("heap sort",L);
}
return L;
}
结果
Input:
l1
3 2 1 23 6 8 7 5 15 9
l2
10 9 8 7 6 5 4 3 2 1
l3
1 2 3 4 5 6 7 8 9 10
=======[0] sentry========
l1
2020 3 2 1 23 6 8 7 5 15 9
l2
2020 10 9 8 7 6 5 4 3 2 1
l3
2020 1 2 3 4 5 6 7 8 9 10
===============
Max Heap
3 23 15 8 5 9 1 7 3 2 6
heap sort
6 15 9 8 5 6 1 7 3 2 23
heap sort
2 9 6 8 5 2 1 7 3 15 23
heap sort
3 8 6 7 5 2 1 3 9 15 23
heap sort
3 7 6 3 5 2 1 8 9 15 23
heap sort
1 6 5 3 1 2 7 8 9 15 23
heap sort
2 5 2 3 1 6 7 8 9 15 23
heap sort
1 3 2 1 5 6 7 8 9 15 23
heap sort
1 2 1 3 5 6 7 8 9 15 23
heap sort
1 1 2 3 5 6 7 8 9 15 23
Max Heap
10 10 9 8 7 6 5 4 3 2 1
heap sort
1 9 7 8 3 6 5 4 1 2 10
heap sort
2 8 7 5 3 6 2 4 1 9 10
heap sort
1 7 6 5 3 1 2 4 8 9 10
heap sort
4 6 4 5 3 1 2 7 8 9 10
heap sort
2 5 4 2 3 1 6 7 8 9 10
heap sort
1 4 3 2 1 5 6 7 8 9 10
heap sort
1 3 1 2 4 5 6 7 8 9 10
heap sort
2 2 1 3 4 5 6 7 8 9 10
heap sort
1 1 2 3 4 5 6 7 8 9 10
Max Heap
1 10 9 7 8 5 6 3 1 4 2
heap sort
2 9 8 7 4 5 6 3 1 2 10
heap sort
2 8 5 7 4 2 6 3 1 9 10
heap sort
1 7 5 6 4 2 1 3 8 9 10
heap sort
3 6 5 3 4 2 1 7 8 9 10
heap sort
1 5 4 3 1 2 6 7 8 9 10
heap sort
2 4 2 3 1 5 6 7 8 9 10
heap sort
1 3 2 1 4 5 6 7 8 9 10
heap sort
1 2 1 3 4 5 6 7 8 9 10
heap sort
1 1 2 3 4 5 6 7 8 9 10
性能分析
空间效率 | 时间复杂度 | 稳定性 |
---|---|---|
O ( 1 ) O(1) O(1) | O ( n l o g 2 n ) O(nlog_2^n) O(nlog2n) | 不稳定 |