集中常用排序算法代码

2021-06-27 17:04

阅读:442

标签:continue   ted   计算   数据   tor   system   sum   color   new   

  1 public class CheckSum {
  2 
  3     /**
  4      * 计算数组中数据之后为sum的两个数字
  5      * @param array
  6      * @param sum
  7      */
  8     public void findTheSum(int array[], int sum) {
  9 
 10         //Arrays.sort(array);
 11 
 12         int[] copy = new int[array.length];
 13 
 14         MergeOrder(array, copy, 0, array.length - 1);
 15 //        fastOrder(array, 0, array.length - 1);
 16         System.out.println(array);
 17         int row = 0, high = array.length - 1;
 18         while (row  high) {
 19 
 20             if (array[row] + array[high] > sum) {
 21                 high--;
 22             } else if (array[row] + array[high]  sum) {
 23                 row++;
 24             } else {
 25                 System.out.println(array[row] + " " + array[high]);
 26                 if (high - 1 >= 0 && array[high] == array[high - 1]) {
 27                     high--;
 28                     continue;
 29                 }
 30 
 31                 if (high - 1 >= 0 && array[row] == array[row + 1]) {
 32                     row++;
 33                     continue;
 34                 }
 35 
 36                 row++;
 37                 high--;
 38             }
 39 
 40         }
 41     }
 42 
 43 
 44 
 45     //小堆排序
 46 
 47     public void duiSortedOrder(int array[], int index,int len) {
 48 
 49         int tempt = array[index];
 50         for (int i = 2 * index + 1; i ) {
 51             if(iarray[i+1]) i++;
 52             if(temptbreak;
 53             array[index] = array[i];
 54             index = i;
 55         }
 56         array[index] = tempt;
 57     }
 58 
 59 
 60     //直接插入法
 61     public void InsertOrder(int array[]) {
 62 
 63         for (int i = 1; i ) {
 64             int tempt = array[i], j = i - 1;
 65             for (; j >= 0 && tempt ) {
 66                 array[j + 1] = array[j];
 67             }
 68             array[j + 1] = tempt;
 69         }
 70     }
 71 
 72 
 73 
 74 
 75     /**
 76      * 归并排序
 77      * @param array
 78      * @param copy
 79      * @param low
 80      * @param high
 81      */
 82     public void MergeOrder(int[] array, int[] copy, int low, int high) {
 83 
 84         if (low == high) return;
 85 
 86         int mid = (low + high) >> 1;
 87 
 88         MergeOrder(array, copy, low, mid);
 89 
 90         MergeOrder(array, copy, mid+1, high);
 91 
 92         //把元素聚合在一起
 93         int left = low;
 94         int right = mid+1;
 95         int cindex = low;
 96         while (lefthigh) {
 97 
 98             if (array[left]  array[right]) {
 99                 copy[cindex++] = array[left++];
100             } else {
101                 copy[cindex++] = array[right++];
102             }
103         }
104 
105         for (; left  mid; ) {
106             copy[cindex++] = array[left++];
107         }
108 
109         for (; right high; ) {
110             copy[cindex++] = array[right++];
111         }
112 
113 
114         //把排序之后的元素放置到 array中
115 
116         for (int i = low; i ) {
117             array[i] = copy[i];
118         }
119         return;
120     }
121 
122 
123     /**
124      * 快速排序
125      *
126      * @param array
127      * @param row
128      * @param high
129      */
130     public void fastOrder(int[] array, int row, int high) {
131 
132         if (row  high) {
133             int mid = Order(array, row, high);
134             fastOrder(array, row, mid - 1);
135             fastOrder(array, mid + 1, high);
136         }
137 
138     }
139 
140 
141     public int Order(int[] array, int row, int high) {
142 
143         int tempt = array[row];
144 
145         while (row  high) {
146 
147             while (tempt  row) {
148 
149                 high--;
150             }
151 
152             array[row] = array[high];
153 
154             while (tempt > array[row] && row  high) {
155                 row++;
156             }
157             array[high] = array[row];
158         }
159 
160         array[row] = tempt;
161         return row;
162 
163     }
164 
165 
166 
167      public static void main(String... args) {
168         CheckSum checkSum = new CheckSum();
169 
170         int[] params = new int[]{8, 7, 6, 5, 4, 3, 2};
171         for (int i = params.length / 2 - 1; i >= 0; i--) {
172             checkSum.duiSortedOrder(params, i,params.length-1);
173         }
174 
175         //输出排序后的数据
176 
177         for (int i = 0; i ) {
178             System.out.println(params[0]);
179             params[0] = params[params.length - 1 - i];
180             checkSum.duiSortedOrder(params, 0, params.length - 1 - i);
181         }
182 
183 
184         checkSum.InsertOrder(params);
185         System.out.println(params);
186 
187     }
188 }

 

集中常用排序算法代码

标签:continue   ted   计算   数据   tor   system   sum   color   new   

原文地址:https://www.cnblogs.com/09120912zhang/p/9651359.html


评论


亲,登录后才可以留言!