Java bubble Sort and select Sort Sample _java

Source: Internet
Author: User

The basic concept of bubble sort (bubblesort) is to compare the number of adjacent two digits, place the decimal number in the front, and put the large numbers behind. On the first trip: first compare the 1th and 2nd numbers, put the decimal number before the big number put. Then compare the 2nd and 3rd numbers, put the decimal places before the large number, so continue until the last two, the number of decimal places before the large number of put. At the end of the first trip, the largest number was put to the end. On the second trip: the comparison is still starting from the first logarithm (since the exchange of the 2nd and 3rd numbers is possible, so that the 1th number is no longer less than the 2nd number, the decimal place before, after the large number, has been compared to the penultimate number (the first in the last position is already the largest), the second end, To get a new maximum number (in fact, the second largest number in the whole series) in the penultimate position. So go on, repeat the process until you finish sorting.

Copy Code code as follows:

public class Paixu {
public static void Main (string[] args) {
int [] a = {2,6,4,5,1,7,3};
int i = 0;
int j = 0;
int n = 0;
for (i= 0;i<a.length-1;i++) {
for (j=0;j<a.length-i-1;j++) {
if (A[j]>a[j+1]) {
n = a[j];
A[J] = a[j+1];
A[j+1] = n;
}
}
}
for (i = 0; i < a.length; i++) {
System.out.println (A[i]);
}
}
}

Direct Selection Ordering (Straight Select sorting) is also a simple sorting method, its basic idea is: the first time from r[0]~r[n-1] to select the minimum value, and r[0] Exchange, the second from the r{1}~r[n-1 to select the minimum value, and r[1] exchange, ...., I choose the minimum value from r[i-1]~r[n-1], exchange with r[i-1], ..., the first n-1 from r[n-2]~r[n-1, select the minimum, and r[n-2] exchange, in total through the n-1, get a sorted code from small to large order sequence.

Copy Code code as follows:

public class Paixu {
public static void Main (string[] args) {
int [] a = {2,6,4,5,1,7,3};
int i = 0;
int j = 0;
int n = 0;
for (i= 0;i<a.length;i++) {
for (j=i+1;j<a.length;j++) {
if (A[i]>a[j]) {
n = a[i];
A[J] = A[i];
A[i] = n;
}
}
}
for (i = 0; i < a.length; i++) {
System.out.println (A[i]);
}
}
}

Example 2

Copy Code code as follows:

Package Cn.cqu.coce.xutao;

public class Selectsort {
public static void Main (String args[]) {

int a[]={34,56,3,234,767,89,0,324,1,32,54,89,8};
int b[]=new Int[a.length];
System.arraycopy (A, 0, b, 0, a.length);

for (int i=0;i<a.length;i++) {
System.out.print (a[i]+ "T");
}
System.out.println ();
Select sort
for (int i=0;i<a.length-1;i++) {
int min=i;
for (int j=i+1;j<a.length;j++) {
if (A[min]>a[j])
Min=j;
}
if (min!=i) {
int temp=a[min];
A[min]=a[i];
A[i]=temp;
}
}
for (int i=0;i<a.length;i++)
System.out.print (a[i]+ "T");
System.out.println ();
Bubble sort
for (int i=0;i<b.length;i++) {
for (int j=1;j<b.length-i;j++) {
if (B[j-1]>b[j]) {
int TE=B[J];
B[J]=B[J-1];
B[j-1]=te;
}
}
}
for (int i=0;i<b.length;i++)
System.out.print (b[i]+ "T");
System.out.println ();

}
}



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.