Sorting algorithm first (simple bucket, select sort, bubble sort, quick sort)

Source: Internet
Author: User

Simple bucket sorting

1 /**      2  * @author: Small white with siege lion3 * @creationTime: November 24, 2017 PM 10:37:594 * @description: Bucket sorting (this is not a real bucket, the real barrel parallelism is slightly more complicated.) But the idea of a bucket, call it a simple bucket.5 * @questionDesc: There are 6 students in a class, the test results are as follows Arr Array (out of 10), how to quickly put student scores from small to large arrangement? 6  */7  Public classBucketsortdemo {8      Public voidBucketsort (int[] arr) {9         int[] Array =New int[11];Ten          for(inti = 0; i < arr.length; i++) { Onearray[arr[i]]++; A         } -System.out.print ("Simple barrel output:"); -          for(inti = 0; i < Array.Length; i++) { the              for(intj=0; j<array[i]; J + +){ -System.out.print (i + "")); -             } -         } +     } -      Public Static voidMain (string[] args) { +         int[] arr = {5,3,9,5,2,8}; A         NewBucketsortdemo (). Bucketsort (arr);; at     } -}

Select sort

1 Importjava.util.Arrays;2 /**      3  * @author: Small white with siege lion4 * @creationTime: November 24, 2017 PM 10:39:065 * @description: Select sort (This is done in ascending order, descending simply > to <)6  */7  Public classSelectionsortdemo {8      Public voidSelectionsort (int[] arr) {9         inttemp;Ten         intn =arr.length; One          for(inti=0; i<n-1; i++){ A              for(intj=i+1; j<n; J + +){ -                 if(Arr[i] >Arr[j]) { -temp =Arr[i]; theArr[i] =Arr[j]; -ARR[J] =temp; -                 } -             } +         } -System.out.println ("Select Sort output:" +arrays.tostring (arr)); +     } A      Public Static voidMain (string[] args) { at         int[] arr = {6,2,1,7,9,3,4,5,0,8}; -         NewSelectionsortdemo (). Selectionsort (arr); -     } -}

Bubble sort

1 /**      2  * @author: Small white with siege lion3 * @creationTime: November 24, 2017 PM 10:38:404 * @description: Bubble sort (This is done in ascending order, descending just > to <)5  */6  Public classBubblesortdemo {7      Public voidBubblesort (int[] arr) {8         inttemp;9         intn =arr.length;Ten          for(inti=0; i<n-1; i++){ One              for(intj=0; j<n-1-i; J + +){ A                 if(Arr[j] > arr[j+1]){ -temp =Arr[j]; -ARR[J] = arr[j+1]; theARR[J+1] =temp; -                 } -             } -         } +System.out.println ("Bubbling sort output:" +arrays.tostring (arr)); -     } +      Public Static voidMain (string[] args) { A         int[] arr = {6,2,1,7,9,3,4,5,0,8}; at         NewBubblesortdemo (). Bubblesort (arr); -     } -}

Quick Sort

1 Importjava.util.Arrays;2 /**      3  * @author: Small white with siege lion4 * @creationTime: November 24, 2017 PM 10:32:475 * @description: Quick sort: Divide the sorted data into separate two parts by a single trip, and one part of all the data is smaller than the other part of the data,6 * And then the two parts of the data are quickly sorted by this method, the whole sorting process can be recursive, in order to achieve the entire data into an ordered sequence. 7 * Popular Speaking: It is in the array arbitrarily select a target number (generally choose arr[0] better understanding), and then take two variables I and J respectively to the array index 0 position8 * and length-1 position, then will be less than the target number of all to the left, greater than the target number is placed to the right, after the operation has found the target number of9 * position, and then place the target number at that location. At this point the original array will be divided into two arrays of the target number, and then recursively follow the above steps, the end will be able to the originalTen * The entire array becomes an ordered sequence.  One  */ A  Public classQuicksortdemo { -      Public voidQuickSort (int[] arr,intLeftintRight ) { -         if(left>=Right ) { the             return; -         } -         inti =Left ; -         intj =Right ; +         inttarget =Arr[left]; -          while(i<j) { +              while(Arr[j] >= target && j>i) { Aj--; at             } -              while(Arr[i] <= target && i<j) { -i++; -             } -             //In general, this is the branch, exchange two number of positions, will be greater than the base number of the back, less than the base number of the front -             if(I! =j) { in                 inttemp; -temp =Arr[j]; toARR[J] =Arr[i]; +Arr[i] =temp; -             } the         } *         //the condition for jumping out of the outer loop is that I is exactly equal to J, and that the number of targets and the current IJ position (representing the same position) need to be exchanged $         if(i = =j) {Panax NotoginsengArr[left] =Arr[i]; -Arr[i] =Target; the         } +QuickSort (arr, left, i-1); AQuickSort (arr, i+1, right); the     } +      Public Static voidMain (string[] args) { -         int[] arr = {6,2,1,7,9,3,4,5,0,8}; $Quicksortdemo Quicksortdemo =NewQuicksortdemo (); $Quicksortdemo.quicksort (arr, 0, Arr.length-1); -SYSTEM.OUT.PRINTLN ("Fast output Result:" +arrays.tostring (arr)); -     } the}

Sorting algorithm first (simple bucket, select sort, bubble sort, quick sort)

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.