常用排序算法复习

news/2024/5/19 21:47:49 标签: 算法, 排序算法, 快速排序, 插入排序

排序

O(n2):选择排序、插入排序、冒泡排序

  • 选择排序:第一次从待排序的数据元素中选出最小(或最大)的一个元素,存放在序列的起始位置,然后再从剩余的未排序元素中寻找到最小(大)元素,然后放到已排序的序列的末尾。以此类推,直到全部待排序的数据元素的个数为零。
  • 直接插入排序:将一条记录插入到已排好的有序表中,从而得到一个新的、记录数量增1的有序表。
  • 冒泡排序:两个数比较大小,较大的数下沉,较小的数冒起来。
//选择排序
void selection_sort(int arr[],int len){
	for(int i=0;i<len;i++){
		int min=i;
		for(int j=i+1;j<len;j++){
			if(arr[j]<arr[min]){
				min=j;
			}
		}
		swap(arr[min],arr[i]);
	}
} 
//冒泡排序
void bubble_sort(int arr[],int len){
	for(int i=0;i<len-1;i++)
	for(int j=0;j<len-1-i;j++)
	if(arr[j]>arr[j+1])
	swap(arr[j],arr[j+1]);
}
//插入排序
void insertion_sort(int arr[],int len){
	for(int i=1;i<len;i++){
		int key=arr[i];
		int j=i-1;
		while(j>=0&&key<arr[j]){
			arr[j+1]=arr[j];
			j--;
		}
		arr[j+1]=key;
	}
}

O(nlog n):归并排序、快速排序

  • 归并排序:把待排序的序列分为若干个子序列,每个子序列是有序的。然后再把有序子序列合并为整体有序的序列。
  • 快速排序:从数列中挑出一个元素,称为 “基准”(pivot);然后重新排序数列,所有比基准值小的元素放置在基准前面,所有比基准值大的元素放置在基准的后面,和基准相同的元素则可以放置在任何一边。在这个分区结束之后,该基准就处于序列中的某一位置。这一操作称为分区(partition)操作。然后,递归地把小于基准值元素的子序列和大于基准值元素的子序列进行排序
//归并排序
void Merge(int r[],int temp[],int s,int m,int t){
	int i=s;
	int j=m+1;
	int k=i;
	while(i<=m&&j<=t){
		if(r[i]<=r[j])
		temp[k++]=r[i++];
		else
		temp[k++]=r[j++];
	}
	while(i<=m)
	temp[k++]=r[i++];
	while(j<=t)
	temp[k++]=r[j++];
	for(i=s;i<=t;i++)
	r[i]=temp[i];
}
void MergeSort(int r[],int temp[],int s,int t){
	if(s==t)
	return;
	else{
		int m=(s+t)/2;
		MergeSort(r,temp,s,m);
		MergeSort(r,temp,m+1,t);
		Merge(r,temp,s,m,t);
	}
}

//快速排序
int Paritition(int A[],int low,int high){
	int pivot=A[low];
	while(low<high){
	while(low<high&&A[high]>=pivot)
		--high;
	A[low]=A[high];
	while(low<high&&A[low]<=pivot)
		++low;
	A[high]=A[low];
}
A[low]=pivot;
return low;
}
void QUickSort(int A[],int low,int high){
	if(low<high){
		int pivot=Paritition(A,low,high);
		QuickSort(A,low,pivot-1);
		QucikSort(A,pivot+1,high);
	}
}

http://www.niftyadmin.cn/n/1164975.html

相关文章

POJ 1318 Word Amalgamation

题目描述 对于每个输入的字典先进行排序&#xff0c;对于输入的单词按字符序排序&#xff0c;字典中的单词也按照字符序排序&#xff0c;两者比较 字符串比较函数strcmp按字典序比较两个字符串&#xff0c;并返回结果&#xff1a;如果两个字符串相等&#xff0c;则返回零&#…

HDOJ 1236 排名

题目描述 此题较为简单&#xff0c;进行结构体排序即可 #include<iostream> #include<algorithm> #include<cstring> using namespace std;struct student{string name;int score; }; bool com(const student& a,const student& b){if(a.scoreb.sco…

POJ 3664 Election Time

题目描述 结构体排序即可&#xff0c;排序两次 数据量较大需要使用scanf&#xff0c;使用cin会tle #include<cstdio> #include<algorithm> using namespace std;struct cow{int first;int second;int index; }; bool com1(const cow& a,const cow& b){re…

POJ 2726 Holiday Hotel

题目描述 选取宾馆M 离海滩比M近的宾馆要比M贵。 比M便宜的宾馆离海滩比M远。 根据距离由小到大对宾馆排序&#xff0c;假如当前宾馆的费用比之前的都小&#xff08;小于之前的宾馆的价格的最小值&#xff09;即符合题意 #include<iostream> #include<algorithm&g…

UVA 156 反片语 Ananagrams

题目描述 把每个单词先转换为小写&#xff0c;进行排序&#xff0c;记录出现的次数&#xff0c;输出出现次数只有一次的单词 map记录单词出现的次数 #include<string> #include<iostream> #include<sstream> #include<vector> #include<algorithm&…

POJ 1208 The Blocks Problem

题目描述 模拟题&#xff0c;利用vector来做&#xff0c;模拟每种操作&#xff0c;题意较为简单&#xff0c;但是操作起来还是有些复杂的&#xff0c;需要多多练习 #include<iostream> #include<vector> #include<cstdio> using namespace std;const int m…

UVA 10887 Concatenation of Languages

题目描述 本题目较为简单&#xff0c;利用set存储两种语言的拼接即可 注意输入可能有空格 #include<set> #include<cstring> #include<iostream> using namespace std; string str1[1505]; string str2[1505]; int main(){int cas1;set<string> s;int…

UVA 141 The Spot Game

题目描述 运用一个结构体&#xff0c;把每种棋盘状态存到一个set中&#xff0c;对每次输入后的棋盘状态进行比较&#xff0c;判断是否出现过 或者对输入的棋盘进行旋转也可以由于set的具体实现采用了红黑树的平衡二叉树的数据结构&#xff0c;所以&#xff0c;set中的元素就有大…