学习笔记---算法

今天无聊又开始在看算法了,可惜看那么久还是在看简单的排序,把今天的成果贴出来吧

 1 package sort;
 2 
 3 //冒泡排序
 4 public class BubbleSort {
 5     //the bubblesort method
 6     public static int[] bubbleSort(int[] list) {
 7         //the boolean varaible for check is the last varaible of the list
 8         //if true,the sort method will be stoped.
 9         boolean isNeedNextSort = true;
10         //if index equals list's length
11         for (int index = 1;index < list.length && isNeedNextSort;index++) {
12             //let the boolean varaible be false;
13             isNeedNextSort = false;
14             //if  the varaible nor lower than list.length-index,
15             //in other word,the list's length is equals index,
16             //the next sort can be stop.
17             for(int varaible = 0;varaible < list.length-index;varaible++){
18                 if(list[varaible] > list[varaible+1]){
19                     int temp = list[varaible];
20                     list[varaible] = list[varaible+1];
21                     list[varaible+1] = temp;
22                 }
23                 //there will be a next sort
24                 isNeedNextSort = true;
25             }
26         }
27         return list;
28     }
29 }
 1 package sort;
 2 
 3 //归并排序,至今还有点迷糊,第二次mergeSort()怎么执行的
 4 public class MergeSort {
 5     
 6     /**
 7      * The MegerSort Method 
 8      */
 9     
10     public static int[] mergeSort(int[] list){
11         /**
12          * MergeSort the first half
13          */
14         
15         //if there has more than 1 elements
16         if(list.length > 1){
17             //create a array which can contain the list's half
18             int[] firstHalf = new int[list.length/2];
19             //arraycopy:copy the first half of list to firstHalf
20             System.arraycopy(list, 0, firstHalf, 0, list.length/2);
21             //mergesort the half too,until it's elements is 1
22             mergeSort(firstHalf);
23             
24             /**
25              * MergeSort the secondhalf
26              */
27             
28             //figureout the secondHalf array's length
29             int secondHalfLength = list.length-list.length/2;
30             //create the secondhalf array
31             int[] secondHalf = new int[secondHalfLength];
32             //arraycopy:copy the second half of list to secondHalf
33             System.arraycopy(list, list.length/2, secondHalf, 0, secondHalfLength);
34             //mergesort the half too,until it's elements is 1
35             mergeSort(secondHalf);
36             
37             int[] temp = merge(firstHalf,secondHalf);
38             System.arraycopy(temp, 0, list, 0, temp.length);
39         }
40         return list;
41     }
42     
43     /**
44      * The Meger Method
45      */
46     
47     private static int[] merge(int[] firstHalf, int[] secondHalf) {
48         //create a array called temp,it's length equals list's
49         int[] temp = new int[firstHalf.length+secondHalf.length];
50         //create three index of three array
51         int firstHalf_index = 0;
52         int secondHalf_index = 0;
53         int temp_index = 0;
54         //all the rest code is just for sort the all elements
55         while (firstHalf_index < firstHalf.length && secondHalf_index < secondHalf.length) {
56             if (firstHalf[firstHalf_index] <= secondHalf[secondHalf_index]) {
57                 temp[temp_index] = firstHalf[firstHalf_index];
58                 firstHalf_index++;
59             } else {
60                 temp[temp_index] = secondHalf[secondHalf_index];
61                 secondHalf_index++;
62             }
63             temp_index++;
64         }
65  
66         if (firstHalf_index == firstHalf.length) {
67             while (secondHalf_index < secondHalf.length) {
68                 temp[temp_index++] = secondHalf[secondHalf_index++];
69             }
70         } else { 
71             // secondHalf_index==secondHalf.length
72             while (firstHalf_index < firstHalf.length) {
73                 temp[temp_index++] = firstHalf[firstHalf_index++];
74             }
75         }
76         return temp;
77     }
78 }

 

 

posted @ 2013-08-21 22:05  渲染的青春  阅读(212)  评论(0编辑  收藏  举报