Sorting Algorithm code-Java

Source: Internet
Author: User

I. Java implementation of Bubble Sorting

 

Public void bubblesort (INT [] array) {int Len = array. length; int temp = 0; // number of cycles: array. lengthfor (INT I = 0; I <Len; I ++) {// in each loop, move the maximum number of len-I numbers before the array to the position of array [len-i-1] For (Int J = 1; j <len-I; j ++) {// This sorting algorithm is ascending. If (array [J-1]> array [J]) {temp = array [J-1]; array [J-1] = array [J]; array [J] = temp ;}}}}

Ii. Java implementation of quick sorting

 

Public void quicksort (INT [] array) {quicksort (array, 0, array. length-1);} private void quicksort (INT [] array, int begin, int end) {// If the array is empty or there is only one element, if (begin> = END) return; int middle = partition (array, begin, end); quicksort (array, begin, middle-1); quicksort (array, middle + 1, end);} private int partition (INT [] array, int begin, int end) {// use the value of array [end] as the demarcation point, so that the value on the left of the array is less than or equal to it, and the value on the right is greater than int value = array [end]; // I as the index, its next location is the first location of a large number, int I = begin-1; int temp = 0; For (Int J = begin; j <end; j ++) {If (array [J] <= value) {I ++; temp = array [I]; array [I] = array [J]; array [J] = temp;} I ++; array [end] = array [I]; array [I] = value; return I ;}

 

 

III,

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.