【Bubble Sort】and【Select Sort】

Bubble Sort

package com.itheima;


public class T {
    
    
    public static void main(String[] args) {
    
    
        int Array[] = {
    
    4,5,6,3,2,1};
        sort(Array);
        for(int i:Array){
    
    
            System.out.print(i + " ");
        }


    }
    public static void sort(int []Array){
    
    
        for(int i=Array.length-1;i>0;i--){
    
     // 遍历n-1次
            for(int j=0;j<i;j++){
    
    
                if(Array[j] > Array[j+1]){
    
    
                    int temp = Array[j];
                    Array[j] = Array[j+1];
                    Array[j+1] = temp;
                }
            }
        }
    }

}


package com.itheima;
import java.util.*;
public class Main {
    
    
    public static void main(String[] args) {
    
    
        Integer nums[] = {
    
    4,5,6,3,2,1};
        Bubble bubble = new Bubble();
        bubble.sort(nums);
        System.out.println(Arrays.toString(nums));
    }
}
class Bubble{
    
    
    public void sort(Comparable[]a){
    
    
        for(int i=a.length-1;i>0;i--){
    
    
            for(int j=0;j<i;j++){
    
    
                if(greater(a[j],a[j+1])){
    
    
                    exch(a,j,j+1);
                }
            }
        }

    }
    private boolean greater(Comparable v,Comparable w){
    
    
        return v.compareTo(w) > 0;
    }
    private void exch(Comparable []a,int i,int j){
    
    
        Comparable temp;
        temp = a[i];
        a[i] = a[j];
        a[j] = temp;
    }

}

Time complexity O(n 2 )

package com.itheima;


public class T {
    
    
    public static void main(String[] args) {
    
    
        int Array[] = {
    
    4,5,6,3,2,1};
        sort(Array);
        for(int i:Array){
    
    
            System.out.print(i + " ");
        }


    }
    public static void sort(int []Array){
    
    
        for(int i=0;i<Array.length-2;i++){
    
    
            int minIndex = i;
            for(int j=i+1;j<Array.length;j++){
    
    
                if(Array[minIndex] > Array[j]){
    
    
                    minIndex = j;
                }
            }
            int temp = Array[minIndex];
            Array[minIndex] = Array[i];
            Array[i] = temp;
        }
    }

}


package com.itheima;
import java.util.*;
public class Main {
    
    
    public static void main(String[] args) {
    
    
        Integer nums[] = {
    
    4,6,8,7,9,2,10,1};
        Selection selection = new Selection();
        selection.sort(nums);
        System.out.println(Arrays.toString(nums));
    }
}
class Selection{
    
    
    public void sort(Comparable[]a){
    
    
        for(int i=0;i<a.length-1;i++){
    
    
            int minIndex = i;
            for(int j=i+1;j<a.length;j++){
    
    
                if(greater(a[minIndex],a[j])){
    
    
                    minIndex = j;
                }
            }
            exch(a,i,minIndex);
        }

    }
    private boolean greater(Comparable v,Comparable w){
    
    
        return v.compareTo(w) > 0;
    }
    private void exch(Comparable []a,int i,int j){
    
    
        Comparable temp;
        temp = a[i];
        a[i] = a[j];
        a[j] = temp;
    }

}

Time complexity O(n 2 )

Guess you like

Origin blog.csdn.net/weixin_48180029/article/details/112988204