Java bubble sort and selection sort examples

  • 2020-04-01 03:23:52
  • OfStack

The basic concept of BubbleSort is to compare adjacent Numbers one by one, putting decimals first and large Numbers last. That is, in the first step: first compare the first number and the second number, put the decimal before, after the large number. Then compare the second number with the third number, put the decimal before, put the large number after, and so on, until you compare the last two Numbers, put the decimal before, put the large number after. So this is the end of the first run, and we put the largest number at the end. In second trip: from the first log is still (because the number of exchange may be due to the number 2 and 3, no longer makes 1 number less than the number 2) and put before the decimal number after, has been compared to the penultimate number (last position is one of the biggest), the second end, in the penultimate position to get a new maximum number (in fact is the second largest in the whole sequence number). Repeat the process until the sorting is complete.


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, and exchange with R[0]; Select the minimum value from R{1}~R[n-1] for the second time, and exchange with R[1]. ,     Select the minimum value from R[I -1]~R[n-1] for the I th time, and exchange with R[I -1]... , the minimum value is selected from R[n-2]~R[n-1] for the n-1 th time and exchanged with R[n-2]. In total, an ordered sequence from small to large is obtained by n-1 times.


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


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();
 //Selection 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();

 }
}


< img SRC = "border = 0 / / files.jb51.net/file_images/article/201405/20140507093046.jpg? 20144794641 ">


Related articles: