数据结构之排序算法

// mySort.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

//选择排序
void SelectionSort(int *a, int len) {//传入数组,以及遍历的len
	int i, h, j, k, temp;
	for (i = 0; i < len - 1; i++) {
		k = i;
		for (j = i + 1; j < len; j++) {
			if (a[j] < a[k]) {
				k = j;
			}
		}
		if (k != i) {
			temp = a[i];
			a[i] = a[k];
			a[k] = temp;
		}

		//输出每步排序的结果
		cout << "第" << i << "步排序结果:\t";
		for (h = 0; h < len; h++) {
			cout << a[h] << "\t";
		}
		cout << endl << endl;
	}
}

//冒泡排序算法
void BubblueSort(int *a, int len) {
	int i, j, k, temp;

	for (i = 0; i < len - 1; i++) {
		for (j = len - 1; j > i; j--) {
			if (a[j - 1] > a[j]) {//判断大小并交换
				temp = a[j - 1];
				a[j - 1] = a[j];
				a[j] = temp;
			}
		}
		cout << "第" << i << "步排序结果:\t";
		for (k = 0; k < len; k++) {
			cout << a[k] << "\t";
		}
		cout << endl << endl;
	}

}

//插入排序
void InsertionSort(int *a, int len) {
	int i, j, t, h;
	for (i = 1; i < len; i++) {
		t = a[i];
		j = i - 1;
		while (j >= 0 && t < a[j]) {
			a[j + 1] = a[j];
			j--;
		}
		a[j + 1] = t;
		//输出每步排序的结果
		cout << "第" << i << "步排序结果:\t";
		for (h = 0; h < len; h++) {
			cout << a[h] << "\t";
		}
		cout << endl << endl;
	}

}

//希尔排序
void ShellSort(int *a, int len) {
	int i, j, h;
	int r, temp;
	int x = 0;

	for (r = len / 2; r >= 1; r /= 2) {
		for (i = r; i < len; i++) {
			temp = a[i];
			j = i - r;
			while (j >= 0 && temp < a[j]) {
				a[j + r] = a[j];
				j -= r;
			}
			a[j + r] = temp;
		}
		x++;
		//输出每步排序的结果
		cout << "第" << x << "步排序结果:\t";
		for (h = 0; h < len; h++) {
			cout << a[h] << "\t";
		}
		cout << endl << endl;
	}

}

//快速排序
void QuickSort(int *a, int left,int right) {
	int f, t;
	int rtemp, ltemp;

	ltemp = left;
	rtemp = right;
	f = a[(left + right) / 2];
	while (ltemp < rtemp) {
		while (a[ltemp] < f) {
			++ltemp;
		}while (a[rtemp] > f) {
			--rtemp;
		}
		if (ltemp <= rtemp) {
			t = a[ltemp];
			a[ltemp] = a[rtemp];
			a[rtemp] = t;
			--rtemp;
			++ltemp;

		}
	}
	if (left == rtemp)
		ltemp++;
	if (ltemp < rtemp)
		QuickSort(a, left, ltemp-1);
	if (ltemp < right)
		QuickSort(a, rtemp+1, right);
}
int main()
{
	const int size = 5;

	int shuzu[size];
	int  i;
	//产生随机数
	srand(time(nullptr));
	for (i = 0; i < size; i++) {
		shuzu[i] = rand() / 1000 + 10;
	}
	cout << "排序前数组为:\t";
	for (i = 0; i < size; i++) {
		cout << shuzu[i] << "\t";
	}
	cout << endl << endl;

	//开始进行排序
	cout << "请选择你要的排序算法:" << endl;
	cout << "1.选择排序:" << endl;
	cout << "2.冒泡排序:" << endl;
	cout << "3.插入排序:" << endl;
	cout << "4.希尔排序:" << endl;
	cout << "5.快速排序:" << endl;

	int num = 0;
	cin >> num;
	switch (num)
	{
	case 1:
		cout << "选择排序:" << endl;
		SelectionSort(shuzu, size);
		break;
	case 2:
		cout << "冒泡排序:" << endl;
		BubblueSort(shuzu, size);
		break;
	case 3:
		cout << "插入排序:" << endl;
		InsertionSort(shuzu, size);
		break;
	case 4:
		cout << "希尔排序:" << endl;
		ShellSort(shuzu, size);
		break;
	case 5:
		cout << "快速排序:" << endl;
		QuickSort(shuzu, 0, size - 1);
		break;
	default:
		break;
	}
	cout << endl;
	cout << "排序后数组为:\t";
	for (i = 0; i < size; i++) {
		cout << shuzu[i] << "\t";
	}
	cout << endl << endl;
	system("pause");

    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

LL大个仔

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值