Array sorting algorithm (bubble sort, select sort, insert sort)

Source: Internet
Author: User

package j2se;/** * created by jingqing.zhou on 2015/6/16. *  Array sorting algorithm  */public class insersort {   /**    * bubble sort      */    public static int[] bubblesort () {         int[] arr = {10,5,7,45,11,-6,2};         for (int i=0;i<arr.length-1;i++) {             for (int j=0; j<arr.length-i-1;j++) {                 if (arr[j]<arr[j+1]) {                     int temp;                      temp=arr[j];                     arr[j]=arr[j+1];                     arr[j+1]=temp;                 }            }         }        return  arr;    }    /**     *  Insert Sort       *  @return      */    public static  int[] insertsort () {        int[] arr = { 10,5,7,45,11,-6,2};        for (int i=1;i<arr.length;i++) {             int j=i;             while  (j>0&&j<arr.length) {                 int temp ;                 if (Arr[j]<arr[j-1]) {                     temp =  arr[j-1];                     arr[j-1] = arr[j];                     arr[j]=temp;                 }                 j--;            }         }        return arr;     }    /**     *  Direct Select Sort       *  @return      */    public static  int[] selectsort () {        int[] arr = { 10,5,7,45,11,-6,2};        for (int i=0;i<arr.length-1;i++) {             int min = i ;// First assume that the minimum value is the first             for (int j=min+1;j< arr.length;j++) {                 if (Arr[j]<arr[min]) {&NBSP;&Nbsp;                   min = j;                 }            }             int temp;             temp = arr[i];             arr[i] = arr[min];             arr[min] = temp;        }         return arr;    }    public static  void main (String[] args)  {        int[] a  =insertsort ();   &Nbsp;     for (int i=0;i<a.length;i++) {             system.out.print (a[i] +  ", ");         }        system.out.println ();         int[] b =selectsort ();         for (int i=0;i<b.length;i++) {             system.out.print (b[i] +  ", ");        }         system.out.println ();         int[] c =bubblesort ();         for (int i=0;i< c.length;i++) {            system.out.print (C[i]  +  ", ");         }    }} 


Array sorting algorithm (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.