Sorting Algorithm (bucket, bubble, fast), Code backup, algorithm bubble

Source: Internet
Author: User

Sorting Algorithm (bucket, bubble, fast), Code backup, algorithm bubble

1/** 2 * @ Description quick sorting, time complexity O {nlgn} 3 * @ date 6:34:49 on January 1, February 21, 2017 4 */5 @ Test 6 public void kuaisu () {7 int [] arr = getData (); 8 quicksort (0, arr. length-1, arr); 9 System. out. println (Arrays. toString (arr); 10} 11 12 public static void quicksort (int left, int right, int [] arr) {13 if (left> right) return; 14 int I, j, temp, n; 15 temp = arr [left]; 16 I = left; 17 j = right; 18 while (I! = J) {19 while (arr [j]> = temp & I <j) 20 j --; 21 while (arr [I] <= temp & I <j) 22 I ++; 23 if (I <j) {24 n = arr [j]; 25 arr [j] = arr [I]; 26 arr [I] = n; 27} 28} 29 arr [left] = arr [I]; 30 arr [I] = temp; 31 quicksort (left, I-1, arr); 32 quicksort (j + 1, right, arr); 33}

 




1/** 2 * @ Description bucket sorting, time complexity O {n} 3 * @ date February 21, 2017 9:58:34 4 * @ action will be 5 integers less than 10, sort from large to small 5 */6 @ Test 7 public void tong () {8 @ SuppressWarnings ("resource") 9 blocks SC = new blocks (System. in); 10 System. out. println ("maximum value to be sorted:"); 11 int numberMax = SC. nextInt (); // The maximum value to be sorted is 12 System. out. println ("number of sorted:"); 13 int number = SC. nextInt (); // number of sorted Items 14 int [] arr = new int [numberMax]; 15 for (int I = 0; I <number; I ++) {16 arr [SC. nextInt ()] + = 1; 17} 18 19 for (int I = 0; I <numberMax; I ++) {20 for (int j = 0; j <arr [I]; j ++) {21 System. out. println (I); 22} 23} 24} 25 26/** 27 * @ Description bubble sort-sort by number size, time complexity O {n square} 28 * @ date February 21, 2017 :53:1329 * @ action maopao30 */31 @ Test32 public void maopao () {33 @ SuppressWarnings ("resource ") 34 then SC = new then (System. in); 35 System. out. println ("number of sorted items:"); 36 int number = SC. nextInt (); // number of sorted 37 int [] arr = new int [number]; 38 for (int I = 0; I <number; I ++) {39 arr [I] = SC. nextInt (); 40} 41 int temp = 0; 42 for (int I = 0; I <number-1; I ++) {43 for (int j = I; j <number-1; j ++) {// or for (int j = I + 1; j <number-1; j ++) {44 if (arr [j]> arr [j + 1]) {45 temp = arr [j]; 46 arr [j] = arr [j + 1]; 47 arr [j + 1] = temp; 48} 49} 50} 51 System. out. println (Arrays. toString (arr); 52}

 

  

  

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.