Java Bubble sorting and selection sorting, java Bubble Sorting Selection

Source: Internet
Author: User

Java Bubble sorting and selection sorting, java Bubble Sorting Selection

// Bubble sort

Package test;


Public class Maopaosort {
Public static void method (){
Int [] arr = new int [10];
For (int I = 0; I <arr. length; I ++ ){
Arr [I] = (int) (Math. random () * 100 + 1 );
}
System. out. println ();
For (int num: arr ){
System. out. print (num + "");
}
System. out. println ();
For (int I = 0; I <arr. length-1; I ++ ){
For (int j = 0; j <arr. length-1-i; j ++ ){
Int tem;
If (arr [j] <arr [j + 1]) {
Tem = arr [j];
Arr [j] = arr [j + 1];
Arr [j + 1] = tem;
}
}
System. out. println ("this is the" + (I + 1) + "secondary sorting result :");
For (int num: arr ){
System. out. print (num + "");
}
System. out. println ();
}
System. out. println ("the final sorting result is :");
For (int num: arr ){
System. out. print (num + "");
}
}
Public static void main (String [] args ){
Method ();
}
}

// Select sorting

Package SelectionSort;


Public class SelectionSort {
Public static void selectionsort (){
Int tem;
Int [] arr = new int [10];
For (int I = 0; I <arr. length; I ++ ){
Arr [I] = (int) (Math. random () * 100 + 1 );
}
For (int num: arr ){
System. out. print (num + "");
}
System. out. println ();
System. out. println ("-------- gorgeous split line --------");
For (int I = 0; I <arr. length-1; I ++ ){
Int k = 0;
For (int j = 1; j <arr. length-I; j ++ ){
If (arr [k]> arr [j]) {
K = j;
}
}
System. out. println ("nth" + (I + 1) + "Times," + "the minimum number of" + (k + 1) + "in the array arr, is "+ arr [k]);
Tem = arr [arr. length-i-1];
Arr [arr. length-i-1] = arr [k];
Arr [k] = tem;
System. out. println ("no." + (I + 1) + "the sorting result is :");
For (int num: arr ){
System. out. print (num + "");
}
System. out. println ();
}
System. out. println ("the final result of sorting is ");
For (int num: arr ){
System. out. print (num + "");
}
}
Public static void main (String [] args ){
Selectionsort ();
}
}

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.