一维数组

一维数组

  1. 找最大值 Getmax(int a[],int n)
  2. 选择排序 selectsort(int a[],int n)
  3. 冒泡排序 bubblesort(int a[],int n)
  4. 折半查找某元素的位置(二分法查找) binarysearch(int a[],int n)

找最大值

int Getmax(int a[], int n)
{
	int pos, temp;
	pos = 0;
	for (int i = 1; i < n; i++)
		if (a[i] > a[pos])
			pos = i;
	return a[pos];
}

选择排序

void selectsort(int a[], int n)
{
	int pos, temp;
	for (int trip = 0; trip < n; trip++)
	{
		pos = trip;
		for (int i = trip + 1; i < n; i++)      //注意要是i=trip+1
			if (a[i] > a[pos])
				pos = i;                 //此循环为找出最大的值,并标记位置
		temp = a[trip];
		a[trip] = a[pos];
		a[pos] = temp;              //将最大值往最左移            
	}                      //此循环为将数组从大到小排序
	for (int i = 0; i < n; i++)
		cout << a[i] << " ";
	cout << endl;
}

冒泡排序

void bubblesort(int a[], int n)
{
	int trip, i, temp;
	bool noswap = true;
	for (trip = 1; trip <= n - 1 && noswap; trip++) 
	{
		noswap = false;
		for(i=0;i<n-trip;i++)
			if (a[i + 1] > a[i]) 
			{
				temp = a[i];
				a[i] = a[i + 1];
				a[i + 1] = a[i];
				noswap = true;
			}
	}
	for (int i = 0; i < n; i++)
		cout << a[i] << " ";
	cout << endl;
}

折半查找某元素的位置(二分法查找)
必须是有序数组

int banarysearch(int a[], int n, int x)    //数组从大到小排列
{
	int bot, top, mid;
	bot = 0;
	top = n - 1;
	while (bot < top)
	{
		mid = (top + bot) / 2;
		if (x > a[mid])
		{
			top = mid - 1;
		}			           //若x > a[mid],x在右半边
		else if (x < a[mid])
		{
			bot = mid + 1;
		}			           //若x < a[mid],x在左半边
		else
			return mid;
	}
	if (top < bot)
			return -1;
	else
		return mid;
}

总代码

#include <iostream>
#include <iomanip>
using namespace std;
void input(int a[], int n)
{
	for (int i = 0; i < n; i++)
		cin >> a[i];
}
void output(int a[], int n)
{
	for (int i = 0; i < n; i++)
		cout << a[i]<<" ";
	cout << endl;
}
int Getmax(int a[], int n)
{
	int pos;
	pos = 0;
	for (int i = 1; i < n; i++)
		if (a[i] > a[pos])
			pos = i;
	return a[pos];
}
void selectsort(int a[], int n)
{
	int pos, temp;
	for (int trip = 0; trip < n; trip++)
	{
		pos = trip;
		for (int i = trip + 1; i < n; i++)
			if (a[i] > a[pos])
				pos = i;
		temp = a[trip];
		a[trip] = a[pos];
		a[pos] = temp;
	}
	for (int i = 0; i < n; i++)
		cout << a[i] << " ";
	cout << endl;
}
void bubblesort(int a[], int n)
{
	int trip, i, temp;
	bool noswap = true;
	for (trip = 1; trip <= n - 1 && noswap; trip++) 
	{
		noswap = false;
		for(i=0;i<n-trip;i++)
			if (a[i + 1] > a[i]) {
				temp = a[i];
				a[i] = a[i + 1];
				a[i + 1] = a[i];
				noswap = true;
			}
	}
	for (int i = 0; i < n; i++)
		cout << a[i] << " ";
	cout << endl;
}
int banarysearch(int a[], int n, int x)    //数组从大到小排列
{
	int bot, top, mid;
	bot = 0;
	top = n - 1;
	while (bot < top)
	{
		mid = (top + bot) / 2;
		if (x > a[mid])
		{
			top = mid - 1;
		}			           //若x > a[mid],x在右半边
		else if (x < a[mid])
		{
			bot = mid + 1;
		}			           //若x < a[mid],x在左半边
		else
			return mid;
	}
	if (top < bot)
			return -1;
	else
		return mid;
}
int main()
{
	int a[10], n,x;
	cout << "元素个数:";
	cin >> n;
	cout << "查找元素:";
	cin >> x;
	input(a, n);
	output(a, n);
	cout<<"最大值"<<Getmax(a, n)<<endl;
	cout << "排序:"<<endl;
	selectsort(a, n);
	bubblesort(a, n);
	cout <<"查找元素的位置:"<< banarysearch(a, n, x);
	cout << endl;
	system("pause");
	return 0;
}

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值