oj查找和排序

小白鼠

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct Rat
{
int weight;
char color[10];
};

void swap(struct Rat *x,struct Rat *y)
{
int w=x->weight;
x->weight=y->weight;
y->weight=w;

char ch[10];
strcpy(ch,x->color);
strcpy(x->color,y->color);
strcpy(y->color,ch);
}

void bubbleSort(struct Rat *p , int n)
{
int i,j;
for(i=n-1;i>-1;i--)
for(j=0;j<i;j++)
if(p[j].weight>p[j+1].weight)
swap(&p[j],&p[j+1]);
}
int main()
{
int n,i;
scanf("%d",&n);
struct Rat *rat=(struct Rat*)malloc(sizeof(struct Rat)*n);
for(i=0;i<n;i++)
scanf("%d %s",& rat[i].weight, rat[i].color);

bubbleSort(rat,n);

for(i=0;i<n;i++)
printf("%s\n",rat[i].color);
return 0;
}

递归折半

#include<iostream>
using namespace std;
int BinSearch(int low, int high, int k,int a[])
{
	if (low > high)
	{
		cout << "no answer";
		return 0;
	}
	else
	{
		int mid = (low + high) / 2;
		if (k < a[mid])return BinSearch(low, mid - 1, k,a);
		if (k > a[mid])return BinSearch(mid + 1, high, k, a);
		else return mid;
	}
}
int main()
{
	int n, first, last, k;
	cin >> n;
	int *a= new int[n];
	for (int i = 0; i < n; ++i)
	{
		cin >> a[i];
	}
	cin >> k;
	first = 0; last = n;
	if (BinSearch(first, last, k, a) != 0)
		cout << BinSearch(first, last, k, a)+1;
	delete[]a;
	return 0;
}

快速排序

#include<iostream>
using namespace std;
int Paratation(int first, int last, int a[])
{
	int i = first, j = last;
	while (i < j)
	{
		while (i < j && a[i] <= a[j])j--;
		if (i < j)
		{
			swap(a[i], a[j]);
			++i;
		}
		while (i < j && a[i] <= a[j])++i;
		if (i < j)
		{
			swap(a[i], a[j]);
			--j;
		}
	}
	return i;
}
void QuickSort(int first,int last,int a[])
{
	if (first >= last)return;
	else
	{
		int pivot = Paratation(first, last, a);
		QuickSort(first, last - 1,a);
		QuickSort(first + 1, last, a);
	}
}
int main()
{
	int number;
	cin >> number;
	int* a = new int[number];
	for (int i = 0; i < number; ++i)
	{
		cin >> a[i];
	}
	int first; int last;
	first = 0; last = number;
	QuickSort(first, last, a);
	for (int i = 0; i < number; ++i)
	{
		cout<< a[i]<<" ";
	}
}

数的查找

#include<iostream>


using namespace std;
int a[100001], k, b[100001], m, i;//a为待处理数组  b为原数组 

void Operation(int start, int end) {
	int i = start, j = end;
	while (i != j)
	{
		if (i < j)
		{
			if (a[i] > a[j])
			{
				swap(a[i], a[j]);
				swap(i, j);
			}
			else j--;
		}
		else if (a[i] < a[j])
		{
			swap(a[i], a[j]);
			swap(i, j);
		}
		else j++;

	}
	if (i < k)
	{
		Operation(i + 1, end);
	}
	else if (i == k) {
		int count;
		for (count = 1; count <= m; count++)
			if (b[count] == a[i])
			{
				cout << count;
				break;
			}
	}

	else Operation(start, i - 1);
}

int main()
{
	cin >> m >> k;
	for (i = 1; i <= m; i++)
	{
		cin >> a[i];
		b[i] = a[i];
	}
	Operation(1, m);
	return 0;
}

选择排序

#include<iostream>
using namespace std;
int main()
{
	int a[3001], i, m = 0, tmp1;
	int s;
	cin >> s;
	int x = s - 1;
	for (i = 0; i < s; i++)
	{
		cin >> a[i];
	}
	for (i = 0; i < s; i++)
	{
		while (m < s - i)
		{
			if (a[m] < a[m + 1])
			{
				tmp1 = a[m];
				a[m] = a[m + 1];
				a[m + 1] = tmp1;
			}
			m++;
		}
		m = 0;
	}
	do
	{
		cout << a[x] << ' ';
		x--;
	} while (x >= 0);
	return 0;
}

插入排序

#include<iostream>
#include<stack>
using namespace std;
void InserSort(int data[],int length)
{
	int i, j, temp;
	for (i = 1; i < length; ++i)
	{
		temp = data[i];
		for (j = i - 1; j >= 0 && data[j] > temp; --j)
			data[j + 1] = data[j];
		data[j + 1] = temp;
	}
}
int main()
{
	int n;
	cin >> n;
	int* a = new int[n];
	for (int i = 0; i < n; ++i)
		cin >> a[i];
	InserSort(a, n);
	for (int i = 0; i < n; ++i)
		cout << a[i] << " ";
	delete[]a;
	return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值