0002反转数组,0003冒泡排序,

0002
反转数组

#include<iostream>
using namespace std;

int main() {
	//数组元素逆置

	//创建数组
	int a[8] = { 1,3,5,6,7,9,66,45};
	for (int i = 0; i < 8; i++) {
		cout << "数组元素:" << a[i] << endl;
	}
	//数组元素个数求解  sizeof(a)/sizeof(a[0])-1

	//逆置
	//首元素
	int low = 0;
	//末尾元素
	int high = sizeof(a) / sizeof(a[0]) - 1;

	//交换
	while (low < high) {
		int temp = 0;
		temp = a[low];
		a[low] = a[high];
		a[high] = temp;
		low++;
		high--;	
		}
	//打印逆置数组
	for (int i = 0; i < 8; i++) {
		cout << a[i] << endl;
		}
	
	system("pause");
	return 0;
}

0003
// 冒泡排序 这个写的不太好,有点四班

#include<iostream>
using namespace std;

int main() {
	//冒泡排序


	//创建数组
	int a[8] = {1,3,5,6,7,9,66,45};

	//打印初始序列
	cout << "排序前" << endl;
	for (int i = 0; i <8; i++) {
		cout << a[i] << " " ;
	}
	cout << endl;
	
	//冒泡
	for (int i = 0; i < 8 - 1; i++) {
		//内层循环
		//对比次数:元素次数-轮数-1
		for (int j = 0; j < 8 - i - 1; j++) {
			//交换
			if (a[j] > a[j + 1]) {
				int temp = a[j];
				a[j] = a[j + 1];
				a[j + 1] = temp;
			}
		}
	}

	//输出排序后的数组
	cout << "排序后" << endl;
	for (int i = 0; i < 8; i++) {
		cout << a[i] << " " ;
	}
	cout << endl;
	system("pause");
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值