Sort algorithm
来源:互联网 发布:最简单数据分析方法 编辑:程序博客网 时间:2024/06/01 09:50
Merge Sort / Insertion Sort/ bubble sort/ selection sort
#include <iostream>using namespace std;int CUTOFF = 7;int arrAux[7];void swap(int *xp, int *yp){ int temp = *xp; *xp = *yp; *yp = temp;}// A function to implement bubble sortvoid bubbleSort(int arr[], int n){ int i, j; for (i = 0; i < n-1; i++) // Last i elements are already in place for (j = 0; j < n-i-1; j++) if (arr[j] > arr[j+1]) swap(&arr[j], &arr[j+1]);}void insertionSort(int arr[], int n){ for (int i = 0; i < n-1; i++) { for (int j = i+1; j > 0; j--) { if (arr[j] < arr[j-1]) swap(&arr[j], &arr[j-1]); } }}void selectionSort(int arr[], int n){ for (int i = 0; i < n; i++) { int min_index = i; for (int j = i+1; j < n; j++) { if (arr[min_index] > arr[j]) min_index = j; } swap(&arr[i], &arr[min_index]); } }void merge(int* arr, int low, int mid, int high){ int p = low; int q = mid+1; for (int i = low; i <= high; i++) { arrAux[i] = arr[i]; } for (int i = low; i <= high; i++) { if (p > mid) arr[i] = arr[q++]; else if (q > high) arr[i] = arrAux[p++]; else if (arrAux[p] > arrAux[q]) { arr[i] = arrAux[q++]; } else { arr[i] = arrAux[p++]; } }}void Mergesort(int arr[], int low, int high){ // Imporvement #1 : using insertion sort for smaller array if (high < low + CUTOFF - 1) { insertionSort(arr, high-1); } if (low >= high) return; int mid = (low + high)/2; Mergesort(arr, low, mid); Mergesort(arr, mid+1, high); // Improvement #2 : Stop if it is alrady sorted if (arr[mid+1] > arr[mid]) return; merge(arr, low, mid, high);}/* Function to print an array */void printArray(int arr[], int size){ int i; for (i=0; i < size; i++) printf("%d ", arr[i]); //printf("n");}int main(){ //int arr[] = {64, 34, 25, 12, 22, 11, 90}; int arr[] = {64, 34, 25, 12, 22, 11, 90, 2, 23, 39, 89, 88, 123, 10}; int n = sizeof(arr)/sizeof(arr[0]); //selectionSort(arr, n); Mergesort(arr, 0 , n-1); printf("Sorted array: \n"); printArray(arr, n); return 0;}
#include <iostream>using namespace std;#define ARRARY_SIZE 10int aux[ARRARY_SIZE] = {};int count_inversion_merge(int* a, int first, int mid, int last){ int p1 = first; int p2 = mid+1; int count = 0; for (int i = first; i <= last; i++) { aux[i] = a[i]; } for (int i = first; i <= last; i++) { if (p1 > mid) { a[i] = aux[p2++]; } else if (p2 > last) { a[i] = aux[p1++]; } else if( aux[p1] > aux[p2]) { a[i] = aux[p2++]; count += (mid - p1 + 1); } else { a[i] = aux[p1++]; } } return count;}int count_inversion(int* a, int first, int last){ int counter = 0; if (first >= last) return 0; int mid = (first + last) / 2; int c1 = count_inversion(a, first, mid); int c2 = count_inversion(a, mid+1, last); int c3 = count_inversion_merge(a, first, mid, last); return c1+c2+c3;}int main(){ int a[] = {9, 8 , 7, 6, 5, 4, 3, 2, 1, 10}; static_assert(sizeof(a)/sizeof(int) == ARRARY_SIZE, "The arrary size doesn't match"); int len = ARRARY_SIZE; int inversion = count_inversion(a, 0, len-1); int i; for (i=0 ; i<len; i++) printf("%d ", a[i]); printf("\n"); printf("Total inversions : %d \n", inversion); return 0;}
阅读全文
0 0
- sort algorithm
- sort algorithm
- Sort algorithm
- Sort Algorithm-->Bubble Sort
- Sort Algorithm-->Select Sort
- Sort algorithm overview.
- quick sort algorithm
- insert sort algorithm
- select sort algorithm
- Insertion sort algorithm
- Algorithm---Selection Sort
- Algorithm---Insert Sort
- ACM algorithm sort
- STL: <algorithm>中 sort()
- Csharp Algorithm - Bubble Sort
- CSharp Algorithm - Shell sort
- Select Sort Algorithm
- Sort Algorithm 总结
- 进程购票加锁 python 笔记
- hive-存储格式
- 2017年第2届深圳家居设计周暨32届深圳国际家具展会刊(参展商名录)
- 解决ECLIPSE下Web APP下没有jar包的问题
- 无法访问javax.servlet.ServletException 找不到javax.servlet.ServletException的类文件
- Sort algorithm
- 从程序员之死看 IT 人士如何摆脱低情商诅咒
- javascript实现二叉树的创建,遍历,添加,查找最大值最小值和指定值的寻找及删除功能——递归的多次运用
- SylixOS屏蔽中断实现
- 从APM源码分析GPS、气压计惯导融合
- CMake 命令学习
- Spring Boot (三):Thymeleaf 的使用
- 移动端候选人面试要点
- 使用Glide实现毛玻璃的效果