数据结构复习:排序算法——3.选择排序

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 堆排序

3
2
1
23
6
8
7
5
15
9
建堆前: [ ] 3 2 1 23 6 8 7 5 15 9

i = 5
adjust down

3
2
1
23
9
8
7
5
15
6

i = 4
adjust down

3
2
1
23
9
8
7
5
15
6

i = 3
adjust down

3
2
8
23
9
1
7
5
15
6

i = 2
adjust down

3
23
8
15
9
1
7
5
2
6

i = 1
adjust down

23
15
8
5
9
1
7
3
2
6
23
15
8
5
9
1
7
3
2
6
建堆后:[ ] 23 15 8 5 9 1 7 3 2 6

堆排序

i = 10
adjust down

15
9
8
5
6
1
7
3
2
23
6 15 9 8 5 6 1 7 3 2 23

i = 9
adjust down

9
6
8
5
2
1
7
3
15
23
2 9 6 8 5 2 1 7 3 15 23

i = 8
adjust down

8
6
7
5
2
1
3
9
15
23
3 8 6 7 5 2 1 3 9 15 23

i = 7
adjust down

7
6
3
5
2
1
8
9
15
23
3 7 6 3 5 2 1 8 9 15 23

i = 6
adjust down

6
5
3
1
2
7
8
9
15
23
1 6 5 3 1 2 7 8 9 15 23

i = 5
adjust down

5
2
3
1
6
7
8
9
15
23
2 5 2 3 1 6 7 8 9 15 23

i = 4
adjust down

3
2
1
5
6
7
8
9
15
23
1 3 2 1 5 6 7 8 9 15 23

i = 3
adjust down

2
1
3
5
6
7
8
9
15
23
1 2 1 3 5 6 7 8 9 15 23

i = 2
adjust down

1
2
3
5
6
7
8
9
15
23
1 1 2 3 5 6 7 8 9 15 23

代码

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)不稳定
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值