Select Bubble Sorting Algorithm

Source: Internet
Author: User

Sort by bubble

 

Code

 Package com. Qixin. Five;

Public class bubblesort {
Public static void main (string [] ARGs ){
Int [] array = new int };
Int temp;
System. Out. println ("original array content :");
Printarray (array );

For (INT I = 1; I <array. length; I ++ ){
For (Int J = 0; j <array. Length-I; j ++ ){
If (array [J]> array [J + 1]) {
Temp = array [J];
Array [J] = array [J + 1];
Array [J + 1] = temp;
}
}
}
System. Out. println ("result after ascending order :");
Printarray (array );

For (INT I = 1; I <array. length; I ++ ){
For (Int J = 0; j <array. Length-I; j ++ ){
If (array [J] <array [J + 1]) {
Temp = array [J];
Array [J] = array [J + 1];
Array [J + 1] = temp;
}
}
}
System. Out. println ("result after sorting from big to small :");
Printarray (array );
}

Public static void printarray (INT array []) {
For (int I: array ){
System. Out. Print (I + "");
}
System. Out. println ("\ n ");
}
}

 

 

 

Sort by selection

 

Code

 Package com. Qixin. Five;

Public class selectsort {
Public static void main (string [] ARGs ){
Int [] array = new int };
Int temp;
System. Out. println ("original array content :");
Printarray (array );
Int index;
For (INT I = 1; I <array. length; I ++ ){
Index = 0;
For (Int J = 1; j <= array. Length-I; j ++ ){
If (array [J]> array [Index]) {
Index = J;
}
}
Temp = array [array. Length-I];
Array [array. Length-I] = array [Index];
Array [Index] = temp;
}
System. Out. println ("arrange the array content in the forward order :");
Printarray (array );

For (INT I = 1; I <array. length; I ++ ){
Index = 0;
For (Int J = 1; j <= array. Length-I; j ++ ){
If (array [J] <array [Index]) {
Index = J;
}
}
Temp = array [array. Length-I];
Array [array. Length-I] = array [Index];
Array [Index] = temp;
}
System. Out. println ("Inverted array content :");
Printarray (array );
}

Public static void printarray (INT [] array ){
For (int I: array ){
System. Out. Print (I + "");
}
}

}

 

 

 

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.