简单排序

简单排序分为冒泡排序、直接排序和,直接

 1 /** 
 2  * @ClassName: Sort 
 3  * @Description: 简单排序
 4  * @author dongye 
 5  * @date 2016年3月2日 上午10:26:49 
 6  *  
 7  */
 8 public class Sort {
 9     /** 
10      * @Description: 冒泡排序
11      *  比较相邻的元素。如果第一个比第二个大,就交换他们两个。
12      * @author dongye  
13      * @date 2016年3月2日 上午10:27:14 
14      */
15     public static void bubbleSort(long[] arr) {
16         long tmp=0;
17         for (int i = 0; i < arr.length-1; i++) {
18             for (int j = arr.length-1; j > i; j--) {
19                 if(arr[j]<arr[j-1]){
20                     tmp=arr[j];
21                     arr[j]=arr[j-1];
22                     arr[j-1]=tmp;
23                 }
24             }
25         }
26     }
27     
28     /** 
29      * @Description: 直接排序
30      * 在未排序序列中找到最小元素,存放到排序序列的最后位置
31      * @author dongye  
32      * @date 2016年3月2日 上午18:57:14 
33      */
34     public static void selectionSort(long[] arr) {
35         long tmp=0;
36         int k=0;
37         for (int i = 0; i < arr.length-1; i++) {
38             k=i;
39             for (int j = i+1; j < arr.length; j++) {
40                 if(arr[j]<arr[k]){
41                     k=j;
42                 }
43             }
44             tmp=arr[k];
45             arr[k]=arr[i];
46             arr[i]=tmp;
47             
48         }
49     }
50     
51     /** 
52      * @Description: 直接插入排序
53      * 先取出一个元素,和前面已经排过的对比,直到比他小停止交换,然后把这个元素放在这个位置
54      * @author dongye  
55      * @date 2016年3月2日 上午18:57:14 
56      */
57     public static void insertSort(long[] arr) {
58         long tmp = 0;
59         int j;
60         for (int i = 1; i < arr.length; i++) {
61             tmp = arr[i];
62             j = i;
63             while (j > 0 && arr[j-1] >= tmp) {
64                 arr[j] = arr[j - 1];
65                 j--;
66             }
67             arr[j] = tmp;
68         }
69     }
70 }

 

插入排序

posted @ 2016-03-02 19:49  凌晨五点半  阅读(142)  评论(0编辑  收藏  举报