目录
1.排序概述
1.排序的概念
2.排序方法的分类
3.稳定排序与不稳定排序
4.存储结构
代码示例:
#define maxsize 20
typedef struct {
int key;
}redtype;
typedef struct {
redtype r[maxsize + 1];
int len;
}sqlist;
2.插入排序
1.直接插入排序
代码示例:
void insertsort(sqlist &l) {
int i,j;
for(i = 2; i <= l.len; ++i) {
if(l.r[i].key < l.r[i - 1].key){
l.r[0] = l.r[i];
for(j = i - 1; l.r[0].key < l.r[j].key; --j) {
l.r[j + 1] = l.r[0];
}
}
}
}
性能分析
2.折半插入排序
代码示例 :
void binsertsort(sqlist &l) {
for(int i = 2; i <= l.len; ++i) {
l.r[0] = l.r[i];
int low = 1;
int high = i - 1;
while(low <= high) {
int mid = (low + high) / 2;
if(l.r[0].key < l.r[mid].key)
high = mid - 1;
else low = mid + 1;
}
for(int j = i - 1; j >= high + 1; --j) {
l.r[high + 1] = l.r[0];
}
}
}
算法分析
3.希尔排序
代码示例:
void shellinsert(sqlist &l, int dk) {
int i,j;
for(i = dk + 1; i <= l.len; ++i) {
if(l.r[i].key < l.r[i - dk].key) {
l.r[0] = l.r[i];
for(j = i - dk; j > 0 && (l.r[0].key < l.r[j].key); j = j - dk) {
l.r[j + dk] = l.r[i];
}
l.r[j + dk] = l.r[0];
}
}
}
void shellsort(sqlist &l, int dlta[], int t) {
for(int k = 0; k < t; ++k) {
shellinsert(l, dlta[k]);
}
}
算法分析
3.交换排序
1.冒泡排序
冒泡排序原理
代码示例:
void bubblesort(sqlist &l) {
int m,n,j;
redtype x;
for(m = 1; m <= n - 1; m++) {
for(j = 1; j <= n - m; j++) {
if(l.r[j].key > l.r[j + 1].key) {
x = l.r[j];
l.r[j] = l.r[j + 1];
l.r[j + 1] = x;
}
}
}
}
冒泡排序的改进
代码示例:
void bubblesort2(sqlist &l) {
int m,n,j;
int flag = 1;
redtype x;
for(m = 1; m <= n - 1 && flag == 1; m++) {
flag = 0;
for(j = 1; j <= n - m; j++) {
if(l.r[j].key > l.r[j + 1].key) {
flag = 1;
x = l.r[j];
l.r[j] = l.r[j + 1];
l.r[j + 1] = x;
}
}
}
}
算法分析
2.快速排序
快排的原理
代码示例:
int partition(sqlist &l,int low,int high) {
l.r[0] = l.r[low];
int pivotkey = l.r[low].key;
while(low < high) {
while(low < high && l.r[high].key >= pivotkey)
--high;
l.r[low] = l.r[high];
while(low < high && l.r[low].key <= pivotkey)
++low;
l.r[high] = l.r[low];
}
l.r[low] = l.r[0];
return low;
}
void qsort(sqlist &l,int low,int high) {
if(low < high) {
int pivotloc = partition(l,low,high);
qsort(l,low,pivotloc - 1);
qsort(l,pivotloc + 1,high);
}
}
int main() {
sqlist l;
qsort(l,1,l.len);
return 0;
}
算法分析
4.选择排序
1.简单选择排序
排序原理
代码示例:
void selectsort(sqlist &l) {
int i,j,k;
for(i = 1; i < l.len; ++i) {
k = i;
for(j = i + 1; j < l.len; j++) {
if(l.r[j].key < l.r[k].key)
k = j;
if(k != i) {
redtype temp = l.r[i];
l.r[i] = l.r[k];
l.r[k] = temp;
}
}
}
}
算法分析
2.堆排序
排序原理
筛选过程代码示例:
void heapadjust(int r[],int s,int m) {
int rc = r[s];
for(int j = s * 2; j <= m; j *= 2) {
if(j < m && r[j] < r[j + 1])
++j;
if(rc > r[j])
break;
r[s] = r[j];
s = j;
}
r[s] = rc;
}
堆排序算法代码示例:
void heapsort(int r[]) {
int i;
int n = sizeof(r)/sizeof(r[0]);
for(i = n / 2; i >= 1; i--) {
heapadjust(r,i,n);
}
for(i = n; i > 1; i--) {
swap(r[1],r[i]);
heapadjust(r,1,i - 1);
}
}
算法性能分析
5.归并排序
算法分析
6.基数排序
算法分析
7.各种排序方法的综合比较
8.总的代码
#include<bits/stdc++.h>
#define maxsize 20
typedef struct {
int key;
}redtype;
typedef struct {
redtype r[maxsize + 1];
int len;
}sqlist;
void insertsort(sqlist &l) {
int i,j;
for(i = 2; i <= l.len; ++i) {
if(l.r[i].key < l.r[i - 1].key){
l.r[0] = l.r[i];
for(j = i - 1; l.r[0].key < l.r[j].key; --j) {
l.r[j + 1] = l.r[0];
}
}
}
}
void binsertsort(sqlist &l) {
for(int i = 2; i <= l.len; ++i) {
l.r[0] = l.r[i];
int low = 1;
int high = i - 1;
while(low <= high) {
int mid = (low + high) / 2;
if(l.r[0].key < l.r[mid].key)
high = mid - 1;
else low = mid + 1;
}
for(int j = i - 1; j >= high + 1; --j) {
l.r[high + 1] = l.r[0];
}
}
}
void shellinsert(sqlist &l, int dk) {
int i,j;
for(i = dk + 1; i <= l.len; ++i) {
if(l.r[i].key < l.r[i - dk].key) {
l.r[0] = l.r[i];
for(j = i - dk; j > 0 && (l.r[0].key < l.r[j].key); j = j - dk) {
l.r[j + dk] = l.r[i];
}
l.r[j + dk] = l.r[0];
}
}
}
void shellsort(sqlist &l, int dlta[], int t) {
for(int k = 0; k < t; ++k) {
shellinsert(l, dlta[k]);
}
}
void bubblesort(sqlist &l) {
int m,n,j;
redtype x;
for(m = 1; m <= n - 1; m++) {
for(j = 1; j <= n - m; j++) {
if(l.r[j].key > l.r[j + 1].key) {
x = l.r[j];
l.r[j] = l.r[j + 1];
l.r[j + 1] = x;
}
}
}
}
void bubblesort2(sqlist &l) {
int m,n,j;
int flag = 1;
redtype x;
for(m = 1; m <= n - 1 && flag == 1; m++) {
flag = 0;
for(j = 1; j <= n - m; j++) {
if(l.r[j].key > l.r[j + 1].key) {
flag = 1;
x = l.r[j];
l.r[j] = l.r[j + 1];
l.r[j + 1] = x;
}
}
}
}
int partition(sqlist &l,int low,int high) {
l.r[0] = l.r[low];
int pivotkey = l.r[low].key;
while(low < high) {
while(low < high && l.r[high].key >= pivotkey)
--high;
l.r[low] = l.r[high];
while(low < high && l.r[low].key <= pivotkey)
++low;
l.r[high] = l.r[low];
}
l.r[low] = l.r[0];
return low;
}
void qsort(sqlist &l,int low,int high) {
if(low < high) {
int pivotloc = partition(l,low,high);
qsort(l,low,pivotloc - 1);
qsort(l,pivotloc + 1,high);
}
}
int main() {
sqlist l;
qsort(l,1,l.len);
return 0;
}
void selectsort(sqlist &l) {
int i,j,k;
for(i = 1; i < l.len; ++i) {
k = i;
for(j = i + 1; j < l.len; j++) {
if(l.r[j].key < l.r[k].key)
k = j;
if(k != i) {
redtype temp = l.r[i];
l.r[i] = l.r[k];
l.r[k] = temp;
}
}
}
}
void heapadjust(int r[],int s,int m) {
int rc = r[s];
for(int j = s * 2; j <= m; j *= 2) {
if(j < m && r[j] < r[j + 1])
++j;
if(rc > r[j])
break;
r[s] = r[j];
s = j;
}
r[s] = rc;
}
void heapsort(int r[]) {
int i;
int n = sizeof(r)/sizeof(r[0]);
for(i = n / 2; i >= 1; i--) {
heapadjust(r,i,n);
}
for(i = n; i > 1; i--) {
swap(r[1],r[i]);
heapadjust(r,1,i - 1);
}
}