java实现选择排序算法

前面我们讲解了选择排序算法,现在我们用java代码来实现

package ttt;

public class SelectSort {
    public static int[] BubbleSort(int[] theArray) {
    	int tmp;
    	for(int i = 0; i < theArray.length; i++) {
    		for (int j= i+1; j theArray[j]) {
    				tmp = theArray[i];
    				theArray[i] = theArray[j];
    				theArray[j] =tmp;
    			}
    		}
    	}
    	return theArray;
    }
    public static void main(String[] args) {
    	int []the_array = {10,1,18,30,23,12,7,5,18,17};
        System.out.print("之前的排序:");
        for(int i = 0; i < the_array.length; i++) {
            System.out.print(the_array[i] + " ");
        }
        
        int []result_array = BubbleSort(the_array);
        
        System.out.print("选择排序:");
        for(int i = 0; i < result_array.length; i++) {
            System.out.print(result_array[i] + " ");
        }
    }
}

执行结果如下

之前的排序:10 1 18 30 23 12 7 5 18 17 选择排序:1 5 7 10 12 17 18 18 23 30 

符合预期

你可能感兴趣的:(算法与数据结构)