Bubble, simple selection, direct insert sort comparison (Java edition)

Source: Internet
Author: User
Tags sorts

Bubbling, simple selection, direct insertion these three sorts are simple sorts.

Tool Class
package Utils;import java.util.Arrays;publicclass SortUtils {    publicstaticvoidswap(int[] a,int i,int j){        int temp = a[i];        a[i] = a[j];        a[j] = temp;    }    publicstaticvoidprintString(int[] a){        System.out.println(Arrays.toString(a));    }}
Bubble Sort

 PackageBubblesort;ImportJava.util.Arrays;ImportUtils.sortutils; Public  class Demo02 {     Public Static void Bubblesort(int[] a) {intlen = a.length;BooleanFlag =true; for(intI=0; i<len-1&& Flag; i++) {flag =false;//Assuming no Exchange             for(intj=0; j< len-1-I.; J + +) {if(a[j+1]&LT;A[J]) {flag =true;//Swapped outSortutils.swap (a,j+1, j); }               }        }       } Public Static void Main(string[] args) {int[] A = {2,3,5,4,1,6,9,8,7};        Bubblesort (a);    Sortutils.printstring (a); }}

Simple Selection Sorting

 PackageSimplesort;ImportJava.util.Arrays;ImportUtils.sortutils; Public  class Demo {     Public Static void Simplesort(int[] arr) {intMin for(inti =0; i < arr.length; i++) {min = i;//Assuming the first data is the smallest data             for(intj = i+1; J < Arr.length; J + +) {if(Arr[min]>arr[j])                {min = j; }            }if(i!=min)            {Sortutils.swap (arr, Min, i); }        }    } Public Static void Main(string[] args) {int[] A = {2,3,5,4,1,6,9,8,7};        Simplesort (a);    Sortutils.printstring (a); }}


Direct Insert Sort

 PackageInsertsort;ImportUtils.sortutils; Public  class Demo {     Public Static void Insertsort(int[] a) {intTemp,j; for(inti =0; I < a.length-1; i++) {if(a[i]>a[i+1]){//After one data is less than the previous datatemp = a[i+1];//Insert buffer                 for(j = i; j>=0&&a[j]>temp; j--) {a[j+1] = A[j];//Move backwards}//Because the judgment is made after the previous minus one, so add aa[j+1] = temp; }        }    } Public Static void Main(string[] args) {int[] A = {2,3,5,4,1,6,9,8,7};        Insertsort (a);    Sortutils.printstring (a); }}



compare three kinds of sorting



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Bubble, simple selection, direct insert sort comparison (Java edition)

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.