Bubble sort, select sort, insert sort

Source: Internet
Author: User

 Public classMysort {/*** Insert sort (small to large)     * @param arr* @return     */     Public int[]Insertsort(int[] arr) { for(inti =1; I < arr.length; i++) {intCopynum = arr[I];intTargetindex = i;/** copynumindex>0 See if this copynumindex is out of bounds now.* Copynum<arr[copynumindex-1]) See if the number copied is less than the number in front of itself.* are satisfied, it proves that no location has been found for insertion             */             while(targetindex>0&& copynum<arr[Targetindex-1]) {arr[Targetindex] = arr[Targetindex-1];//Let the previous number replace the body of the Copynumtargetindex--;        } arr[Targetindex] = copynum; }returnArr }/*** Select Sort     * @param arr* @return     */     Public int[]Selectsort(int[] arr) { for(intI=0; I<arr.length-1; i++) {//assumed minimum value            intmin = Arr[i];intMinindex = i; for(intJ=i+1; J<arr.length; J + +) {if(Min > Arr[j])                    {min = arr[j];                Minindex = j; }            }//Skip If the loop has not changed (reduce interchange operation)            if(min = = Arr[i])Continue;Swap(arr, I, minindex); }returnArr }/*** Bubble Sort     * @param arr* @return     */     Public int[]Bubblesort(int[] arr) {//control times         for(intI=0; I<arr.length-1; i++) { for(intj=0; J<arr.length-(I+1); J + +) {if(Arr[j] > Arr[j+1]) {Swap(Arr, J, J+1); }            }        }returnArr }/*** Numeric Exchange     * @param arr     * @param aArray subscript     * @param bArray subscript     */    Private void Swap(int[] arr,intAintb) {inttemp = arr[b];        ARR[B] = Arr[a];            Arr[a] = temp; }/*** Print Data     * @param arr     */     Public void PrintArray(int[] arr) { for(intI:arr) {System. out.Print(i +"\ t"); }    }    }

Bubble sort, select sort, insert 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.