首页 > 代码库 > 【数据结构与算法】选择排序

【数据结构与算法】选择排序

选择排序没什么好说的,直接上代码吧

public class SelectSort {
	public void selectSort(int[] in) {
		int inLength = in.length;
		int minIndex = 0;
		for (int i = 0; i < inLength; i++) {
			minIndex = i;
			for (int j = i + 1; j < inLength; j++) {
				if (in[j] < in[minIndex]) {
					minIndex = j;
				}

			}
			int tmp = in[i];
			in[i] = in[minIndex];
			in[minIndex] = tmp;
		}
	}

	private void swap(int i, int j) {
		// TODO Auto-generated method stub
		i = i + j;
		j = i - j;
		i = i - j;
	}

	public static void main(String[] args) {
		int[] caseOne = { 6, 5, 4, 3, 2, 1, 10, 2 };
		int[] caseTwo = { 1, 6, 5, 2, 4, 3 };
		SelectSort mSelectSort = new SelectSort();
		mSelectSort.selectSort(caseOne);
		for (int i : caseOne) {
			System.out.print(i + " ");
		}
		System.out.println();
		mSelectSort.selectSort(caseTwo);
		for (int i : caseTwo) {
			System.out.print(i + " ");
		}
	}
}