sort

1. Bubble sort

Basic idea:

In a set of numbers to be sorted, compare and adjust the two adjacent numbers from top to bottom for all numbers in the range that is not currently sorted, so that the larger number sinks, and the smaller Small rises. That is: whenever two adjacent numbers are compared and found that their ordering is opposite to the ordering requirements, they are exchanged.

import java.util.Scanner;

public class BubbleSort {
    public static void main(String[] args) {
        int len;
        System.out.println( "Please enter the length of the array!" );
        Scanner sc = new Scanner(System.in);
        len = sc.nextInt();
        int[] arr = new int[len];
        for (int i = 0; i < arr.length; i++) {
            System.out.println( "Please enter the "+i+" value" );
            arr[i] = sc.nextInt();        
        }
        show(arr);
        System.out.println( "After sorting: " );
        show(sort(arr));
    }
    // print array 
    public  static  void show( int [] arr) {
        System.out.println( "Output current array:" );
         for ( int i = 0; i < arr.length; i++ ) {
            System.out.println(i+"----"+arr[i]);
        }
    }
    //冒泡排序
    public static int[] sort(int[] arr) {
        int swap;
        for (int i = 0; i < arr.length-1; i++) {
            for (int j = 0; j < arr.length-i-1; j++) {
                if(arr[j]>arr[j+1]){
                    swap = arr[j+1];
                    arr[j+1] = arr[j];
                    arr[j] = swap;
                }
            }
            
        }
        return arr;
    }
    
}
View Code

2. Simple selection sort

Basic idea:

In a set of numbers to be sorted, select the smallest (or largest) number and exchange it with the number in the first then find the smallest (or largest) and the number in the second position among the remaining numbers. Numbers are swapped, and so on, until the n-1th element (the second-to-last number) is compared with the nth element (the last number ).

public class SelectionSort {      
    
    private static int[] paixu(int[] arr) {
        for (int i = 0; i < arr.length-1; i++) {
            for (int j = i; j < arr.length-1; j++) {
                if (arr[i]>arr[j+1]) {
                    swap(arr,i,j+1);
                }
            }
        }
        return arr;
    }
    public static int[] swap(int[] arr,int x,int y) {
        int swap;
        swap = arr[x];
        arr[x] = arr[y];
        arr[y] = swap;
        return arr;
    }
    private static void dayin(int[] arr) {
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i]+" ");
        }
    }
    public static void main(String[] args) {
        int[] arrTest = {4,2,1,6,3,6,0,-5,1,1};
        System.out.println("前:");
        dayin (arrTest);
        System.out.println();
        System.out.println( "After: " );
        dayin(paixu(arrTest));
    }
}
View Code

 3. Insertion sort

public class SelectSort {
    public static void selectSort(int[] arr) {
        for (int i = 1; i < arr.length; i++) {
            for (int j = i;(j>0)&&(arr[j]<arr[j-1]);j--) {
                    swap(arr,j,j-1);
            }
        }
    }
    
    public static void swap(int[] arr,int x,int y) {
        int swap;
        swap = arr[x];
        arr[x] = arr[y];
        arr[y] = swap;
    }
    public static void main(String[] args) {
        int[] a = {4,2,6,1,3,6};
        selectSort(a);
        for(int i=0;i<a.length;i++){
            System.out.println(a[i]);
        }
    }
}
View Code

Analysis: If the goal is to sort a sequence of n elements: the best case is that the sequence is already sorted in ascending order, in which case, n-1 comparison operations are required. The worst is to sort in descending order, and the number of comparisons required at this time is n(n-1)/2;

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324610412&siteId=291194637