Insert Sort, select sort, bubble sort etc common sort algorithm (Java implementation)

Source: Internet
Author: User


package org.webdriver.autotest.study;import java.util.*;p ublic class sort_examp{  Public static void main (String[] args)  {  integer[] num={ 49,38,65,97,76,13,27,49,78,34,12,64,1};  //insertsort (num);   //selectsort (num);   maopaosort (num);      }//Direct Insert Sort  public  static void  Insertsort (Integer[] num)  {  system.out.println ("before sorting:");   for (int i=0;i <num.length;i++)   {   system.out.print (num[i]+ " ");  }   System.out.println ();   //directly into the sort, starting from the second position of the array   for (int i=1;i<num.length;i++)    {   //the element to be inserted, starting with the second element. Num[i] For the previous set of elements    int insertnum=num[i];    int j;   for (j=i-1;j>=0;j--)    {        //will be greater than the element to be inserted, the value is shifted backward by 1 bits, Num[j] is the latter set of elements &NBSp;      if (Num[j]>insertnum)      {num[j+1]=num[j];} else {      break;     }       }   //until Num[j]>insertnum is not present, move position    num[j+1]=insertnum;    system.out.println ();            System.out.println ("+i+", "round Sort");       for  (int k = 0;  k < num.length; k++)        {            system.out.print (num[k]+ " ");     }      }  system.out.println ();         system.out.println ("After sorting:");        for  (int i =  0; i < num.length; i++)          {          system.out.print (num[i]+ " ");          }    }//Select Sort  public static void  selectsort (Integer[] num)  {   system.out.println ("Before sorting:");   for (int i=0;i<num.length;i++)   {    system.out.print (num[i]+ ",");   }  system.out.println ();   // I is the order number of the sorted result   for (int i=0;i<num.length;i++)   {   //assumes a minimum value min    int min=num[i];   //n Store minimum Index (position)    int n=i;    //from the second number, put the minimum value of each comparison to the left    for (int j=i+1;j<num.length;j++)    {     if (num[j]<min)//Find the minimum value     {     //num[j ] min, assign num[j] to Min, and record Num[j] original position saved to n     min=num[j];     n=j;          }    //Place the minimum value with the Compare value exchange position, and place the num[i] in Num[j]     num[n]=num [i];    //places the minimum values in the order of I     num[i]=min;   }     system.out.println ();    int l=i+1;        system.out.println ("+l+", "round Sort");       for  (int k =  0; k < num.length; k++)        {            system.out.print (num[k]+ " ");     }  }  system.out.println ();         System.out.println ("After sorting:");        for  (int i = 0 ;  i < num.length; i++)          {          system.out.print (num[i]+ " ");          }      }//Bubble sort  public static void  maopaosort (Integer[]  num)  {  system.out.println ("before sorting:");  for  (int i = 0; i  < num.length; i++)    {      system.out.print ( num[i]+ " ")   }   //bubble sort   for  (int i = 0; i  < num.length; i++)   {      for (int j =  0; j<num.length-i-1; j++)       {        //here I mainly to each traverse once the largest I number sank to the bottom, there is no need to replace the            if (num[j]>num[j+1])           {                int temp = num[j];                num[j] = num[j+1];                 num[j+1] = temp;           }      }       system.out.println ();    int l=i+1;        system.out.println ("+l+", "round Sort");       for  (int k =  0; k < num.length; k++)        {            system.out.print (num[k]+ " ");     }    &Nbsp;         }   system.out.println ();    system.out.println ("After sorting:");    for  (int i = 0; i  < num.length; i++)      {        System.out.print (num[i]+ " ");     } } }

Insert Sort, select sort, bubble sort etc common sort algorithm (Java implementation)

Related Article

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.