Java Select sort and bubble sort

Source: Internet
Author: User

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

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.