您的位置:首页 > 其它

排序专题

2014-04-02 20:59 197 查看
一些知识点:

常见的 n^2 的排序算法有:冒泡排序,选择排序,交换排序

常见的 nlogn 的排序算法有:归并排序(稳定排序),快速排序,堆排序,利用AVL 排序

代码实现:

冒泡排序(稳定排序):

/****************************************
* File Name: select_sort.cpp
* Author: sky0917
* Created Time: 2014年04月 2日 20:31:51
****************************************/
#include <cstdio>
#include <algorithm>
using namespace std;
const int maxn = 1002;

int a[maxn];
int main(){
int n;
while (scanf("%d",&n)!=EOF){
for (int i=0; i<n; i++){
scanf("%d",&a[i]);
}

for (int i=0; i<n; i++){
int g = i;
for (int j=i+1; j<n; j++){
if (a[j] < a[g]){
g = j;
}
}
swap(a[i], a[g]);
}
for (int i=0; i<n; i++){
printf("%d\n",a[i]);
}
}
return 0;
}


选择排序
举个例子,序列5 8 5 2 9, 我们知道第一遍选择第1个元素5会和2交换,那么原序列中2个5的相对前后顺序就被破坏了,所以选择排序不是一个稳定的排序算法


归并排序(稳定排序):

/****************************************
* File Name: merge_sort.cpp
* Author: sky0917
* Created Time: 2014年04月24日 21:50:42
****************************************/
#include <cstdio>
#include <algorithm>
using namespace std;
const int maxn = 100005;

int a[maxn];
int b[maxn];
void merge_sort(int a[], int l, int r){
if (l >= r)return;
if (l + 1 == r){
if (a[l] > a[r])
swap(a[l], a[r]);
return;
}
int m = (l + r) / 2;
merge_sort(a, l, m);
merge_sort(a, m+1, r);
int tl=l, tr = m+1;
int tp = 0;
while (tl <= m && tr <= r){
if (a[tl] <= a)
b[tp++] = a[tl++];
else b[tp++] = a[tr++];
}
while (tl <= m)
b[tp++] = a[tl++];
while (tr <= r)
b[tp++] = a[tr++];
for (int i=0; i<tp; i++){
a[l+i] = b[i];
}
}
int main(){
int n;
while (scanf("%d",&n)!=EOF){
for (int i=0; i<n; i++){
scanf("%d",&a[i]);
}
merge_sort(a,  0, n-1);
for (int i=0; i<n; i++)
printf("%d%c",a[i],i==n-1?'\n':' ');
}
return 0;
}


快速排序(不稳定排序):

/****************************************
* File Name: quick_sort.cpp
* Author: sky0917
* Created Time: 2014年04月24日 20:17:44
****************************************/
#include <cstdio>
#include <algorithm>
using namespace std;
const int maxn = 100005;

int a[maxn];

int getmid(int a[], int l, int r){
int x = a[r];
int mid = l;
for (int i=l; i<r; i++){
if (a[i] < x){
swap(a[mid], a[i]);
mid++;
}
}
swap(a[mid], a[r]);
return mid;
}

void quick_sort(int a[], int l, int r){
if (l >= r) return;
int m = getmid(a, l, r);
quick_sort(a, l, m-1);
quick_sort(a, m+1, r);
}
int main(){
int n;
while (scanf("%d",&n)!=EOF){
for (int i=1; i<=n; i++){
scanf("%d",&a[i]);
}
quick_sort(a, 1, n);
for (int i=1; i<=n; i++){
printf("%d%c",a[i],i==n?'\n':' ');
}
}
return 0;
}


堆排序(不稳定排序):

/****************************************
* File Name: heap_sort.cpp
* Author: sky0917
* Created Time: 2014年04月24日 20:04:35
****************************************/
#include <cstdio>
#include <algorithm>
using namespace std;
const int maxn = 100005;

int a[maxn];
void down(int i, int n){
for (int son; i <= n/2; i = son){
son = i<<1;
if ((son != n) && (a[son+1] > a[son]))
son = son + 1;
if (a[son] > a[i])
swap(a[son], a[i]);
}
}
void heap_sort(int a[], int n){
for (int i=n/2; i>=1; i--){
down(i, n);
}
for (int i=n; i>=1; i--){
swap(a[1], a[i]);
down(1, i-1);
}
}

int main(){
int n;
while (scanf("%d",&n)!=EOF){
for (int i=1; i<=n; i++){
scanf("%d",&a[i]);
}
heap_sort(a, n);
for (int i=1; i<=n; i++){
printf("%d%c",a[i],i==n?'\n':' ');
}
}
return 0;
}


基数排序(稳定排序): 其时间复杂度为O (nlog(r)m),其中r为所采取的基数,而m为堆数,在某些时候,基数排序法的效率高于其它的比较性排序法。

/****************************************
* File Name: bucket_sort.cpp
* Author: sky0917
* Created Time: 2014年04月24日 22:09:35
****************************************/
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 100005;

int a[maxn];
int s[10][maxn];
int tp[10];

void bucket_sort(int a[], int n){
memset(tp, 0, sizeof(tp));
int k = 0, w = 1;
int max_val = 1000000;
while (w < max_val){
for (int i=0; i<n; i++){
int ls = (a[i] / w) % 10;
s[ls][tp[ls]++] = a[i];
}
for (int i=0; i<10; i++){
if (tp[i]){
for (int j=0; j<tp[i]; j++){
a[k++] = s[i][j];
}
tp[i] = 0;
}
}
k = 0, w *= 10;
}
}
int main(){
int n;
while (scanf("%d",&n)!=EOF){
for (int i=0; i<n; i++)
scanf("%d",&a[i]);
bucket_sort(a, n);
for (int i=0; i<n; i++){
printf("%d%c",a[i],i==n-1?'\n':' ');
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: