首页 > 其他分享 >数据结构之排序

数据结构之排序

时间:2023-11-06 13:11:24浏览次数:34  
标签:13 27 49 int void 排序 数据结构

一.什么是稳定排序?

排序后相等元素的相对位置不发生变化

二.稳定排序有哪些?

2.1.不稳定排序:快速排序、希尔排序、堆排序

2.2.稳定排序:冒泡排序、插入排序、归并排序、基数排序

三.各大排序算法

3.1.稳定算法

3.1.1.冒泡排序
思想:通过两两比较不断将最大的数浮出水面。一次浮出一个数,需要n-1次。
#include<iostream>
#include<algorithm>

using namespace std;

const int N = 100;

void input(int a[],int &n){
	cin >> 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;
}

void bubbleSort(int a[], int n) {
	for (int i = 0; i < n - 1; i++)
		for (int j = 0; j < n - i - 1; j++)
			if (a[j] > a[j + 1])
				swap(a[j], a[j + 1]);
}

int main() {
	int a[N];
	int n;
	input(a, n);
	bubbleSort(a, n);
	output(a, n);
	return 0;
}
题目:1、已知待排序记录的关键字序列为 {49, 38, 65, 97, 76, 13, 27, 49},请给出用冒泡排序法进行排序的过程。
第一趟:38 49 65 76 13 27 49 97; 
第二趟:38 49 65 13 27 49 76 97;
第三趟:38 49 13 27 49 65 76 97;
第四趟:38 13 27 49 49 65 76 97;
第五趟:13 27 38 49 49 65 76 97;
第六趟:13 27 38 49 49 65 76 97;
第七趟:13 27 38 49 49 65 76 97
程序运行

3.1.2.插入排序
思想:“扑克牌排序”;由于一个数天然有序,所以从第二个数开始,不断将后续的数插入正确的位置。
#include<iostream>
#include<algorithm>

using namespace std;
const int N = 100;
void input(int a[], int& n) {
	cin >> 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;
}

void insertSort(int a[],int n) {
	for (int i = 1; i < n; i++) {
		int value = a[i];
		int pos = i - 1;
		while (pos >= 0 && a[pos] > value) {
			a[pos + 1] = a[pos];
			pos--;
		}
		a[pos + 1] = value;
	}
}

int main() {
	int n;
	int a[N];
	input(a, n);
	insertSort(a, n);
	output(a, n);
	return 0;
}
3.1.3.归并排序
思想:“分治法的典型之一”;先分再合;保证子块的有序性。
具体的我们以一组无序数列{14,12,15,13,11,16}为例分解说明,如下图所示:

#include<iostream>

using namespace std;

const int N = 100;

void input(int a[], int& n) {
	cin >> 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;
}

void merge(int a[], int left, int mid, int right) {
	int l_pos = left, r_pos = mid + 1;
	int pos = left;
	int temp[N];
	while (l_pos <= mid && r_pos <= right) {
		if (a[l_pos] < a[r_pos]) temp[pos++] = a[l_pos++];
		else temp[pos++] = a[r_pos++];
	}
	while (l_pos <= mid) 
		temp[pos++] = a[l_pos++];
	while (r_pos <= right) 
		temp[pos++] = a[r_pos++];
	while (left <= right) {
		a[left] = temp[left];
		left++;
	}
}

void mergeSort(int a[],int left,int right) {
	if (left < right) {
		int mid = (left + right) / 2;
		mergeSort(a, left, mid);
		mergeSort(a, mid + 1, right);
		merge(a, left, mid, right);
	}
}

int main() {
	int a[N];
	int n;
	input(a, n);
	mergeSort(a, 0, n - 1);
	output(a, n);
	return 0;
}

3.2.不稳定算法

3.2.1.堆排序
#include<iostream>
#include<algorithm>

using namespace std;

const int N = 100;

void input(int a[],int& n) {
	cin >> 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;
}

void heapify(int a[],int n,int pos) {
	int l_child = pos * 2 + 1;
	int r_child = pos * 2 + 2;
	int max = pos;
	if (a[max] < a[l_child] && l_child < n) max = l_child;
	if (a[max] < a[r_child] && r_child < n) max = r_child;
	if (max != pos) {
		swap(a[max], a[pos]);
		heapify(a, n, max);
	}
}

void heapSort(int a[], int n) {
	//建堆
	for (int i = n / 2 - 1; i >= 0; i--)
		heapify(a, n, i);
	//排序
	for (int i = n - 1; i > 0; i--) {
		swap(a[0], a[i]);
		heapify(a, i, 0);//维护大顶堆的性质
	}
}

int main() {
	int n;
	int a[N];
	input(a, n);
	heapSort(a, n);
	output(a, n);
	return 0;
}

标签:13,27,49,int,void,排序,数据结构
From: https://www.cnblogs.com/cony1/p/17233043.html

相关文章

  • cf1322BPresent(基数排序+双指针+拆位)
    cf1322BPresent首先拆位是显然的,对于两个数a[i],a[j],除了考虑当前位上的数,我们还要考虑是否会产生进位,我们可以利用基数排序+双指针,因为我们每次都是将低位的排好序了,所以我们可以用双指针计算进位,然后分类计算一下,当前为为1的情况即可。#include<cstdio>#include<algorithm>#......
  • java中的重排序和volatile关键字
    一、内存模型基础1、内存模型描述的是程序中各变量(线程共享变量)的访问规则,以及在实际计算机系统中将变量存储到内存和从内存读取出变量这样的低层细节。2、Jvm系统中存在一个主内存(MainMemory或JavaHeapMemory),Java中所有变量都储存在主存中,对于所有线程都是共享的。3、每......
  • 数据结构的基本概念和术语
    数据(Data)数据:能输入计算机且能被计算机处理的各种符号的集合,信息的载体能被计算机识别,存储和加工包括:数值型的数据:整数,实数等  非数值型的数据:文字,图像,声音等;2.数据元素和数据项数据元素:是数据的基本单位,在计算机程序......
  • C++使用冒泡排序算法对数组进行排序
     #include<iostream>//包含iostream库usingnamespacestd;//使用标准命名空间intmain(){//主函数intarr[]={5,3,2,8,6,7,1,4};//定义并初始化数组intn=sizeof(arr)/sizeof(arr[0]);//计算数组长度//使用冒泡排序算法对数组进......
  • 排序
    排序目录排序排序算法C语言程序排序算法排序算法是计算机科学中经常使用的一类算法,用于将一组数据按照特定条件进行排序,以便更方便地进行搜索、插入等操作。常见的排序算法包括冒泡排序、插入排序、选择排序、快速排序、归并排序等。冒泡排序(BubbleSort):该算法通过不断地......
  • 插入排序
    目录目录目录算法代码流程图算法将待排序序列第一个元素看做一个有序序列,把第二个元素到最后一个元素当成是未排序序列。从头到尾依次扫描未排序序列,将扫描到的每个未排序元素插入有序序列的适当位置。就像给一副扑克牌排序,先取第一张作为排序的开始,再从剩下的牌中取第二张,并......
  • 力扣905 按奇偶排序数组 C++ 双指针+一次遍历
    905.按奇偶排序数组classSolution{public:vector<int>sortArrayByParity(vector<int>&nums){inti=0,j=nums.size()-1;while(i<nums.size()-1&&i<j){while(i<j&&(nums[i]%2==0))i++;......
  • 如何按字典中的值对Python中的字典列表进行排序?
    内容来自DOChttps://q.houxu6.top/?s=如何按字典中的值对Python中的字典列表进行排序?如何按特定键的值对字典列表进行排序?给定:[{'name':'Homer','age':39},{'name':'Bart','age':10}]当按name排序时,它应该变成:[{'name':'Bart�......
  • 排序算法
    目录1.选择排序2.冒泡排序3.插入排序4.快速排序给定数组:[12,23,8,15,33,24,77,55]1.选择排序选择排序的思路是从未排序的部分中选择最小的元素,然后将其与未排序部分的第一个元素交换。选择最小值为8,与第一个元素12交换,得到:[8,23,12,15,33,24,77,55]2.冒......
  • 【MySQL】MVCC机制、ReadView数据结构、匹配规则详解
    (目录)MySQLMVCC机制1.隔离级别在MySQLInnoDB存储引擎下,RC、RR基于MVCC(多版本并发控制)进行并发事务控制MVCC是**基于”数据版本”**对并发事务进行访问2.场景分析UNDO_LOG不是会被删除吗?中间数据万一被删了版本链不就断了?UNDO_LOG版本链不是立即删除,MySQL确保版......