加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

【数据结构】直接插入排序

发布时间:2020-12-15 06:00:48 所属栏目:安全 来源:网络整理
导读:头文件: #include iostreamusing namespace std;#define MAX 10typedef struct{int r[MAX];}Sqlist;// 交换两个数void swap(int a,int b){int temp = a;a = b;b = temp;return;}// 比较大小void InsertSort(Sqlist sl,int n){for (int i = 1; i6; ++i){if (

头文件:


#include <iostream>
using namespace std;

#define MAX 10

typedef struct
{
	int r[MAX];
}Sqlist;


// 交换两个数
void swap(int &a,int &b)
{
	int temp = a;
	a = b;
	b = temp;
	return;
}

// 比较大小
void InsertSort(Sqlist &sl,int n)
{
	for (int i = 1; i<6; ++i)
	{
		if (sl.r[i] < sl.r[i - 1])
		{
			for (int j = i; j>0 && sl.r[j] < sl.r[j - 1]; --j)
			{
				swap(sl.r[j],sl.r[j - 1]);
			}
		}
	}
}


主函数:


#include "InsertSort.h"

int main()
{
	Sqlist sq = { 21,25,49,16,8 };
	InsertSort(sq,6);
	for (int i = 0; i < 6; ++i)
	{
		cout << sq.r[i] << " ";
	}
	cout << endl;
	return 0;
}


(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读