One day a Java foundation--Sort

Source: Internet
Author: User

One day a series of the first, to see if they can persist, refueling!

Bubble sort and quick sort

Bubble sort, the first interview in life, NICE's examiner asked if I can write down

Quick sort, feel tall, divide and conquer thought, must learn

1  PackageOne ;2 3  Public classTest_sort {4 5      Public Static voidMain (string[] args) {6         int[] A = {2, 9, 5, 4, 8, 1 };7         //sort (bubble_sort);8Quick_sort (A, 0, a.length-1);9          for(inti = 0; i < a.length; i++) {TenSystem.out.print (A[i] + ""); One         } A     } -  -     //Bubble Sort the      Public Static int[] Bubble_sort (int[] a) { -         //set up a decision to optimize -         //If the interchange does not occur during a traversal, it is not necessary to proceed because the sort is complete -         BooleanAC =true; +          for(intK = 1; K < A.length & ac; k++) { -AC =false; +              for(inti = 0; i < a.length-1; i++) { A                 if(A[i] > a[i + 1]) { at                     inttemp =A[i]; -A[i] = a[i + 1]; -A[i + 1] =temp; -AC =true; -                 } -             } in         } -         returnA; to     } +  -     //Quick Sort the      Public Static int[] Quick_sort (intS[],intLintr) { *         if(L <r) { $             //Swap (S[l], s[(l + R)/2]);//swap this number and first number in the middle to see note 1Panax Notoginseng             inti = l, j = r, x =S[l]; -              while(I <j) { the                 //find the first number less than x from right to left +                  while(I < J && S[j] >=x) Aj--; the                 if(I <j) +s[i++] =S[j]; -  $                 //find the first number greater than or equal to x from left to right $                  while(I < J && S[i] <x) -i++; -                 if(I <j) thes[j--] =S[i]; -             }WuyiS[i] =x; theQuick_sort (S, l, i-1);//recursive calls from right to left -Quick_sort (S, i + 1, R);//from left to right Wu         } -         returns; About     } $}

One day a Java foundation--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.