排序

一     常见快排;

  1. 一  less<数据类型>()//从小到大排序
  2.  greater<数据类型>()//从大到小排序
  3. sort(a,a+10,less<int>());
  4. sort(a,a+10,greater<int>());

5 .sort(a,a+10,greater<char>())。

二    插入排序

插入排序由N-1趟排序组成,对于P=1到N-1趟,插入排序保证从位置0到位置P上的元素为已排序状态。

简单的说,就是插入排序总共需要排序N-1趟,从index为1开始,讲该位置上的元素与之前的元素比较,放入合适的位置,这样循环下来之后,即为有序数组。

1 for(int i=1;i<a.length;i++) {
2             //只能从当前索引往前循环,因为索引前的数组皆为有序的,索引只要确定当前索引的数据的为止即可
3             for(int j=i;j>0 && a[j] < a[j-1];j--) {
4                 temp = a[j];
5                 a[j] = a[j-1];
6                 a[j-1] = temp;
7             }
8             print(i +"",a);
9 }

三    希尔排序

 1 public class ShellSort extends SortBase {
 2 
 3     @Override
 4     public Integer[] sort(Integer[] a) {
 5         // TODO Auto-generated method stub
 6         print("init",a);
 7         Integer h = a.length;
 8         Integer temp = 0;
 9         while(h >= 1) {
10             for(int i=h;i<a.length;i++) {
11                 for(int j=i;j>=h && a[j] < a[j-h];j -= h) {
12                     temp = a[j];
13                     a[j] = a[j-h];
14                     a[j-h] = temp;
15                     
16                 }
17             }
18             h /= 9;
19         }
20         print("result",a);
21         return a;
22     }
23     
24     public static void main(String[] args) {
25         Integer[] a = {2,1,5,9,0,6,8,7,3};
26         (new ShellSort()).sort(a);
27     }
28 }
View Code

详情查看:http://www.cnblogs.com/edwinchen/p/4782179.html

原文地址:https://www.cnblogs.com/lengsong/p/10224354.html