Java Implementation of the classic Sorting Algorithm and java Implementation of the Sorting Algorithm

Source: Internet
Author: User

Java Implementation of the classic Sorting Algorithm and java Implementation of the Sorting Algorithm

1/** 2*3 * @ author yuzhiping 4 * @ version 1.0 5 * function description: typical Computer Algorithms 6*7 */8 public class sortAlgorithm <T extends Comparable <T> {9 10 // exchange index I and index j Values 11 private void swap (T [] data, int I, int j) {12 T tmp; 13 tmp = data [I]; 14 data [I] = data [j]; 15 data [j] = tmp; 16} 17 18 19 // ----- heap sorting time complexity O (nlogn) ----- 20 21 public void heapSort (T [] data) {22 int arrayLength = data. length; 23 // loop parts heap 24 for (int I = 0; I <arrayLength-1; I ++) {25 // build heap 26 builMaxdHeap (data, arrayLength-1-I); 27 // swap heap top and last element 28 swap (data, 0, arrayLength-1-I); 29 30} 31} 32 33 // create a large top heap for the data array from 0 to lastIndex 34 private void builMaxdHeap (T [] data, int lastIndex) {35 // starting from the parent node of the node (last node) at lastIndex 36 for (int I = (lastIndex-1)/2; I> = 0; I --) {37 // k Save the 38 int k = I of the node currently being determined; 39 // if the child node of the current k node has 40 while (k * 2 + 1 <= last Index) {41 // Index of the Left subnode of k node 42 int biggerIndex = 2 * k + 1; 43 // If biggerIndex is smaller than lastIndex, that is, biggerIndex + 1 44 // indicates that the right child node of the k node has 45 if (biggerIndex <lastIndex) {46 // if the value of the right subnode is greater than 47 if (data [biggerIndex]. compareTo (data [biggerIndex + 1]) <0) {48 // biggerIndex always records the index of the large subnode 49 biggerIndex ++; 50} 51} 52 // if the value of k node is smaller than the value of its larger subnode, 53 if (data [k]. compareTo (data [biggerIndex]) <0) {54 // exchange them 55 swap (data, K, biggerIndex); 56 // assign biggerIndex to k to start the next loop of the while loop, 57 // re-ensure that the value of k nodes is greater than the value of its left and right subnodes. 58 k = biggerIndex; 59} else {60 break; 61} 62} 63} 64} 65 66 // ----- time complexity O (n ^ 2) ----- 67 public void bubbleSort (T [] data) {68 int I, j; 69 for (I = 0; I <data. length-1; I ++) {70 for (j = 0; j <data. length-i-1; j ++) {71 if (data [j]. compareTo (data [j + 1])> 0) {72 swap (data, j + 1, j ); 73} 74} 75} 76} 77 78 // ----- select sorting method time complexity O (n ^ 2) ----- 79 public void selectSort (T [] data) {80 int I, j; 81 82 for (I = 0; I <da Ta. length-1; I ++) {83 for (j = I + 1; j <data. length; j ++) {84 if (data [I]. compareTo (data [j])> 0) {85 swap (data, I, j ); 86} 87} 88} 89} 90 91 // ----- the time complexity of the quick sorting method is O (log2n) ----- 92 public void quickSort (T [] data) {93 subQuickSort (data, 0, data. length-1); 94} 95 96 private void subQuickSort (T [] data, int start, int end) {97 if (start <end) {98 // use the first element as the demarcation value 99 T base = data [start]; 100 // I search for an index 101 greater than the demarcation value element from the left Int I = start; 102 // j searches for indexes less than the Boundary Value element from the right side. 103 int j = end + 1; 104 while (true) {105 // The left side skips the element 106 while (I <end & data [++ I]. compareTo (base) <= 0); 107 // The right side skips the element 108 while (j> start & data [-- j]. compareTo (base)> = 0); 109 110 if (j> I) {111 swap (data, I, j); 112} else {113 break; 114} 115} 116 // restore the demarcation value to 117 swap (data, start, j); 118 119 // recursive left sequence 120 subQuickSort (data, start, J-1 ); 121 // recursive Right Sequence 122 subQuickSort (Data, j + 1, end); 123} 124} 125 126 // ----- insertion time complexity of the sort method O (n ^ 2) ----- 127 public void insertSort (T [] data) {128 int arrayLength = data. length; 129 130 for (int I = 1; I <arrayLength; I ++) {131 // ensure that the value of data [I] will not be lost 132 T tmp = data [I] when the overall post migration; 133 // the value at the index has been greater than all the previous values, indicating that it has been ordered, no need to insert 134 // The value before the index at the I-1 has been ordered, the value of the element at the index at the I-1 is also the maximum 135 if (data [I]. compareTo (data [I-1]) <0) {136 int j = I-1; 137 // The whole moves a 138 while (j> = 0 & data [j]. compareTo (tmp)> 0) {139 data [j + 1] = data [j]; 140 j --; 141} 142 data [j + 1] = tmp; 143} 144} 145 146 147 // ----- time complexity of the semi-insertion sorting method ----- 148 public void binaryInsertSort (T [] data) {149 int arrayLength = data. length; 150 151 for (int I = 1; I <arrayLength; I ++) {152 if (data [I-1]. compareTo (data [I])> 0) {153 // The element value at cache I is 154 T tmp = data [I]; 155 156 // The left boundary of the record search range 157 int low = 0; 158 // The right boundary of the record search range 159 int high = I-1; 160 161 w Hile (high> = low) {162 // record intermediate position 163 int mid = (high + low)/2; 164 // compare the data size between the intermediate position and I, to narrow the search range by 165 if (tmp. compareTo (data [mid])> 0) {167 low = mid + 1; 168} else {169 high = mid-1; 170} 171} 172/set low ~ At I, the data is moved backwards by one 173 for (int j = I; j> low; j --) {174 data [j] = data [j-1]; 175} 176 data [low] = tmp; 177 178} 179} 180 181 182 // ----- time complexity O (nlogn) O (n ^ 2) of the hill sorting method) for details, see the value of h ----- 183 public void shellSort (T [] data) {184 int arrayLength = data. length; 185 // h save variable increment 186 187 int h = 1; 188 while (h <= arrayLength/3) {189 h = h * 3 + 1; 190} 191 192 while (h> 0) {193 // System. out. println (Arrays. toString (data) + "h =" + h); 194 195 for (int I = h; I <arrayLength; I ++) {196 // when the overall move is back, ensure that the value of data [I] is not lost 197 T tmp = data [I]; 198 // the value at the I index is already 199 larger than all previous values // (the value prior to the I-1 index is already ordered, and the value of the element at the I-1 index is the maximum value) 200 if (data [I]. compareTo (data [I-h]) <0) {201 int j = I-h; 202 // The entire backend is 203 while (j> = 0 & data [j]. compareTo (tmp)> 0) {204 data [j + h] = data [j]; 205 j-= h; 206} 207 208 // Insert the tmp value to the appropriate position 209 data [j + h] = tmp; 210} 211} 212 h = (h-1)/3; 213} 214 215} 216 217 // ----- merge sort The time complexity is O (nlog2n) ----- 218 public void mergeSort (T [] data) {219 subMergeSort (data, 0, data. length-1); 220} 221 222 private void subMergeSort (T [] data, int left, int right) {223 if (right> left) {224 // locate the intermediate index 225 // System. out. println (Arrays. toString (data); 226 int center = (left + right)/2; 227 // recursion of the left array 228 subMergeSort (data, left, center ); 229 // recursion 230 subMergeSort (data, center + 1, right) for the right array; 231 // merge 232 merg E (data, left, center, right); 233} 234} 235 236 @ SuppressWarnings ("unchecked") 237 private void merge (T [] data, int left, int center, int right () {238 Object [] tmpArr = new Object [data. length]; 239 int mid = center + 1; 240 // index in the middle of the third record 241 int third = left; 242 int tmp = left; 243 244 while (left <= center & mid <= right) {245 // fetch the smallest value from the two arrays and put it into the intermediate array 246 if (data [left]. compareTo (data [mid]) <=0) {247 tmpArr [Third ++] = data [left ++]; 248} else {249 tmpArr [third ++] = data [mid ++]; 250} 251} 252 253 // Add the remaining part to the intermediate array 254 while (mid <= right) {255 tmpArr [third ++] = data [mid ++]; 256} 257 while (left <= center) {258 tmpArr [third ++] = data [left ++]; 259} 260 261 // copy the content of the intermediate array to the original array 262 // (original left ~ The content in the right range is copied back to the original array.) 263 while (tmp <= right) {264 data [tmp] = (T) tmpArr [tmp ++]; 265} 266} 267 268 269 270 public static void main (String [] args) {271 // TODO Auto-generated method stub272 273} 274 275}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.