The basic concept of bubble sort (BubbleSort) is to compare two adjacent numbers in sequence, putting the decimal in the front and the large number in the back. That is, in the first pass: first compare the first and second numbers, put the decimal first and the large number last. Then compare the second number and the third number, put the decimal in front and the large number in the back, and continue like this until comparing the last two numbers, put the decimal in front and the large number in the back. This is the end of the first trip, leaving the largest number at the end. In the second pass: still start the comparison from the first pair of numbers (because it may be due to the exchange of the second number and the third number that the first number is no longer smaller than the second number), put the decimal first, and the large number After placing it, the comparison is continued until the second-to-last number (the first-to-last position is already the largest). At the end of the second pass, a new maximum number is obtained at the second-to-last position (in fact, it is the second-to-last number in the entire sequence). The second largest number). Continue like this and repeat the above process until the sorting is finally completed.
Copy the 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]);
}
}
}
Straight Select Sorting is also a simple sorting method. Its basic idea is: select the minimum value from R[0]~R[n-1] for the first time, exchange it with R[0], and then Select the minimum value from R{1}~R[n-1] twice and exchange it with R[1]...., The i-th time selects the minimum value from R[i-1]~R[n-1] and exchanges it with R[i-1],..., the n-1th time selects the minimum value from R[n-2]~ Select the minimum value from R[n-1], exchange it with R[n-2], and pass it a total of n-1 times to obtain an ordered sequence arranged from small to large according to the sorting code.
Copy the 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 the 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();
}
}