public class choose{
public static void Main (string[] args) {
Int[] arr = {4,6,7,3,1,6};
Select a = new select ();
A.sort1 (arr);
System.out.println ("After selection:");
for (int i=0;i<arr.length;i++) {
System.out.print (Arr[i]);
}
A.sort2 (arr);
System.out.println ("");
System.out.println ("After bubble sort:");
for (int j=0;j<arr.length;j++) {
System.out.print (Arr[j]);
}
}
}
/**
* Choose the basic idea of sorting: during each cycle, a minimum value is selected and the pointer moves down. The first step: Select a
The small value is placed in the position of the array 0, the second selection//of the small number of places in the array is labeled as 1
*/
Class select{
public static void Sort1 (int arr[]) {
for (int i=0;i<arr.length-1;i++) {
for (int j=i+1;j<arr.length;j++) {
if (Arr[i]>arr[j]) {
Swap location
int temp = Arr[i];
Arr[i] = Arr[j];
ARR[J] = temp;
}
}
}
return arr;
}
/**
* Bubble sort basic idea: in the first cycle, the previous and second numbers are compared, the small forward, the end of a loop
After that, the maximum number is determined on the last side.
*/
public static void Sort2 (int arr[]) {
for (int i=0;i<arr.length;i++) {
for (int j=i;j<arr.length-i-1;j++) {
if (Arr[j]>arr[j+1]) {
int temp = Arr[j];
ARR[J] = arr[j+1];
ARR[J+1] = temp;
}
}
}
}
}
Of course, Java can also be quick to sort, but it is recommended that you do not write fast sorting, because Java provides a lot of class library, the direct call on the line to save a lot of writing code necessary. For the sort method, call the sort method directly.
In the operation of the array, to pay attention to the problem has a corner of the leapfrog problem, in the write-sorting method, you can directly return the array, when the method return value type to set the number of group type. The following is an example of an array return value type to write a simple code.
Class a{
public static int[] Sort1 (int arr[]) {
for (int i=0;i<arr.length-1;i++) {
for (int j=i+1;j<arr.length;j++) {
if (Arr[i]>arr[j]) {
Swap location
int temp = Arr[i];
Arr[i] = Arr[j];
ARR[J] = temp;
}
}
}
return arr;
}
}
public class back{
public static void Main (string[] arg) {
A = new A ();
Int[] Arr ={4,6,1,3,9};
System.out.println (A.sort1 (arr));
}
}
Java Select sort and bubble sort