Sort
来源:互联网 发布:恺英网络王悦老婆 编辑:程序博客网 时间:2024/05/22 01:15
SHELL
// merge algorithm example#include <iostream>#include <algorithm>using namespace std;void shellsort(int v[],int n){ int gap,i,j,temp; for (gap = n/2; gap > 0; gap=gap/3+1) { for(i = gap; i < n; i++) for(j = i-gap; j>=0 && v[j]>v[j+gap]; j -= gap) { temp = v[j]; v[j] = v[j+gap]; v[j+gap] = temp; } }}void Shellsort(int s[],const int left,const int right){ int i,j,gap=right-left+1;//增量的初始化 int temp; do{ gap=gap/3+1; //求下一个增量 for(i = left+gap;i<=right;i++)//各子序列交替处理 if(s[i]<s[i-gap]){//逆序 temp=s[i];j=i-gap; do{ s[j+gap]=s[j];//后移元素 j=j-gap;//再比较前一元素 }while(j>=left&&temp<s[j]); s[j+gap]=temp;//将s[i]回送 } }while(gap>1);}int main () { int first[] = {5,10,15,20,25}; int second[] ={ 57,757,75,5,577, 5,5,5,5,6 ,6,6,6,66,5}; int v[10];// sort(first,first+5);// sort(second,second+5);// merge(first,first+5,second,second+10,v);// cout << "The resulting vector contains:";// for(int i=0;i<15;i++)// cout<<v[i]<<" "; cout << endl; Shellsort(second,0,14); for (int i=0;i<sizeof(second)/sizeof(int);i++) { cout<<second[i]<<" "; } return 0;}
Heapsort
/*堆排序(大顶堆) 2011.9.14*/#include <iostream>#include<algorithm>#include<stdio.h>using namespace std;void HeapAdjust(int *a,int i,int size) //调整堆{ int lchild=2*i; //i的左孩子节点序号 int rchild=2*i+1; //i的右孩子节点序号 int max=i; //临时变量 if(i<=size/2) //如果i不是叶节点就不用进行调整 { if(lchild<=size&&a[lchild]>a[max]) { max=lchild; } if(rchild<=size&&a[rchild]>a[max]) { max=rchild; } if(max!=i) { swap(a[i],a[max]); HeapAdjust(a,max,size); //避免调整之后以max为父节点的子树不是堆 } }}void BuildHeap(int *a,int size) //建立堆{ int i; for(i=size/2;i>=1;i--) //非叶节点最大序号值为size/2 { HeapAdjust(a,i,size); }}void HeapSort(int *a,int size) //堆排序{ int i; BuildHeap(a,size); for(i=size;i>=1;i--) { //cout<<a[1]<<" "; swap(a[1],a[i]); //交换堆顶和最后一个元素,即每次将剩余元素中的最大者放到最后面 //BuildHeap(a,i-1); //将余下元素重新建立为大顶堆 HeapAdjust(a,1,i-1); //重新调整堆顶节点成为大顶堆 }}int main(int argc, char *argv[]){ //int a[]={0,16,20,3,11,17,8}; int i; int a[]={10,9,8,7,6,5,4,3,2,1}; int size=10; HeapSort(a,size); for(i=1;i<=size;i++) cout<<a[i]<<" "; return 0;}
MergeSort
http://blog.csdn.net/morewindows/article/details/6678165/
// merge algorithm example#include <iostream>#include <algorithm>using namespace std;//将有序数组a[]和b[]合并到c[]中void MemeryArray(int a[], int n, int b[], int m, int c[]){ int i, j, k; i = j = k = 0; while (i < n && j < m) { if (a[i] < b[j]) c[k++] = a[i++]; else c[k++] = b[j++]; } while (i < n) c[k++] = a[i++]; while (j < m) c[k++] = b[j++];} //将有二个有序数列a[first...mid]和a[mid...last]合并。void mergearray(int a[], int first, int mid, int last, int temp[]){ int i = first, j = mid + 1; int m = mid, n = last; int k = 0; while (i <= m && j <= n) { if (a[i] <= a[j]) temp[k++] = a[i++]; else temp[k++] = a[j++]; } while (i <= m) temp[k++] = a[i++]; while (j <= n) temp[k++] = a[j++]; for (i = 0; i < k; i++) a[first + i] = temp[i];}void mergesort(int a[], int first, int last, int temp[]){ if (first < last) { int mid = (first + last) / 2; mergesort(a, first, mid, temp); //左边有序 mergesort(a, mid + 1, last, temp); //右边有序 mergearray(a, first, mid, last, temp); //再将二个有序数列合并 }}bool MergeSort(int a[], int n){ int *p = new int[n]; if (p == NULL) return false; mergesort(a, 0, n - 1, p); delete[] p; return true;}int main () { int first[] = {5,10,15,20,25}; int s[] ={ 57,757,75,5,577, 5,5,5,5,6 ,6,6,6,66,5}; MergeSort(s,15); for(int i=0;i<sizeof(s)/sizeof(int);i++) cout<<s[i]<<" "; return 0;}
阅读全文
0 0
- sort
- Sort
- sort
- Sort
- sort
- Sort
- sort
- sort
- Sort
- Sort
- sort
- sort
- sort
- sort()
- sort
- sort
- sort
- sort
- POJ 1973 Software Company 笔记
- Android 进阶16:IntentService 使用及源码解析
- 【Leetcode】58. Length of Last Word
- 流程python学习笔记:第三章(2)
- 以空格为分隔符的字符串提取
- Sort
- Almost Sorted Array HDU
- (牛客网)构建乘积数组
- Qt笔记(1)连接 SQL Server 数据库
- 漠然的第一篇博客
- CMake 构建动态链接库
- IPC之—信号量
- jstl url标签
- git 使用笔记以及github注册以及创建仓库方法