排序算法
来源:互联网 发布:填表软件 编辑:程序博客网 时间:2024/05/19 17:03
一、冒泡排序
#include <stdio.h>void bubbleSort(int a[], int n);void bubbleSort(int a[], int n) { int i, j; bool swap = true;//如果序列已经排好序,则跳出循环 while(swap) { swap = false; for(i = 0; i < n-1; i++) {//只需要进行n-1次交换 for(j = 0; j < n-1-i; j++) {//一次交换完成后,次数便减少一次 if(a[j] > a[j+1]) { int t = a[j]; a[j] = a[j+1]; a[j+1] = t; swap = true; } } } }}int main() { int a[] = {1,2,5,4,6,8,9,7,3}; int i; bubbleSort(a, 9); for(i = 0; i < 9; i++) { printf("%d ", a[i]); } printf("\n"); return 0;}
二、插入排序
#include <stdio.h>void insertSort(int a[], int n);void insertSort(int a[], int n) { int i; for(i = 1; i < n; i++) {//从第二个开始插入,第一个默认为排序完成的元素 int get = a[i]; //类似于打牌,a[i]为摸到的牌,将其插入到手中已经排好序的牌 int j = i-1;//j表示手中的牌的下标,从最后一张开始 while(j >= 0 && a[j] >= get) {//找到get插入的位置,该循环结束后j为get插入的前面的位置 a[j+1] = a[j]; j--; } a[j+1] = get; }}int main() { int a[] = {1,2,5,4,6,8,9,7,3}; int i; insertSort(a, 9); for(i = 0; i < 9; i++) { printf("%d ", a[i]); } printf("\n"); return 0;}
三、选择排序
#include <stdio.h>void selectSort(int a[], int n);void selectSort(int a[], int n) { int i, j ,minIndex; for(i = 0; i < n-1; i++) { minIndex = i;//假设第i个就是最小的 for(j = i+1; j < n; j++) {//找到最小的元素下标minIndex if(a[minIndex] > a[j]) { minIndex = j; } } if(minIndex != i) { //如果i!=minIndex说明i不是最小元素的下标,将找到的a[minIndex]与其交换 int t = a[minIndex]; a[minIndex] = a[i]; a[i] = t; } }}int main() { int a[] = {1,2,5,4,6,8,9,7,3}; int i; selectSort(a, 9); for(i = 0; i < 9; i++) { printf("%d ", a[i]); } printf("\n"); return 0;}
四、快速排序
#include <stdio.h>void quickSort(int a[], int n);void quickSort(int a[], int start, int end) { if(start >= end) return; int i = start, j = end, p = a[start]; while(i < j) { while(i < j && a[j] >= p) j--; while(i < j && a[i] <= p) i++; if(i < j) { int t = a[i]; a[i] = a[j]; a[j] = t; } } a[start] = a[i]; a[i] = p; quickSort(a, start, i-1); quickSort(a, i+1, end);}int main() { int arr[] = {0,3,5,6,8,9,7,4,2,1}; int i; int n = sizeof(arr)/sizeof(arr[0]); quickSort(arr, 0, n-1); for(i = 0; i < n; i++) { printf("%d ", arr[i]); } printf("\n"); return 0;}
五、希尔排序
#include <stdio.h>void shellSort(int a[], int count);void insert(int a[], int count, int start, int step);void insert(int a[], int count, int start, int step) { int i, j, k; for(i = start+step; i < count; i += step) { int get = a[i]; for(j = start; j < i && a[j] <= get; j += step){;} for(k = i; k > j; k -= step) { a[k] = a[k-step]; } a[j] = get; }}void shellSort(int a[], int count) { int step, start; for(step = count/2; step > 0; step /= 2) { for(start = 0; start < step; start++) { insert(a, count, start, step); } }}int main() { int arr[] = {0,3,5,6,8,9,7,4,2,1}; int i; int n = sizeof(arr)/sizeof(arr[0]); shellSort(arr, n); for(i = 0; i < n; i++) { printf("%d ", arr[i]); } printf("\n"); return 0;}
六、堆排序
#include <stdio.h>void heapSort(int *data, int count);void adjustBigHeap(int *data, int count, int root);void swap(int *one, int *another);void swap(int *one, int *another) { int t = *one; *one = *another; *another = t;}void adjustBigHeap(int *data, int count, int root) { int leftChild; int rightChild; int child; while(root <= count/2-1) {//非叶子节点 leftChild = 2*root+1; rightChild = 2*root +2; child = rightChild >= count ? leftChild : (data[leftChild] > data[rightChild] ? leftChild : rightChild); if(data[root] >= data[child]) { return; } swap(data+root, data+child); root = child; }}void heapSort(int *data, int count) { int root; for(root = count/2-1; root >= 0; root--) { adjustBigHeap(data, count, root); } do { swap(data, data+count-1); adjustBigHeap(data, --count, 0); }while(count > 1);}int main() { int arr[] = {0,3,5,6,8,9,7,4,2,1}; int i; int n = sizeof(arr)/sizeof(arr[0]); heapSort(arr, n); for(i = 0; i < n; i++) { printf("%d ", arr[i]); } printf("\n"); return 0;}
阅读全文
0 0
- 排序算法
- 排序算法
- 排序算法
- 排序算法
- 排序算法
- 排序算法
- 排序算法
- 排序算法
- 排序算法
- 排序算法
- 排序算法
- 排序算法
- 排序算法
- 排序算法
- 排序算法
- 排序算法
- 排序算法
- 排序算法
- accumulate(求和函数)
- 谨以此纪念进入程序猿的世界
- straight_insertion_sort
- python学习——通过virtualenv中安装不同版本的Python
- 巴什博奕,威佐夫博奕,尼姆博奕,斐波那契博弈模板
- 排序算法
- tab点击,图片放大缩小动画
- Git_解决库早已经存在问题
- pygame库写游戏——入门<6>——存储图像
- leetCode-Spiral Matrix II
- 关于IP协议首部长度的计算
- 【Scikit-Learn 中文文档】模型选择:选择估计量及其参数
- 【耀阳的读书笔记】算法导论(2)_Θ、O、Ω傻傻分不清楚
- 云南之旅:随想+攻略