Java Select Sort, insert sort, quick sort

Source: Internet
Author: User

 Public classTest { Public Static voidMain (string[] args) {intA[] = {1, 2, 3, 4, 5 }; //Select Sort (a); //Insert Sort (a);Quicksort (A, 0, a.length-1); System.out.print ("After sorting:");  for(intn:a) {System.out.print (n+ " "); }    }    Static voidSelect Sort (int[] a) {intpos = 0;  for(inti = 0; i < a.length-1; i++) {pos=i; //maximum value after positioning to I             for(intj = i; J < A.length; J + +) {                if(A[pos] <A[j]) {POS=J; }            }            //Max value and I value Exchange            intt =A[pos]; A[pos]=A[i]; A[i]=T; }    }    Static voidInsert Sort (int[] arr) {         for(inti = 1; i < arr.length; i++) {System.out.print (i+ ":"); //Assuming (0-> (j-1)) is orderly, see where J Platoon             for(intj = i; J > 0; j--) {                if(Arr[j] > Arr[j-1]) {                    inttemp =Arr[j]; ARR[J]= Arr[j-1]; Arr[j-1] =temp; } Else {                    //Assuming that the front is ordered, you only need to swap once.                     Break; }            }             for(intN:arr) {System.out.print (n+ " ");        } System.out.println (); }    }    Static voidQuicksortintArr[],intLeftintRight ) {        if(Left <Right ) {            //Locate the left            intKey =Arr[left]; inti =Left ; intj =Right ;  while(I <j) { while(I < J && Arr[j] <key) {                    //high standard left move ←j--; }                if(Arr[i]! =Arr[j]) {                    //High: Small to the left to pullArr[i] =Arr[j]; }                 while(I < J && Arr[i] >key) {                    //low mark right shift →i++; }                if(Arr[i]! =Arr[j]) {                    //Low: Large to the right pullARR[J] =Arr[i]; }} Arr[i]=key; Quicksort (arr, left, I-1); Quicksort (arr, I+ 1, right); }    }}

Java Select Sort, insert sort, quick 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.