[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] + "]");
}
}
}