Common sort algorithm (Java implementation)

Source: Internet
Author: User

Common Sort algorithm Introduction bubble sort
    • :

    • Code:
 Public  class bubblesort {     Public Static void Sort(int[] array) {intTValue; for(inti =0; i < Array.Length; i++) { for(intj = i; J < Array.Length; J + +) {if(Array[i] > Array[j])                    {tValue = Array[i];                    Array[i] = Array[j];                ARRAY[J] = TValue; }            }        }    } Public Static void Main(string[] args) {intA[] = {6,2,4,5,1,9,Ten,7}; Sort (a); for(intV:A) {System.out.print (v +" "); }    }}

Input Result:
1 2 4 5 6 7 9 10

Insert Sort:
    • :

    • Code:

 Public  class insertsort {     Public Static void Sort(int[] array) {intTValue;intJ for(inti =0; i < Array.Length;            i++) {j = i; TValue = Array[i]; while(J >0&& TValue < Array[j-1]) {Array[j] = array[j-1];            j--;        } Array[j] = TValue; }    } Public Static void Main(string[] args) {intA[] = {6,2,4,5,1,9,Ten,7}; Sort (a); for(intV:A) {System.out.print (v +" "); }    }}

Input Result:
1 2 4 5 6 7 9 10

Select Sort:
    • :

    • Code:

 Public  class selectsort {     Public Static void Sort(int[] array) {intindex, TValue; for(inti =0; i < Array.Length; i++) {index = i; for(intj = i +1; J < Array.Length; J + +) {if(Array[j] < Array[index])                {index = j; }            }if(Index! = i)                {tValue = Array[i];                Array[i] = Array[index];            Array[index] = TValue; }        }    } Public Static void Main(string[] args) {intA[] = {6,2,4,5,1,9,Ten,7}; Sort (a); for(intV:A) {System.out.print (v +" "); }    }}

Input Result:
1 2 4 5 6 7 9 10

Quick sort:
    • :

    • Code:

 Public  class QuickSort {     Public Static void QuickSort(int[] Array,intLeftintright) {intI, J, BValue, TValue;if(Left > right) {return;        } i = left;        j = right; BValue = Array[left]; while(I! = j) { while(Array[j] >= bValue && i < J)            {j--; } while(Array[i] <= bValue && i < J)            {i++; }if(I < J)                {tValue = Array[i];                Array[i] = Array[j];            ARRAY[J] = TValue;        }} Array[left] = Array[i];        Array[i] = BValue; QuickSort (array, left, I-1); QuickSort (array, i +1, right); } Public Static void Main(string[] args) {intA[] = {6,2,4,5,1,9,Ten,7}; QuickSort (A,0, A.length-1); for(intV:A) {System.out.print (v +" "); }    }}

Input Result:
1 2 4 5 6 7 9 10

Resources:

http://blog.jobbole.com/11745/

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Common sort algorithm (Java implementation)

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.