这里总结了一些比较常见的排序。
1.简单选择排序
题目链接:http://acm.njupt.edu.cn/acmhome/problemdetail.do?&method=showdetail&id=1061
代码:
#include <iostream>
#include <cstdio>
using namespace std;
#define MAX 410
int a[MAX];
void printSource(int n);
void printSelectSort(int n);
void printResult(int n);
int main()
{
int n, i;
scanf("%d", &n);
for(i = 0; i < n; i++) scanf("%d", &a[i]);
printSource(n);
printSelectSort(n);
printResult(n);
return 0;
}
void printSource(int n)
{
printf("Source:\n");
for(int i = 0; i < n; i++)
{
if(i) printf(" ");
else printf("(");
printf("%d", a[i]);
}
printf(")\n");
}
void printSelectSort(int n)
{
printf("Select Sort:\n");
for(int i = 0; i < n - 1; i++)
{
int index = i, j;
for(j = i + 1; j < n; j++)
{
if(a[index] > a[j])
index = j;
}
swap(a[index], a[i]);
for(j = 0; j <= i; j++)
{
if(j) printf(" ");
else printf("(");
printf("%d", a[j]);
}
printf(")");
for(; j < n; j++)
{
printf(" %d", a[j]);
}
printf("\n");
}
}
void printResult(int n)
{
printf("Result:\n");
for(int i = 0; i < n; i++)
{
if(i) printf(",");
else printf("(");
printf("%d", a[i]);
}
printf(")\n");
}
2.直接插入排序
链接:http://acm.njupt.edu.cn/acmhome/problemdetail.do?&method=showdetail&id=1062
代码:
#include <iostream>
using namespace std;
#define MAX 410
int a[MAX];
void printSource(int n);
void printInsertSort(int n);
void printResult(int n);
int main()
{
int n, i;
scanf("%d", &n);
for(i = 0; i < n; i++) scanf("%d", &a[i]);
printSource(n);
printInsertSort(n);
printResult(n);
return 0;
}
void printSource(int n)
{
printf("Source:\n");
for(int i = 0; i < n; i++)
{
if(i) printf(" %d", a[i]);
else printf("(%d)", a[i]);
}
printf("\n");
}
void printInsertSort(int n)
{
printf("Insert Sort:\n");
for(int i = 0; i < n - 1; i++)
{
int tmp = a[i+1], j = i;
while((j >= 0) && (tmp < a[j]))
{
a[j+1] = a[j]; j--;
}
a[j+1] = tmp;
for(j = 0; j <= i + 1; j++)
{
if(j) printf(" ");
else printf("(");
printf("%d", a[j]);
}
printf(")");
for(; j < n; j++) printf(" %d", a[j]);
printf("\n");
}
}
void printResult(int n)
{
printf("Result:\n");
for(int i = 0; i < n; i++)
{
if(i) printf(" ");
else printf("(");
printf("%d", a[i]);
}
printf(")\n");
}
3.冒泡排序
链接:http://acm.njupt.edu.cn/acmhome/problemdetail.do?&method=showdetail&id=1063
代码:
#include <iostream>
#include <cstdio>
using namespace std;
#define MAX 410
int a[MAX];
void printSource(int n);
void printBubbleSort(int n);
void printResult(int n);
int main()
{
int n;
scanf("%d", &n);
for(int i = 0; i < n; i++) scanf("%d", &a[i]);
printSource(n);
printBubbleSort(n);
printResult(n);
return 0;
}
void printSource(int n)
{
printf("Source:\n");
for(int i = 0; i < n; i++)
{
if(i) printf(" ");
else printf("(");
printf("%d", a[i]);
}
printf(")\n");
}
void printBubbleSort(int n)
{
int index, j, t = n;
printf("Bubble Sort:\n");
while(true)
{
index = 1;
for(j = 0; j < t - 1; j++)
{
if(a[j] > a[j+1]) swap(a[j], a[j+1]), index = j + 1;
}
t = index;
for(j = 0; j < index; j++)
{
if(j) printf(" ");
else printf("(");
printf("%d", a[j]);
}
printf(")");
for(; j < n; j++) printf(" %d", a[j]);
printf("\n");
if(1 == index) break;
}
}
void printResult(int n)
{
printf("Result\n");
for(int i = 0; i < n; i++)
{
if(i) printf(" ");
else printf("(");
printf("%d", a[i]);
}
printf(")\n");
}
4.快速排序
链接:http://acm.njupt.edu.cn/acmhome/problemdetail.do?&method=showdetail&id=1064
代码:
#include <iostream>
#include <cstdio>
using namespace std;
#define MAXN 100010
#define MAX 100000
int a[MAXN];
void qsort(int left, int right)
{
if(left < right)
{
int i = left + 1, j = right;
while(i < j)
{
while(a[i] <= a[left]) i++;
while(a[j] > a[left]) j--;
if(i < j) swap(a[i], a[j]);
}
if(a[left] > a[j]) swap(a[left], a[j]);
qsort(left, j-1);
qsort(j+1, right);
}
}
int main()
{
int n, i;
scanf("%d", &n);
for(i = 0; i < n; i++) scanf("%d", &a[i]);
a[i] = MAX;
qsort(0, n-1);
for(i = 0; i < n; i++) { if(i) printf(" "); printf("%d", a[i]); }
printf("\n");
return 0;
}
5.两路合并排序
链接:http://acm.njupt.edu.cn/acmhome/problemdetail.do?&method=showdetail&id=1065
代码:
#include <iostream>
#include <cstdio>
using namespace std;
#define MAXN 100010
int a[MAXN];
void merge(int i1, int j1, int i2, int j2);
void mergeSort(int n);
int main()
{
int n, i;
scanf("%d", &n);
for(i = 0; i < n; i++) scanf("%d", &a[i]);
mergeSort(n);
for(i = 0; i < n; i++) {if(i) printf(" "); printf("%d", a[i]);}
printf("\n");
return 0;
}
void merge(int i1, int j1, int i2, int j2)
{
int *tmp = new int[j2 - i1 + 1], k = 0, m = i1, i;
while((i1 <= j1) && (i2 <= j2)) tmp[k++] = a[i1] < a[i2] ? a[i1++] : a[i2++];
while(i1 <= j1) tmp[k++] = a[i1++];
while(i2 <= j2) tmp[k++] = a[i2++];
for(i = 0; i < k; i++) a[m+i] = tmp[i];
delete []tmp;
}
void mergeSort(int n)
{
int i1, j1, i2, j2, size = 1;
while(size < n)
{
i1 = 0;
while(i1 + size < n)
{
if(i1 + size < n) i2 = i1 + size, j1 = i2 - 1;
if(i2 + size < n) j2 = i2 + size - 1; else j2 = n - 1;
merge(i1, j1, i2, j2);
i1 = j2 + 1;
}
size *= 2;
}
}
6.堆排序
链接:http://acm.njupt.edu.cn/acmhome/problemdetail.do?&method=showdetail&id=1066
代码:
#include <iostream>
using namespace std;
#define MAXN 100010
int a[MAXN];
void heapSort(int n);
void adjustDown(int n, int m);
int main()
{
int n, i;
scanf("%d", &n);
for(i = 0; i < n; i++) scanf("%d", &a[i]);
heapSort(n);
for(i = 0; i < n; i++) { if(i) printf(" "); printf("%d", a[i]); }
printf("\n");
return 0;
}
void adjustDown(int n, int m)
{
int child = 2 * n + 1, tmp = a[n];
while(child <= m)
{
if((child < m) && (a[child] < a[child+1])) child++;
if(a[(child-1)/2] >= a[child]) break;
else swap(a[(child-1)/2], a[child]);
child = 2 * child + 1;
}
}
void heapSort(int n)
{
int i;
for(i = (n - 2) / 2; i >= 0; i--) adjustDown(i, n-1);
for(i = n - 1; i > 0; i--)
{
swap(a[0], a[i]);
adjustDown(0, i - 1);
}
}