码迷,mamicode.com
首页 > 编程语言 > 详细

8个排序算法——java

时间:2016-12-29 20:04:56      阅读:273      评论:0      收藏:0      [点我收藏+]

标签:sys   list   排序   uil   heap   insert   heapsort   empty   position   

  1 public static void radixsort(int[] a){
  2         int max=a[0];
  3         for(int i=1;i<a.length;i++){
  4             if (max<a[i]) {
  5                 max=a[i];
  6             }
  7         }
  8         int time=0;
  9         while(max>0){
 10             max/=10;
 11             time++;
 12         }
 13         java.util.List<ArrayList> queue=new ArrayList<ArrayList>();
 14         for(int i=0;i<10;i++){
 15             ArrayList<Integer> queue1=new ArrayList<>();
 16             queue.add(queue1);
 17         }
 18         for(int i=0;i<time;i++){
 19             for(int j=0;j<a.length;j++){
 20                 int x=a[j]%(int)Math.pow(10, i+1)/(int)Math.pow(10, i);
 21                 queue.get(x).add(a[j]);
 22             }
 23             int tmp=0;
 24             for(int j=0;j<10;j++){
 25                 while(!queue.get(j).isEmpty()){
 26                     a[tmp++]=(int)queue.get(j).remove(0);
 27                 }
 28             }
 29         }
 30         
 31         for (int i = 0; i < a.length; i++) {
 32             System.out.println("radixsort" + a[i]);
 33         }
 34     }
 35 
 36     public static void mergesort(int[] a) {
 37         sort(a, 0, a.length-1);
 38         for (int i = 0; i < a.length; i++) {
 39             System.out.println("mergesort" + a[i]);
 40         }
 41     }
 42 
 43     public static void merging(int[] a, int left, int middle, int right) {
 44         if (middle < left || middle > right) {
 45             return;
 46         }
 47         int[] tmpArray = new int[a.length];
 48         int tmp = left;
 49         int tmpIndex = left;
 50         int begin1 = left;
 51         int begin2 = middle+1;
 52         while (begin1 <= middle && begin2 <= right) {
 53             if (a[begin1] < a[begin2]) {
 54                 tmpArray[tmp++] = a[begin1++];
 55             } else {
 56                 tmpArray[tmp++] = a[begin2++];
 57             }
 58         }
 59         while (begin1 <= middle) {
 60             tmpArray[tmp++] = a[begin1++];
 61         }
 62         while (begin2 <= right) {
 63             tmpArray[tmp++] = a[begin2++];
 64         }
 65         while(tmpIndex<=right){
 66             a[tmpIndex]=tmpArray[tmpIndex++];
 67         }
 68     }
 69 
 70     public static void sort(int[] a, int left, int right) {
 71         if (left < right) {
 72             int middle = (right - left) / 2 + left;
 73             sort(a, left, middle);
 74             sort(a, middle + 1, right);
 75             merging(a, left, middle, right);
 76         }
 77     }
 78 
 79     public static void quicksort(int[] a) {
 80         System.out.println("quicksort");
 81         quick(a, 0, a.length - 1);
 82         for (int i = 0; i < a.length; i++) {
 83             System.out.println("quicksort" + a[i]);
 84         }
 85     }
 86 
 87     public static void quick(int[] a, int left, int right) {
 88         if (left < right) {
 89             int middle = getMiddle(a, left, right);
 90             quick(a, left, middle);
 91             quick(a, middle + 1, right);
 92         }
 93     }
 94 
 95     public static int getMiddle(int[] a, int left, int right) {
 96         int tmp = a[left];
 97         while (left < right) {
 98             while (a[right] >= tmp && right > left) {
 99                 right--;
100             }
101             a[left] = a[right];
102             while (a[left] <= tmp && left < right) {
103                 left++;
104             }
105             a[right] = a[left];
106         }
107         a[left] = tmp;
108         return left;
109     }
110 
111     public static void bubblesort(int[] a) {
112         for (int i = 0; i < a.length; i++) {
113             for (int j = 0; j < a.length - i - 1; j++) {
114                 if (a[j] > a[j + 1]) {
115                     swap(a, j, j + 1);
116                 }
117             }
118         }
119         for (int i = 0; i < a.length; i++) {
120             System.out.println("bubblesort" + a[i]);
121         }
122     }
123 
124     public static void heapsort(int[] a) {
125         for (int i = a.length - 1; i >= 0; i--) {
126             buildmaxheap(a, i);
127             swap(a, i, 0);
128 
129         }
130         for (int i = 0; i < a.length; i++) {
131             System.out.println("heapsort" + a[i]);
132         }
133     }
134 
135     public static void swap(int[] a, int i, int j) {
136         int temp = a[i];
137         a[i] = a[j];
138         a[j] = temp;
139     }
140 
141     public static void buildmaxheap(int[] a, int lastindex) {
142         int length = a.length;
143         if (lastindex >= length) {
144             return;
145         }
146         int index = (lastindex - 1) / 2;
147         for (; index >= 0; index--) {
148             int left = index * 2 + 1;
149             if (left <= lastindex) {
150                 if (a[index] < a[left]) {
151                     swap(a, index, left);
152                 }
153             }
154             if (left < lastindex && a[index] < a[left + 1]) {
155                 swap(a, index, left + 1);
156             }
157         }
158     }
159 
160     public static void selectsort(int[] a) {
161         int pos = 0;
162         for (int i = 0; i < a.length; i++) {
163             pos = i;
164             for (int j = i + 1; j < a.length; j++) {
165                 if (a[pos] > a[j]) {
166                     pos = j;
167                 }
168             }
169             if (pos != i) {
170                 int temp = a[i];
171                 a[i] = a[pos];
172                 a[pos] = temp;
173             }
174         }
175         for (int i = 0; i < a.length; i++) {
176             System.out.println("shellsort" + a[i]);
177         }
178     }
179 
180     public static void shellsort(int[] a) {
181         int length = a.length;
182         System.out.println(length);
183         for (int d = (int) Math.ceil(length / 2); d > 0; d /= 2) {
184             for (int i = 0; i < d; i++) {
185                 System.out.println("i=" + i + " d=" + d);
186                 for (int j = i + d; j < length; j += d) {
187                     int temp = a[j];
188                     int k = j - d;
189                     System.out.println("j=" + j + " temp=" + temp + " k=" + k);
190                     for (; k >= 0 && temp < a[k]; k -= d) {
191                         System.out.println("k+d=" + (k + d) + " k=" + k + " a[k+d]=" + a[k + d] + " a[k]=" + a[k]);
192                         a[k + d] = a[k];
193                     }
194                     System.out.println("end" + "k+d=" + (k + d) + " a[k+d]=" + a[k + d] + " temp=" + temp);
195                     a[k + d] = temp;
196                 }
197             }
198         }
199         for (int i = 0; i < a.length; i++) {
200             System.out.println("shellsort" + a[i]);
201         }
202     }
203 
204     public static void selectSort(int[] a) {
205         int length = a.length;
206         int position = 0;
207         for (int i = 0; i < length - 1; i++) {
208             int temp = a[i];
209             int j = i + 1;
210             position = i;
211             for (; j < length; j++) {
212                 if (a[j] < temp) {
213                     temp = a[j];
214                     position = j;
215                 }
216             }
217             a[position] = a[i];
218             a[i] = temp;
219         }
220         for (int i = 0; i < a.length; i++) {
221             System.out.println("selectSort" + a[i]);
222         }
223     }
224 
225     public static void insertSort(int[] a) {
226         int temp = 0;
227         for (int i = 1; i < a.length; i++) {
228             int j = i - 1;
229             temp = a[j + 1];
230             for (; j >= 0 && a[j] > temp; j--) {
231                 System.out.println(j);
232                 a[j + 1] = a[j];
233             }
234             System.out.println(j);
235             a[j + 1] = temp;
236         }
237         for (int i = 0; i < a.length; i++) {
238             System.out.println(a[i]);
239         }
240     }
241 }

 

8个排序算法——java

标签:sys   list   排序   uil   heap   insert   heapsort   empty   position   

原文地址:http://www.cnblogs.com/zl1991/p/6233994.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!