选择排序(SelectionSort)

选择排序(SelectionSort)

在这里插入图片描述

public class SelectionSort {
    
    

	private static void selectionSort(int[] arr, int n) {
    
    
		for (int i = 0; i < n; i++) {
    
    
			// 寻找最小值minIndex
			int minIndex = i;
			for (int j = i + 1; j < n; j++) {
    
    
				if (arr[j] < arr[i]) {
    
    
					minIndex = j;
				}
				if (i != minIndex) {
    
    
					int temp = arr[i];
					arr[i] = arr[minIndex];
					arr[minIndex] = temp;
				}
			}
		}
	}

	public static void main(String[] args) {
    
    
		int[] a = new int[] {
    
     10, 9, 8, 7, 6, 5, 4, 3, 2, 1 };
		SelectionSort.selectionSort(a, 10);
		for (int i = 0; i < 10; i++) {
    
    
			System.out.print(a[i] + " ");
		}
	}
}

猜你喜欢

转载自blog.csdn.net/weixin_44524658/article/details/114391023