排序
2016-06-11 02:22
288 查看
《数据结构与算法分析——c语言描述》 第七章
插入排序
希尔排序
堆排序
归并排序
快速排序
#include<stdlib.h>
#include<stdio.h>
int RandInt(int i, int j) {
int temp;
temp = (int)(i + (1.0*rand() / RAND_MAX)*(j - i));
return temp;
}
void getRandomInt(int *A, int n) {
for (int i = 0; i < n; i++) {
A[i] = i + 1;
}
for (int i = 1; i < n; i++) {
//std::swap(A[i], A[RandInt(0, i)]);
int randAdrr = RandInt(0, i);
int t = A[i];
A[i] = A[randAdrr];
A[randAdrr] = t;
}
}
int a[99999999];
typedef int ElementType;
void insertionSort(int *a, int n) {
int j, p;
int temp;
for (p = 1; p < n; p++) {
temp = a[p];
for (j = p; j > 0 && temp < a[j - 1]; j--)
a[j] = a[j - 1];
a[j] = temp;
}
}
void swap_my(ElementType *a, ElementType *b) {
ElementType temp;
temp = *a;
*a = *b;
*b = temp;
}
ElementType median3(ElementType a[], int left, int right) {
int center = (left + right) / 2;
if (a[left] > a[center])
swap_my(&a[left], &a[center]);
if (a[left] > a[right])
swap_my(&a[left], &a[right]);
if (a[center] > a[right])
swap_my(&a[center], &a[right]);
swap_my(&a[center], &a[right - 1]);
return a[right - 1];
}
#define CUTOFF (3)
void qsort_mya(ElementType a[], int left, int right) {
if (left + CUTOFF <= right) {
int i, j;
ElementType pivot;
pivot = median3(a, left, right);
i = left;
j = right - 1;
while (1) {
while (a[++i] < pivot) {}
while (a[--j] > pivot) {}
if (i < j)
swap_my(&a[i], &a[j]);
else
break;
}
swap_my(&a[i], &a[right - 1]);
qsort_mya(a, left, i - 1);
qsort_mya(a, i + 1, right);
}
else
insertionSort(a + left, right - left + 1);
}
void quickSort_my(ElementType a[], int n) {
qsort_mya(a, 0, n - 1);
}
#define N 22
int main() {
getRandomInt(a, N);
quickSort_my(a, N);
int cnt = 1;
for (int i = 0; i < N; i++)
if (cnt == a[i])
cnt++;
else
printf("error");
/*printf("%d ", a[i]);*/
}
插入排序
#include<stdlib.h> #include<stdio.h> #include"fatal.h" int RandInt(int i, int j) { int temp; temp = (int)(i + (1.0*rand() / RAND_MAX)*(j - i)); return temp; } void getRandomInt(int *A, int n) { for (int i = 0; i < n; i++) { A[i] = i + 1; } for (int i = 1; i < n; i++) { //std::swap(A[i], A[RandInt(0, i)]); int randAdrr = RandInt(0, i); int t = A[i]; A[i] = A[randAdrr]; A[randAdrr] = t; } } int a[99999999]; void insertionSort(int *a, int n) { int j, p; int temp; for (p = 1; p < n; p++) { temp = a[p]; for (j = p; j > 0 && temp < a[j - 1]; j--) a[j] = a[j - 1]; a[j] = temp; } } #define N 1000 int main() { getRandomInt(a, N); insertionSort(a, N); int cnt = 1; for (int i = 0; i < N; i++) if (cnt == a[i]) cnt++; else Error("ERROR"); }
希尔排序
#include<stdlib.h> #include<stdio.h> #include"fatal.h" int RandInt(int i, int j) { int temp; temp = (int)(i + (1.0*rand() / RAND_MAX)*(j - i)); return temp; } void getRandomInt(int *A, int n) { for (int i = 0; i < n; i++) { A[i] = i + 1; } for (int i = 1; i < n; i++) { //std::swap(A[i], A[RandInt(0, i)]); int randAdrr = RandInt(0, i); int t = A[i]; A[i] = A[randAdrr]; A[randAdrr] = t; } } int a[99999999]; void shellSort(int *a, int n) { int i, j, increment; int temp; for (increment = n / 2; increment > 0; increment /= 2) { for (i = increment; i < n; i++) { temp = a[i]; for (j = i; j >= increment; j -= increment) { if (temp < a[j - increment]) a[j] = a[j - increment]; else break; } a[j] = temp; } } } #define N 10000000 int main() { getRandomInt(a, N); shellSort(a, N); int cnt = 1; for (int i = 0; i < N; i++) if (cnt == a[i]) cnt++; else Error("ERROR"); }
堆排序
#include<stdlib.h> #include<stdio.h> #include"fatal.h" int RandInt(int i, int j) { int temp; temp = (int)(i + (1.0*rand() / RAND_MAX)*(j - i)); return temp; } void getRandomInt(int *A, int n) { for (int i = 0; i < n; i++) { A[i] = i + 1; } for (int i = 1; i < n; i++) { //std::swap(A[i], A[RandInt(0, i)]); int randAdrr = RandInt(0, i); int t = A[i]; A[i] = A[randAdrr]; A[randAdrr] = t; } } int a[99999999]; #define leftChild(i) (2*(i)+1) void percDown(int *a, int i, int n) { int child = leftChild(i); int temp; for (temp = a[i]; leftChild(i) < n; i=child) { child = leftChild(i); if (child != n - 1 && a[child] < a[child + 1]) child++; if (temp < a[child]) a[i] = a[child]; else break; } a[i] = temp; } void heapSort(int *a, int n) { for (int i = (n-1) / 2; i >= 0; i--) { percDown(a, i, n); } for (int i = n - 1; i > 0; i--) { int temp = a[0]; a[0] = a[i]; a[i] = temp; percDown(a, 0, i); } } #define N 10000 int main() { getRandomInt(a, N); heapSort(a, N); int cnt = 1; for (int i = 0; i < N; i++) if (cnt == a[i]) cnt++; else Error("error"); }
归并排序
#include<stdlib.h> #include<stdio.h> #include"fatal.h" int RandInt(int i, int j) { int temp; temp = (int)(i + (1.0*rand() / RAND_MAX)*(j - i)); return temp; } void getRandomInt(int *A, int n) { for (int i = 0; i < n; i++) { A[i] = i + 1; } for (int i = 1; i < n; i++) { //std::swap(A[i], A[RandInt(0, i)]); int randAdrr = RandInt(0, i); int t = A[i]; A[i] = A[randAdrr]; A[randAdrr] = t; } } int a[99999999]; void merge(int a[], int tempArray[], int left, int right, int rightEnd) { int i, j, k; int leftEnd; for (i = left, j = right, k = left, leftEnd = right - 1; i <= leftEnd && j <= rightEnd; k++) { if (a[i] < a[j]) { tempArray[k] = a[i]; i++; } else { tempArray[k] = a[j]; j++; } } while (i <= leftEnd) { tempArray[k++] = a[i++]; } while (j <= rightEnd) { tempArray[k++] = a[j++]; } for (int i = left; i <= rightEnd; i++) a[i] = tempArray[i]; } void mSort(int a[], int tempArray[], int left, int right) { if (left < right) { int center = (left + right) / 2; mSort(a, tempArray, left, center); mSort(a, tempArray, center + 1, right); merge(a, tempArray, left, center + 1, right); } } void mergeSort(int a[], int n) { int *tempArray = malloc(sizeof(int)*n); if (tempArray == NULL) Error("OUT OF MEMORY"); mSort(a, tempArray, 0, n - 1); } #define N 10000000 int main() { getRandomInt(a, N); mergeSort(a, N); int cnt = 1; for (int i = 0; i < N; i++) if (cnt == a[i]) cnt++; else Error("error"); /*printf("%d ", a[i]);*/ }
快速排序
#include<stdlib.h>
#include<stdio.h>
int RandInt(int i, int j) {
int temp;
temp = (int)(i + (1.0*rand() / RAND_MAX)*(j - i));
return temp;
}
void getRandomInt(int *A, int n) {
for (int i = 0; i < n; i++) {
A[i] = i + 1;
}
for (int i = 1; i < n; i++) {
//std::swap(A[i], A[RandInt(0, i)]);
int randAdrr = RandInt(0, i);
int t = A[i];
A[i] = A[randAdrr];
A[randAdrr] = t;
}
}
int a[99999999];
typedef int ElementType;
void insertionSort(int *a, int n) {
int j, p;
int temp;
for (p = 1; p < n; p++) {
temp = a[p];
for (j = p; j > 0 && temp < a[j - 1]; j--)
a[j] = a[j - 1];
a[j] = temp;
}
}
void swap_my(ElementType *a, ElementType *b) {
ElementType temp;
temp = *a;
*a = *b;
*b = temp;
}
ElementType median3(ElementType a[], int left, int right) {
int center = (left + right) / 2;
if (a[left] > a[center])
swap_my(&a[left], &a[center]);
if (a[left] > a[right])
swap_my(&a[left], &a[right]);
if (a[center] > a[right])
swap_my(&a[center], &a[right]);
swap_my(&a[center], &a[right - 1]);
return a[right - 1];
}
#define CUTOFF (3)
void qsort_mya(ElementType a[], int left, int right) {
if (left + CUTOFF <= right) {
int i, j;
ElementType pivot;
pivot = median3(a, left, right);
i = left;
j = right - 1;
while (1) {
while (a[++i] < pivot) {}
while (a[--j] > pivot) {}
if (i < j)
swap_my(&a[i], &a[j]);
else
break;
}
swap_my(&a[i], &a[right - 1]);
qsort_mya(a, left, i - 1);
qsort_mya(a, i + 1, right);
}
else
insertionSort(a + left, right - left + 1);
}
void quickSort_my(ElementType a[], int n) {
qsort_mya(a, 0, n - 1);
}
#define N 22
int main() {
getRandomInt(a, N);
quickSort_my(a, N);
int cnt = 1;
for (int i = 0; i < N; i++)
if (cnt == a[i])
cnt++;
else
printf("error");
/*printf("%d ", a[i]);*/
}
相关文章推荐
- 【Linux学习日志】源码编译安装GVIM7.4/VIM7.4
- hexo写博客
- 承接Unreal4外包虚幻外包,北京正规公司
- ACtion的多例和单例(网站看到的。自己遇到的)
- VR软件定制外包团队:国内设备开发公司已超90家
- Linux 下挂载硬盘方法
- VR内容定制请找北京动软VR团队,长年承接VR/AR应用、游戏内容定制
- 【树莓派2B折腾日志】无法获得锁 /var/lib/dpkg/lock - open (11: 资源临时不可用)
- Intersection of Two Arrays II
- 网站手机号码抓取软件哪个好? 自己使用的经历推荐
- Contains Duplicate
- 构建DHCP服务器(以Centos为例)
- centOS升级python2.6至2.7
- iOS 事件处理之UIResponder简介
- golang 远程连接mssql 2000
- Leetcode 157,158, Read N Characters Given Read4
- 最快的了解安卓中常用的设计模式
- Windows 消息机制浅析
- Majority Element
- Core Dump 调试方法