一年后再回头看系列之C/C++中的选择法排序、冒泡排序


前言

转眼大二了,突然感觉比大一还要迷茫(也可能是因为数模竞赛,评优都没有搞好,明年暑假的智能车也一点没有头绪23333),计二报的是Python(虽然没什么卵用,但是学校要我们搞非精准扶贫),在用多了自带的函数之后,突然想起去年让我相爱相杀的排序算法,emmm专业课刚好遇到了数据结构里面的各种排序,就想着过了一年再系统的整理一下,也是因为自己对于C太生疏了吧,虽然我不知道C学了个什么玩意儿,真正让自己有点感触的还是OOP,毕竟万物皆对象,行为皆方法。打算做一个系列,再回头看看自己曾经学过的东西,数电模电,线代高数等等。
打算以后多在CSDN、Github、头条丢一些大佬压根看不上的垃圾,说不定哪天大佬就出来教我做事了呢:(

本人超级菜,求求大佬们出来教我做事啊

一、两种排序算法的基本思想

1、冒泡法(起泡排序):
用到for循环,round1的两次for循环分别确定List[0]和List[1],现在我们就得到了两个相邻的元素,按照期望的序列决定是较小的数上浮还是较大的数上浮,一直到List[length_List](以升序为例),此时相当于找到了序列0-length_List的最大值;round2的两次for循环分别确定List[1]和List[2],以此类推一直到List[length_List-1],此时找到了序列0-length_List-1的最大值…依次往后直到确定List[0]与List[1]大小关系,此时程序结束,如下所示:

for(int j=len-1;j>i;j--)
		{
			if(name_List[j]<name_List[j-1])
			{
				n++;
				Exchange(&name_List[j],&name_List[j-1]);
				Flag=true;
                //After the exchange is performed, the bool value changes
			}
		}
before:5 8 9 6 3 7 .....(5 8) 9 6 3 7 .....5 (8 9) 6 3 7 .....5 8 (6 9) 3 7 .....5 8 6 (3 9) 7 .....5 8 6 3 (7 9) Round 1 finish... 其实就相当于把数组拆分,长度依次减去一,每次操作的结果就是将对应数组的最小值(最大值)放在一端,当程序结束,相当于完成了排序。 2、选择法排序: 其实原理和冒泡排序类似,只不过不再是相邻两个比较,而是直接在拆分好的子序列里面找到最值,并放到最前面,这就好理解为什么同样一个序列,选择法排序执行次数要比冒泡排序少很多:
Index_max=i;
        for(int j=i+1;j<len;j++)
        {
            n++;
            if(name_List[Index_max]<name_List[j])
            {
                Index_max=j;
            }
        }
        if(Index_max!=i)
        {
            Exchange(&name_List[Index_max],&name_List[i]);
        }
        /*
        Suppose the sequence is :| 5 2 8 6 4 7 3
        In first round the maximum is 8,i=0,put it on location 0,and the new sequence is :8 | 5 2 6 4 7 3
        In second round the maximum is 7,i=1,put it on location 1,and the new sequence is 8 7 | 5 2 6 4 3
        ....
        */

二、具体步骤

1.引入库

由于生成数列用的是随机数方式,而且为了保证每次运行程序所得到的结果都不一样,用到了以下库:

# include <iostream>
# include <stdlib.h>
# include <time.h>

2.生成随机数

参考大佬的文章后用了动态生成随机数以及动态分配数组长度的方法:

int *List=new int[length_List];
			srand((unsigned int)time(NULL));
			for(int index=0;index<length_List;index++)
			{
				List[index]=rand()%100+1;
			}

结果每次运行程序都会得到不同的0-100之间的随机数组

三.具体代码

冒泡排序

/*
Create a random list,sort it with upwards and downwards sequence
*/
# include <iostream>
# include <stdlib.h>
# include <time.h>
using namespace std;
/*
Bubble sort:
Basic idea:Compare adjacent elements in turn,smaller go up(upwards sort) and bigger go down;or bigger go up(downwards sort)
Each turn we compare position and position->next,if position and it's next meet the requests,we don't need to do it again
So the algorithm can be optimized:Because each "Bubble" actually is switch the value
We could set a BOOL value as a key,each compare initialize the key to false,and after comparing turn it to true
After comparing check key value,if it's value is false interrupt the program
*/
/*
BubbleSort1 is upwards sort
BubbleSort2 is downwards sort
*/
void Exchange(int *a,int *b)
{
	int temp;
	temp=*a;
	*a=*b;
	*b=temp;
}
//Swap() is a place that is easy to ignore. Exchange values are exchanged addresses, or exchange their respective references (alias)
void BubbleSort1(int name_List[],int length_List)
{
	int m=0,n=0;
	int len=length_List;
	bool Flag;
    //Threshold(key), if a certain time has been sorted, there is no need to continue to traverse down
	for(int i=0;i<len;i++)
	{
		m++;
		Flag=false;
		for(int j=len-1;j>i;j--)
		{
			if(name_List[j]<name_List[j-1])
			{
				n++;
				Exchange(&name_List[j],&name_List[j-1]);
				Flag=true;
                //After the exchange is performed, the bool value changes
			}
		}
		if(!Flag) break;
        //If the bool value has not changed, there is no exchange, jumping out of this cycle, that is to say, this time the order meets the requirements
		/*
		if(Flag!=true)
		break;
		*/
	}
	cout<<"Sort the list with the format of up is :"<<endl;
	for(int i=0;i<len;i++)
	{
		cout<<name_List[i]<<"  ";
	}
	cout<<"Bool time :"<<m*n<<endl;
    /*
    Just like this:
    before:5 8 9 6 3 7
    .....(5 8) 9 6 3 7
    .....5 (8 9) 6 3 7
    .....5 8 (6 9) 3 7
    .....5 8 6 (3 9) 7
    .....5 8 6 3 (7 9)
    Round 1 finish...
    */
}
void BubbleSort2(int name_List[],int length_List)
{
	int m=0,n=0;
	int len=length_List;
	bool Flag;
	for(int i=0;i<len;i++)
	{
		m++;
		Flag=false;
		for(int j=len-1;j>i;j--)
		{
			if(name_List[j]>name_List[j-1])
			{
				n++;
				Exchange(&name_List[j],&name_List[j-1]);
				Flag=true;
			}
		}
		if(!Flag) break;	
		
	}
	cout<<"Sort the list with the format of down is :"<<endl;
	for(int i=0;i<len;i++)
	{
		cout<<name_List[i]<<"  ";
	}
	cout<<"Bool time :"<<m*n<<endl;
}
int main(void)
{
	int length_List;
	int Flag=1;
	while(Flag)
	{
		cout<<"Enter the length of random list (length>=10):";
		cin>>length_List;
		if(length_List<10)
		{
			Flag=0;
			cout<<"Exit by fault input"<<endl;
			break;
		}
		else
		{
			int *List=new int[length_List];
			srand((unsigned int)time(NULL));
			for(int index=0;index<length_List;index++)
			{
				List[index]=rand()%100+1;
			}
			cout<<"Original sort :"<<endl;
			for(int index=0;index<length_List;index++)
			{
				cout<<List[index]<<" ";
			}
			cout<<"Upwards sort :"<<endl;
			BubbleSort1(List,length_List);
			cout<<"Downwards sort :"<<endl;
			BubbleSort2(List,length_List);
			cout<<"Type 1 to continue and 0 to exit :";
			cin>>Flag;
		}
	}
	return 0;
}

选择法排序

/*
Upwards
First round,find minimum value of List[length_list],put it on first location index
Second round,find minimum value of List[length_list],put it on first location index+1
...
Downwards
First round,find maximum value of List[length_list],put it on first location index
Second round,find maximum value of List[length_list],put it on first location index+1
...
*/
# include <iostream>
# include <stdlib.h>
# include <time.h>
using namespace std;
void Exchange(int *a,int *b)
{
    int temp;
    temp=*a;
    *a=*b;
    *b=temp;
}
/*
SelectionSort1() is upwards
SelectionSort2() is downwards
*/
void SelectionSort1(int name_List[],int length_List)
{
    int len=length_List;
    int Index_min;
    int m=0,n=0;
    for(int i=0;i<len;i++)
    {
        m++;
        Index_min=i;
        for(int j=i+1;j<len;j++)
        {
            n++;
            if(name_List[Index_min]>name_List[j])
            {
                Index_min=j;
            }
        }
        if(Index_min!=i)
        {
            Exchange(&name_List[Index_min],&name_List[i]);
        }
        /*
        Suppose the sequence is :| 5 2 8 6 4 7 3
        In first round the minimum is 2,i=0,put it on location 0,and the new sequence is :2 | 5 8 6 4 7 3
        In second round the minimum is 3,i=1,put it on location 1,and the new sequence is 2 3 | 5 8 6 4 7
        ....
        */
    }
    cout<<"The upwards sort is :"<<endl;
    for(int index=0;index<length_List;index++)
    {
        cout<<name_List[index]<<" ";
    }
    cout<<endl;
    cout<<"Number of executions :"<<m*n<<endl;
}
void SelectionSort2(int name_List[],int length_List)
{
    int len=length_List;
    int Index_max;
    int m=0,n=0;
    for(int i=0;i<len;i++)
    {
        m++;
        Index_max=i;
        for(int j=i+1;j<len;j++)
        {
            n++;
            if(name_List[Index_max]<name_List[j])
            {
                Index_max=j;
            }
        }
        if(Index_max!=i)
        {
            Exchange(&name_List[Index_max],&name_List[i]);
        }
        /*
        Suppose the sequence is :| 5 2 8 6 4 7 3
        In first round the maximum is 8,i=0,put it on location 0,and the new sequence is :8 | 5 2 6 4 7 3
        In second round the maximum is 7,i=1,put it on location 1,and the new sequence is 8 7 | 5 2 6 4 3
        ....
        */
    }
    cout<<"The downwards sort is :"<<endl;
    for(int index=0;index<length_List;index++)
    {
        cout<<name_List[index]<<" ";
    }
    cout<<endl;
    cout<<"Number of executions :"<<m*n<<endl;
}
int main(void)
{
	int length_List;
	int Flag=1;
	while(Flag)
	{
		cout<<"Enter the length of random list (length>=10):";
		cin>>length_List;
		if(length_List<10)
		{
			Flag=0;
			cout<<"Exit by fault input"<<endl;
			break;
		}
		else
		{
			int *List=new int[length_List];
			srand((unsigned int)time(NULL));
			for(int index=0;index<length_List;index++)
			{
				List[index]=rand()%100+1;
			}
			cout<<"Original sort :"<<endl;
			for(int index=0;index<length_List;index++)
			{
				cout<<List[index]<<" ";
			}
			cout<<endl;
			SelectionSort1(List,length_List);
			cout<<endl;
			SelectionSort2(List,length_List);
			cout<<"Type 1 to continue and 0 to exit :";
			cin>>Flag;
		}
	}
	return 0;
}

到最后还是感觉选择法排序更好理解,也可能确实是对一年前学的C太生疏了吧,下次再整理一下其他的排序算法。路还长啊…

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值