Guest User

Untitled

a guest
Jul 23rd, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.74 KB | None | 0 0
  1. public class SortDemo {
  2. public static void main(String[] args) {
  3. RandomArrays numbers = new RandomArrays(10);
  4. int [] arrays = numbers.arrays;
  5. printArrays(arrays);
  6. long a = System.currentTimeMillis();
  7. //selectionSort(arrays);
  8. // bubbleSort(arrays);
  9. mergeSort(arrays);
  10. //insertionSort(arrays);
  11. long time = System.currentTimeMillis() - a;
  12. System.out.println();
  13. printArrays(arrays);
  14. System.out.println("");
  15. System.out.println(time);
  16. }
  17.  
  18. /**
  19. * 冒泡排序
  20. *复杂度0(n^2)
  21. *
  22. *
  23. * */
  24. static void bubbleSort(int[] numbers){
  25. boolean needNextPass = true; //是否已经排序完成
  26. for (int i = 0; i <numbers.length-1&&needNextPass; i++) {
  27. needNextPass = false;
  28. for (int j = i+1; j <numbers.length ; j++) {
  29.  
  30. if(numbers[i]>numbers[j]){
  31. swap(numbers,i,j);
  32. needNextPass = true;
  33. }
  34. }
  35.  
  36. }
  37. }
  38.  
  39.  
  40.  
  41. /**
  42. *@param numbers 待排序数组
  43. * TODO: 2018-7-17
  44. * authors zhuwanqi
  45. * 选择排序 每次排序将剩余待排数组的最小值放到第一个位置
  46. * 优化后 每次排序将最大值放入最后一个位置
  47. * 复杂度0(n^2)
  48. */
  49.  
  50. static void selectionSort(int[] numbers){
  51. int minindex ; //目前最小值的位置
  52. int tempmin;//目前最小值
  53. int tempmax ;//目前最大值
  54. int maxindex;//目前最大值所在位置
  55. for (int i = 0; i <numbers.length/2; i++) {
  56. minindex = i;
  57. tempmin = numbers[i];
  58. tempmax = numbers[numbers.length-i-1];
  59. maxindex = numbers.length-i-1;
  60. for (int j = i+1; j < numbers.length-i; j++) {
  61. if(tempmin>numbers[j]){
  62. tempmin = numbers[j];
  63. minindex = j ;
  64. }
  65. if(tempmax<numbers[numbers.length-j]){
  66. tempmax = numbers[numbers.length-j];
  67. maxindex = numbers.length-j;
  68. }
  69. }
  70. swap(numbers,i,minindex);
  71. swap(numbers,(numbers.length-i-1),maxindex);
  72. }
  73.  
  74.  
  75. }
  76.  
  77. /**
  78. * 插入排序
  79. * 将值插入到已经排好顺序的数组中。
  80. * 复杂度O(n^2)
  81. * */
  82. static void insertionSort(int[] numbers){
  83. for (int i = 0; i <numbers.length ; i++) {
  84. int currentElement = numbers[i];
  85. int j;
  86. for ( j = i-1; j >=0&& numbers[j]>currentElement ; j--) {
  87. numbers[j+1]=numbers[j];
  88. }
  89. numbers[j+1] = currentElement;
  90. }
  91. }
  92.  
  93. /**
  94. * 归并排序
  95. * 将数组分为两半分别排序 再合到一起
  96. *
  97. * */
  98. static void mergeSort(int[] numbers){
  99. if(numbers.length>1){
  100. int[] firstHalf = new int[numbers.length/2];
  101. System.arraycopy(numbers,0,firstHalf,0,numbers.length/2);
  102. mergeSort(firstHalf);
  103. //对第二部分排序
  104. int secondHalfLength = numbers.length-numbers.length/2;
  105. int[] secondHalf = new int[secondHalfLength];
  106. System.arraycopy(numbers,numbers.length/2,secondHalf,0,secondHalfLength);
  107. mergeSort(secondHalf);
  108. merge(firstHalf,secondHalf,numbers);
  109.  
  110.  
  111. }
  112.  
  113. }
  114. static void merge(int[] list1,int[] list2 , int[] temp){
  115. int current1 = 0 ; // list1中的位置
  116. int current2 = 0 ; // list2 中的位置;
  117. int current3 = 0 ; // list3 中的位置
  118. while (current1<list1.length&&current2<list2.length){
  119. if(list1[current1]<list2[current2]){
  120. temp[current3++] = list1[current1++];
  121. }else{
  122. temp[current3++]= list2[current2++];
  123. }
  124. }
  125. while (current1<list1.length){
  126. temp[current3++]=list1[current1++];
  127. }
  128. while (current2<list2.length){
  129. temp[current3++] = list2[current2++];
  130. }
  131.  
  132. }
  133.  
  134. /**
  135. * 快速排序
  136. * 选择一个主元 使得第一部分的元素都小于这个元素,后一部分都大于这个元素
  137. * 再递归地对着2部分进行快速排序
  138. *
  139. * */
  140. static void quickSort(int[] numbers){
  141.  
  142.  
  143. }
  144. static void quickSort(int[] numbers,int first,int last){
  145. if(last>first){
  146. int pivotIndex = partition(numbers,first,last);
  147. quickSort(numbers,first,pivotIndex-1);
  148. quickSort(numbers,pivotIndex+1,last);
  149. }
  150. }
  151. //找到主元应该在的位置,使得数组左边都比主元小,右边都比主元大。
  152. static int partition(int[]numbers,int first,int last){
  153. //第一个元素的值作为中间点
  154. int pivot = numbers[first];
  155. int low = first + 1 ;
  156. int high = last;
  157. while(high > low){
  158. while(low<=high&&numbers[low]<=pivot){
  159. low++;
  160. }
  161. while(low<=high&&numbers[high]>pivot){
  162. high -- ;
  163. }
  164. if(high>low){
  165. swap(numbers,numbers[high],numbers[low]);
  166. }
  167. }
  168.  
  169. while(high>first&&numbers[high]>=pivot){
  170. high -- ;
  171. }
  172. if(pivot>numbers[high]){
  173. numbers[first] =numbers[high];
  174. numbers[high]=pivot;
  175. return high;
  176. }else{
  177. return first;
  178. }
  179.  
  180.  
  181. }
  182.  
  183.  
  184.  
  185. //交换2个值
  186. static void swap( int[] e, int a ,int b){
  187. int temp ;
  188. temp = e[a];
  189. e[a] =e[b];
  190. e[b] = temp;
  191.  
  192. }
  193. //打印数组的值
  194. static void printArrays(int[] n){
  195. for (int i = 0; i <n.length ; i++) {
  196. System.out.print(n[i]+" ");
  197. }
  198. }
  199.  
  200. }
Add Comment
Please, Sign In to add comment