随笔分类 -  排序

摘要:1 #include 2 #include 3 #include 4 #include 5 #include 6 #include 7 using namespace std; 8 9 const int nMax = 3000; 10 int A[nMax+1]; 11 int B[nMax+1];//用来每次5分法后保存要比较的值在A中的下... 阅读全文
posted @ 2018-06-10 20:38 myth_HG 阅读(1064) 评论(0) 推荐(0)
摘要:#include <stdio.h>#include <iostream>#include<algorithm>#include<windows.h>#include<math.h>using namespace std;#define Max 20#define LT(a,b) ((a)<(b))void setcolor(int color){ HANDLE hout=GetStdHandle(STD_OUTPUT_HANDLE); SetConsoleTextAttribute(hout,color);}typedef s 阅读全文
posted @ 2013-01-17 16:50 myth_HG 阅读(547) 评论(0) 推荐(0)
摘要:#include<iostream>using namespace std;void Adjust(int k[],int i,int n){ int j; int temp; temp=k[i]; j=2*i; while(j<=n) { if(j<n&&k[j]<k[j+1]) j++; if(temp>=k[j]) break; k[j/2]=k[j]; j=2*j; } k[j/2]=temp;}void Heapsort(int k[],... 阅读全文
posted @ 2012-09-11 21:09 myth_HG 阅读(428) 评论(0) 推荐(0)
摘要://sort快速排序法#include<iostream>#include<algorithm>using namespace std;void Quick(int k[],int s,int t){ int i,j; if(s<t) { i=s;//0表示第一个位置 j=t+1;//j表示第n+1个位置 while(1) { do i++; while(!(k[s]<=k[i]||i==t)); do j--; while(!(k... 阅读全文
posted @ 2012-09-10 17:47 myth_HG 阅读(254) 评论(0) 推荐(0)
摘要://谢尔排序又称缩小增量排序法(对直接插入法的改进)#include<iostream>using namespace std;void ShellSort(int k[],int n){ int i,j,flag,gap=n; int temp; while(gap>1) { gap=gap/2; do{ flag=0; for(i=0;i<=n-gap;i++) { j=i+gap; if(k[i]>k[j]) ... 阅读全文
posted @ 2012-09-09 23:51 myth_HG 阅读(235) 评论(0) 推荐(0)
摘要:#include<iostream>#include<algorithm>using namespace std;int comp(const void *a,const void *b){ return ((int *)a)[0]<((int *)b)[0];}int main(){int a[5][5] = {{5,2,3,4,5}, {4,3,2,5,6}, {9,2,4,5,7}, {3,2,4,5,7}, {2,2,4,8,7}};qsort(a[0],5,sizeof(int),comp);//a[0]被排序qsort(a,5... 阅读全文
posted @ 2012-08-01 17:58 myth_HG 阅读(451) 评论(1) 推荐(0)
摘要:归并(Merge)排序法是将两个(或两个以上)有序表合并成一个新的有序表,即把待排序序列分为若干个子序列,每个子序列是有序的。然后再把有序子序列合并为整体有序序列。如 设有数列{6,202,100,301,38,8,1} 初始状态: [6] [202] [100] [301] [38] [8] [1] 比较次数 i=1 [6 202 ] [ 100 301] [ 8 38] [ 1 ] 3 i=2 [ 6 100 202 301 ] [ 1 8 38 ] 4 i=3 [ 1 6 8 38 100 202 301 ] 4 总计: 11次#include <stdio.h>#inclu 阅读全文
posted @ 2012-07-30 18:49 myth_HG 阅读(152) 评论(0) 推荐(0)
摘要:#include <stdio.h>#define N 10int main(){ int a[N]; int i,j,temp; for(i=0;i<N;i++) scanf("%d",&a[i]); for(i=0;i<N-1;i++) //冒泡排序的核心代码,通过比较相邻两个,达到排序效果. for(j=1;j<N-i;j++)//可以自己举例子按算法过程来实现 if(a[j]<a[j-1]) { temp=a[j]; a[j]=a[j-1]; a[j-1]=temp; } for(i=0;i<N;i++) print 阅读全文
posted @ 2012-07-30 16:54 myth_HG 阅读(159) 评论(0) 推荐(0)
摘要:插入排序,就像好多书上讲的像抓牌时将刚抓的新牌插入手里已经排好序的牌。a数组像牌堆,b数组像已经抓到我们手里的牌#include <stdio.h>#define N 10 //这里是定义要排序的数目int main(){ int a[N],b[N]; //定义数组,a数组用来存输入的数,b数组用来存排序的结果 int i,j; for(i=0;i<N;i++) scanf("%d",&a[i]); b[0]=a[0]; for(i=1;i<N;i++) { j=i; while(j&&b[j-1]>a[i])//这里作 阅读全文
posted @ 2012-07-30 15:56 myth_HG 阅读(132) 评论(0) 推荐(0)