【數據結構與算法】選擇排序

選擇排序沒什麼好說的,直接上代碼吧

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 + " ");
		}
	}
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章