Java basic interview question 3 (Sorting Algorithm)

Source: Internet
Author: User

1. Bubble Sorting: Same as fast sorting, it is a type of exchange sorting.

public class Sort{    public void generatArray(int [] array){        Random random = new Random();        for (int i=0;iarray[j+1]){                    tmp=array[j];                    array[j]=array[j+1];                    array[j+1]=tmp;                }            }        }        for (int a : array) {            System.out.print(a+",");        }        System.out.println();    }        public static void main(String args[]){        Sort sort = new Sort();        int array[]=new int[10];        sort.generatArray(array);        sort.bubbleSort(array);    }}


2. Fast sorting: Improves the Bubble sorting.
Public class Sort {public void generatArray (int [] array) {Random random = new Random (); for (int I = 0; I = r) return; int k = array [l]; int I = l, j = r; while (I
 
  

3. Insert sorting:

public class Sort{    public void insertSort(int [] array){        for(int i=1;i0 && array[j-1]>tmp;j--)                array[j]=array[j-1];            array[j]=tmp;        }                for (int a : array) {            System.out.print(a+",");        }        System.out.println();    }        public static void main(String args[]){        Sort sort = new Sort();        int array[]=new int[10];        sort.generatArray(array);        sort.insertSort(array);    }}



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.