直接上代码
冒泡:
private void bubbleSort() { int array[] = {45, 32, 67, 3, 63, 21, 76, 7, 90, 65, 100, 96}; boolean swaped; do { swaped = false; for (int i = 0; i < array.length - 1; i++) { if (array[i] > array[i + 1]) { int temp = array[i + 1]; array[i + 1] = array[i]; array[i] = temp; swaped = true; } } } while (swaped);
选择:
private void selectSort() { int array[] = {45, 32, 67, 3, 63, 21, 76, 7, 90, 65, 100, 96}; for(int i=0; i< array.length -1; i++) { int miniIndex = i; for(int j = i+1; j<array.length; j++) { if(array[j] < array[miniIndex]) { miniIndex = j; } } int temp = array[miniIndex]; array[miniIndex] = array[i]; array[i] = temp; } }
插入:
private void insertSort() { int array[] = {45, 32, 67, 3, 63, 21, 76, 7, 90, 65, 100, 96}; for (int i = 0; i < array.length - 1; i++) { for (int j = i + 1; j > 0; j--) { if(array[j] < array[j-1]) { int temp = array[j - 1]; array[j - 1] = array[j]; array[j] = temp; } } } }