Basic sort (including insert, select, bubble Sort)

Source: Internet
Author: User

More important three basic sorts where the insertion sort is optimal, the selection sort is centered, the bubble sort is the worst
 PackageCom.zzw. Sorting; Public  class alogrithm {     Public Static void Main(string[] args) {int[] Datas =New int[Ten];//Initialize dataInitData (datas);//Print initial dataPrintdata (datas);//Sort the dataSelectsort (datas);//print sorted dataPrintdata (datas); }/** * Initialize data * * @param datas * *     Public Static void InitData(intDatas[]) { for(inti =0; i < datas.length; i++) {//randomly generates a value of 1-100Datas[i] = (int) (Math.random () * -) +1; }    }/** * Print Data * * @param datas * *     Public Static void Printdata(intDatas[]) { for(inti =0; i < datas.length; i++) {System.out.print (Datas[i] +",");    } System.out.println (); }/** * Insert sort * /     Public Static void Insertsort(intDatas[]) {intj =0;The following table where the second data begins to be inserted        inti =0;//Number of inserts         for(i =1; i < datas.length; i++) {inttemp = Datas[i]; for(j = i-1; J >=0; j--) {if(Datas[j] > Temp) {datas[j +1] = Datas[j]; }Else{ Break; }            }//Judge J==-1, or the first position less than or equal to tempDatas[j +1] = temp; }    }/** * Bubble sort * *     Public Static void Bubblesort(intDatas[]) {intj =0;inti =0; for(j = datas.length-1; J >0; j--) { for(i =0; I < J; i++) {if(Datas[i] > datas[i +1]) {inttemp = Datas[i +1]; Datas[i +1] = Datas[i];                Datas[i] = temp; }            }        }    }/** * Select sort * *     Public Static void Selectsort(intDatas[]) { for(intI=0; i<datas.length-1; i++) {intMinindex=i; for(intj=i+1; j<datas.length;j++) {if(Datas[minindex]>datas[j])                {minindex=j; }            }intTemp=datas[i];            Datas[i]=datas[minindex];        Datas[minindex]=temp; }    }}

Basic sort (including insert, select, bubble 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.