Java implementation of several sorts

Source: Internet
Author: User

First, bubble sort

Bubble sort is a simple sort algorithm. It repeats the number of columns to be sorted, compares two elements at a time, and swaps them if they are in the wrong order.

Second, quick sort

Divide a sequence into two sub-sequences using a divide-and-conquer strategy.

There are also selection, insertion, and merge sorts. The code is as follows:
code highlighting produced by Actipro Codehighlighter (freeware) http://Www.CodeHighlighter.com/-->package Test.sort; ImportJava.util.Random; //sorting classes for Java implementations Public classNumbersort {//Private constructor method, prohibit instantiation    PrivateNumbersort () {Super(); }        //Bubble Method Sort     Public Static voidBubblesort (int[] numbers) {           intTemp//Record interim intermediate values        intsize = Numbers.length;//Array Size         for(inti = 0; i < size-1; i++) {                for(intj = i + 1; J < size; J + +) {                   if(Numbers[i] < numbers[j]) {//Exchange two number of positionstemp =Numbers[i]; Numbers[i]=Numbers[j]; NUMBERS[J]=temp; }               }           }       }       //Quick Sort     Public Static voidQuickSort (int[] numbers,intStartintend) {           if(Start <end) {               intbase = Numbers[start];//Selected Datum value (first value as Datum)            intTemp//Record interim intermediate values            inti = start, j =end;  Do {                    while((Numbers[i] < base) && (I <end)) I++;  while((Numbers[j] > Base) && (J >start)) J--; if(I <=j) {Temp=Numbers[i]; Numbers[i]=Numbers[j]; NUMBERS[J]=temp; I++; J--; }               }  while(I <=j); if(Start <j) QuickSort (Numbers, start, j); if(End >i) QuickSort (numbers, I, end); }       }       //Select Sort     Public Static voidSelectsort (int[] numbers) {           intSize =Numbers.length, temp;  for(inti = 0; i < size; i++) {               intK =i;  for(intj = size-1; J > i; j--) {                   if(Numbers[j] <Numbers[k]) k=J; } temp=Numbers[i]; Numbers[i]=Numbers[k]; NUMBERS[K]=temp; }       }       //Insert Sort//@param numbers     Public Static voidInsertsort (int[] numbers) {           intSize =Numbers.length, temp, J;  for(inti = 1; i < size; i++) {Temp=Numbers[i];  for(j = i; j > 0 && Temp < numbers[j-1]; j--) Numbers[j]= Numbers[j-1]; NUMBERS[J]=temp; }       }       //Merge Sort     Public Static voidMergeSort (int[] numbers,intLeftintRight ) {           intt = 1;//number of elements per group        intSize = Right-left + 1;  while(T <size) {               ints = t;//number of elements per group in this cyclet = 2 *s; inti =Left ;  while(i + (t-1) <size) {Merge (numbers, I, I+ (s-1), i + (t-1)); I+=T; }               if(i + (s-1) <Right ) Merge (numbers, I, I+ (s-1), right); }       }        //Merging algorithm Implementation    Private Static voidMergeint[] Data,intPintQintr) {int[] B =New int[Data.length]; ints =p; intt = q + 1; intK =p;  while(s <= q && t <=r) {if(Data[s] <=Data[t]) {B[k]=Data[s]; S++; } Else{B[k]=Data[t]; T++; } k++; }           if(s = = q + 1) B[k+ +] = data[t++]; Elseb[k+ +] = data[s++];  for(inti = P; I <= R; i++) Data[i]=B[i]; }     }

Java implementation of several sorts

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.