跳跃空间(链表)排序 选择排序(selection sort),插入排序(insertion sort) 选择排序(selection sort) 算法原理:有一筐苹果,先挑出最大的一个放在最后,然后再跳出一个筐里剩下的最大的一个,放在刚才跳出来的最大的前面,以此类推,最后就排好顺序了. 代码: //从起始于位置p的n个元素中选出最大者,所以n>1 template<typename T> ListNode<T>* List<T>::selectMax(ListN…
最近从网易公开课在看麻省理工学院的公开课<算法导论>,感觉还不错,接下来几篇文章所示学习日记了,不准备对算法细节做过多描述,感兴趣的可以自己去看. 文章分几篇讲经典排序算法,直接上代码,根据结果对算法性能有个直观了解.本篇先说插入排序(insertion sort). (一)算法实现 protected void sort(int[] toSort) { if (toSort.length <= 1) { return; } for (int i = 1; i < toSort.l…
前言 我们知道,通过比较两个数大小来进行排序的算法(比如插入排序,合并排序,以及上文提到的快速排序等)的时间复杂度至少是Θ(nlgn),这是因为比较排序对应的决策树的高度至少是Θ(nlgn),所以排序最坏情况肯定是Θ(nlgn).那有没有哪种排序算法的时间复杂度是线性的(Θ(n))呢?(因为我们如果要对一个数组排序,肯定至少要考察每个元素,因此可以推断Θ(n)是所有排序算法的下界).答案是:在一定条件下,是有的. 思考过程 为了更好的理解计数排序,我们先来想象一下如果一个数组里所有元素都是整数,…
计数排序法:计数数组适用于当前数组密集的情况.例如(2,3,5,4,2,3,3,2,5,4) 方法:先找出最大值最小值,之后统计每个数出现的次数,根据次数从小到大往数组里添加 计数排序法是一种不需要比较的排序方法 void count(int top,int length,int arr[]) { ],max=arr[],i=,j=; )); )return; while(i<length)//先找出最大和最小值 { if(min>arr[i]) { min=arr[i]; } if(max&…
Bucket Sort is a sorting method that subdivides the given data into various buckets depending on certain characteristic order, thuspartially sorting them in the first go.Then depending on the number of entities in each bucket, it employs either bucke…
//counting sort 计数排序 //参考算法导论8.2节 #include<cstdio> #include<cstring> #include<algorithm> #include<cassert> using namespace std; ; ; , , , , , , }; int b[n]; ]; int main() { ; memset(c, , sizeof c); ;i<n;i++) { c[a[i]]++; maxn=ma…
排序算法--插入排序(Insertion Sort)_C#程序实现 排序(Sort)是计算机程序设计中的一种重要操作,也是日常生活中经常遇到的问题.例如,字典中的单词是以字母的顺序排列,否则,使用起来非常困难.同样,存储在计算机中的数据的次序,对于处理这些数据的算法的速度和简便性而言,也具有非常深远的意义. 1.基本概念 排序是把一个记录(在排序中把数据元素称为记录)集合或序列重新排列成按记录的某个数据项值递增(或递减)的序列. 2插入排序(Insertion Sort) 插入排序(direct…
经典排序算法 – 插入排序Insertion sort  插入排序就是每一步都将一个待排数据按其大小插入到已经排序的数据中的适当位置,直到全部插入完毕. 插入排序方法分直接插入排序和折半插入排序两种,这里只介绍直接插入排序,折半插入排序留到“查找”内容中进行.   图1演示了对4个元素进行直接插入排序的过程,共需要(a),(b),(c)三次插入. 以下代码仅供参考,欢迎指正 /// <summary> /// 插入排序 /// </summary> /// <param na…
B Age Sort Input: Standard Input Output: Standard Output   You are given the ages (in years) of all people of a country with at least 1 year of age. You know that no individual in that country lives for 100 or more years. Now, you are given a very si…
#include <iostream.h> #define  MAX 100 void dispaly(int a[],int n) {     for(int i=0;i<n;i++)     {         cout<<"a["<<i<<"]="<<a[i]<<" ";     }     cout<<endl;      } // bubble s…