山东大学数据结构实验二排序算法

1、 最多接受20个不为零的正整数进行排序,如果中间输入0则代表提前结束输入,0之前输入几个数就用几个数参与排序,0不参与排序。
2、 数字选择排序方法,1-Bubble Sort,2-Insert Sort,3-Radix Sort(注意大小写要区分)。
3、 基数排序能够仅仅实现小于10的正整数的排序。如果输入的数据有大于9数据,基数排序不再排序,直接输出一个0后结束程序。
4、 使用所选排序方法的排序,结果输出所用方法以及结果,每个数之间用“,”隔开,中间不要有空格。
5、 输入输出请严格按下面要求的格式实现,不能少任何一行文字。

#include <iostream>
using namespace std;

//向有序数组中插入元素
void insert(int a[], int n, const int& x)
{
	//向有序数组a[0:n-1]中插入元素x;
	//假设数组a的容量大于n,很重要!!
	int i;
	for (i = n - 1; i >= 0 && x < a[i]; i--)//1 2 3 4 5 _
	{
		a[i + 1] = a[i];//把前面的元素往后移一格
	}
	a[i + 1] = x;
}
//插入排序
void insertionSort(int a[], int n)
{
	//对a[0:n-1]实施插入排序
	//当i=1时,是往a[0]中插入a[1].
	//这个就是往前面已经拍好序的数列重新插入新的数,像滚雪球一样越来越大
	for (int i = 1; i < n; i++)
	{
		int t = a[i];
	insert(a, i, t);
	}
}

//及时终止的冒泡排序
bool bubble(int a[], int n)
{
	//把数组a[0:n-1]中最大的元素通过冒泡移动到右边
	bool swapped = false;
	
		for (int j = 1; j < n; j++)
		{
			if (a[j - 1] > a[j])swap(a[j], a[j - 1]);
			swapped = true;
		}
	
   return swapped;
}
void bubbleSort(int a[], int n)
{
	for (int i = n; i > 1 && bubble(a, i); i--);
}



int Max(int A[], int n)
{
	//初始化最大元素为A[0]
	int maxvalue = A[0];
	for (int j = 1; j < n; j++)
	{
		if (A[j] > maxvalue)
		{
			maxvalue = A[j];
		}
	}
	return maxvalue;
}


//基数排序  A[]  是待排序数组  n是数组的长度
void RadixSort(int A[], int n)
{

	int** B = new int* [10];//二维动态数组


	//首先先初始化一下桶
	for (int i = 0; i < 10; i++)
	{
		B[i] = new int[2*n];//0不使用 为了保险 桶大一点
	}
	
	    for (int i = 0; i < 10; i++)
		B[i][0] = 0;//使用第0位当做计数器


		//分配
	
		for (int i = 0; i < n; i++)
		{
			
			int num = A[i];
			int index = ++B[num][0];//相应桶的计数器应该加1
			B[num][index] = A[i];
			
		}


		//把10个箱子里的数拎出来
		int j = 0;
		for (int i = 0; i < 10; i++)
		{
			for (int k = 1; k <= B[i][0]; k++)
			{
				
					
					A[j++] = B[i][k];
					
			}
				
		}

	
	
	for (int i = 0; i < 10; i++)
	{
		delete[] B[i];
	}
	delete B;
}


int main()
{
	cout << "Input" << endl;
	int* a = new int[23];

	int index = 0;
	int select = 0;
	int input;

	
	while (index != 20)
	{
		cin >> input;
		if (input)
		{
			a[index] = input;
			index++;
		}
		else {
			break;
		}

	}
	/*cout << a[0];
	
	for (int i = 1; i <= index; i++)
	{
		cout << "," << a[i];
	}
	cout << endl;*/

	cout << "1-Bubble Sort,2-Insert Sort,3-Radix Sort" << endl;
	cin >> select;
	switch (select)
	{
	case 1:
	{
		bubbleSort(a, index);
		cout << "Output" << endl;
		cout << "Bubble Sort" << endl;
		cout << a[0];
		for (int i = 1; i < index; i++)
		{
			cout << "," << a[i];
		}
		cout << endl;
		cout << "End" << endl;

	}
	break;

	case 2:
	{
		insertionSort(a, index);
		cout << "Output" << endl;
		cout << "Insert Sort" << endl;
		cout << a[0];
		for (int i = 1; i <index; i++)
		{
			cout << ","<<a[i] ;
		}
		cout << endl;
		cout << "End" << endl;

	}
		break;

	case 3:
	{
		int num = Max(a, index);
		//cout << num << endl;
		cout << "Output" << endl;
		cout << "Radix Sort" << endl;
		if (num > 0 && num < 10)
		{
			RadixSort(a, index);
			
			cout << a[0];
			for (int i = 1; i < index; i++)
			{
				cout << "," << a[i];
			}
			cout << endl;
			cout << "End" << endl;
		}
		else {
			cout << 0 << endl;
			cout << "End" << endl;
		}
	}

	default:
		break;
	}
	
	return 0;
}

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值