Java -- select sorting and Bubble Sorting

Source: Internet
Author: User

[Html]
Import java. util .*;
/*
Sorts the given array.
{5, 1, 6, 4, 2, 8, 9}
 
 
*/
Class ArrayTest2
{
 
/*
Select sort.
The inner loop ends once, and the highest value appears on the cursor position.
*/
Public static void selectSort (int [] arr)
{
For (int x = 0; x <arr. length-1; x ++)
{
For (int y = x + 1; y <arr. length; y ++)
{
If (arr [x]> arr [y])
{
/*
Int temp = arr [x];
Arr [x] = arr [y];
Arr [y] = temp;
*/
Swap (arr, x, y );
}
}
}
}
/*
Bubble Sorting
*/
 
Public static void bubbleSort (int [] arr)
{
For (int x = 0; x <arr. length-1; x ++)
{
For (int y = 0; y <arr. length-x-1; y ++) //-x: reduce the elements for each comparison,-1: avoid cross-border corner.
{
If (arr [y] <arr [y + 1])
{
/*
Int temp = arr [y];
Arr [y] = arr [y + 1];
Arr [y + 1] = temp;
*/
Swap (arr, y, y + 1 );
}
}
}
}
 
/*
No matter what order is found. All elements that meet the conditions must be replaced by positions.
Therefore, we can extract the same code and encapsulate it into a function.
*/
Public static void swap (int [] arr, int a, int B)
{
Int temp = arr [a];
Arr [a] = arr [B];
Arr [B] = temp;
}
Public static void main (String [] args)
{
Int [] arr = {5, 1, 6, 4, 2, 8, 9 };
// Before sorting;
PrintArray (arr );
 
// Sort
// SelectSort (arr );
// BubbleSort (arr );
 
// Arrays. sort (arr); // a sorting method defined in java. In development, sort the array. Use this sentence code.
// After sorting:
PrintArray (arr );

}
 
Public static void printArray (int [] arr)
{
System. out. print ("[");
For (int x = 0; x <arr. length; x ++)
{
If (x! = Arr. length-1)
System. out. print (arr [x] + ",");
Else
System. out. println (arr [x] + "]");
 
}
}
}

Import java. util .*;
/*
Sorts the given array.
{5, 1, 6, 4, 2, 8, 9}


*/
Class ArrayTest2
{

/*
Select sort.
The inner loop ends once, and the highest value appears on the cursor position.
*/
Public static void selectSort (int [] arr)
{
For (int x = 0; x <arr. length-1; x ++)
{
For (int y = x + 1; y <arr. length; y ++)
{
If (arr [x]> arr [y])
{
/*
Int temp = arr [x];
Arr [x] = arr [y];
Arr [y] = temp;
*/
Swap (arr, x, y );
}
}
}
}
/*
Bubble Sorting
*/

Public static void bubbleSort (int [] arr)
{
For (int x = 0; x <arr. length-1; x ++)
{
For (int y = 0; y <arr. length-x-1; y ++) //-x: reduce the elements for each comparison,-1: avoid cross-border corner.
{
If (arr [y] <arr [y + 1])
{
/*
Int temp = arr [y];
Arr [y] = arr [y + 1];
Arr [y + 1] = temp;
*/
Swap (arr, y, y + 1 );
}
}
}
}

/*
No matter what order is found. All elements that meet the conditions must be replaced by positions.
Therefore, we can extract the same code and encapsulate it into a function.
*/
Public static void swap (int [] arr, int a, int B)
{
Int temp = arr [a];
Arr [a] = arr [B];
Arr [B] = temp;
}
Public static void main (String [] args)
{
Int [] arr = {5, 1, 6, 4, 2, 8, 9 };
// Before sorting;
PrintArray (arr );

// Sort
// SelectSort (arr );
// BubbleSort (arr );

// Arrays. sort (arr); // a sorting method defined in java. In development, sort the array. Use this sentence code.
// After sorting:
PrintArray (arr );

}

Public static void printArray (int [] arr)
{
System. out. print ("[");
For (int x = 0; x <arr. length; x ++)
{
If (x! = Arr. length-1)
System. out. print (arr [x] + ",");
Else
System. out. println (arr [x] + "]");

}
}
}


 

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.