标签:stat pad bubble 插入排序 简单选择 quick 规则 java qsort
1 package Sort; 2 3 /** 4 * Created by lenovo on 2017/9/6. 5 */ 6 /* 7 * 优化的冒泡排序 8 * 如果数组i之后元素之间没有发生交换,那么说明i之后的元素已经排好序了,此时flag标记为false, 9 * 数组排序结束,否则继续进行比较、交换。 10 * */ 11 public class BubbleSort { 12 public void bubbleSort(int[] array) { 13 int length = array.length; 14 boolean flag = true; 15 for (int i = 0; i < length-1 && flag; i++) { 16 flag = false; 17 for (int j = length-2; j >=i; j--) { 18 if (array[j] > array[j + 1]) { 19 swap(array, j, j + 1); 20 flag = true; 21 } 22 } 23 } 24 } 25 public void swap(int[] array, int i, int j) { 26 int temp = array[i]; 27 array[i] = array[j]; 28 array[j] = temp; 29 } 30 public static void main(String[] args){ 31 BubbleSort bubbleSort=new BubbleSort(); 32 int[] a={5,2,3,4,1}; 33 bubbleSort.bubbleSort(a); 34 for (int n:a)System.out.println(n); 35 } 36 }
1 package Sort; 2 3 /** 4 * Created by lenovo on 2017/9/6. 5 */ 6 /* 7 *简单选择排序 8 * 每轮比较过后,找出最小值的下标minIndex,如果i==minIndex,则找到最小值,不需要交换; 9 * 如果i!=minIndex,则需要交换i与minIndex位置的数值 10 * */ 11 public class SelectSort { 12 public void selectSort(int[] array){ 13 int length=array.length; 14 int minIndex; 15 for (int i=0;i<length-1;i++){ 16 minIndex=i; 17 for (int j=i+1;j<length;j++){ 18 if (array[j]<array[minIndex]) 19 minIndex=j; 20 } 21 if (minIndex!=i) 22 swap(array,minIndex,i); 23 } 24 } 25 public void swap(int[] array, int i, int j) { 26 int temp = array[i]; 27 array[i] = array[j]; 28 array[j] = temp; 29 } 30 public static void main(String[] args){ 31 SelectSort selectSort=new SelectSort(); 32 int[] a={5,2,3,4,1}; 33 selectSort.selectSort(a); 34 for (int n:a)System.out.println(n); 35 } 36 }
1 package Sort; 2 3 /** 4 * Created by lenovo on 2017/9/5. 5 */ 6 public class HeapSort { 7 //堆排序: 8 // 将无序序列{50,10,90,30,70,40,80,60,20}转换成{0,50,10,90,30,70,40,80,60,20} 9 // 使原序列数字下标一次为1,2,3,4,5,6,7,8,9; 10 // 1,将无序序列构建成一个大顶堆(大顶堆:任意结点的值大于其所有子结点的值); 11 // 2,结合完全二叉树性质,找出该大顶堆中的根结点(下标为在[1,length/2]范围的结点都是根结点); 12 // 3,将所有的根结点所在的子树构建成一个大顶堆; 13 // 4,将整个无序序列构建成一个大顶堆后,将堆顶结点值与当前大顶堆最后一个结点值交换, 14 // 此时大顶堆结构被破坏,再将[1,i-1]重构成大顶堆,i的初始值为length,之后重复步骤4; 15 public void heapSort(int[] array) { 16 int length = array.length; 17 int i; 18 int[] heapArray = new int[length + 1]; 19 System.arraycopy(array, 0, heapArray, 1, length); 20 for (i = length / 2; i > 0; i--) 21 HeapAdjust(heapArray, i, length); 22 for (i = length; i > 1; i--) { 23 swap(heapArray, 1, i); 24 HeapAdjust(heapArray, 1, i - 1); 25 } 26 System.arraycopy(heapArray, 1, array, 0, length); 27 } 28 29 public void HeapAdjust(int[] array, int i, int length) { 30 int root = array[i]; 31 for (int j = 2 * i; j <= length; j = 2 * j) { 32 if (j < length && array[j + 1] > array[j]) 33 j = j + 1; 34 if (root < array[j]) { 35 array[i] = array[j]; 36 i = j; 37 } else break; 38 } 39 array[i] = root; 40 } 41 42 public void swap(int[] array, int i, int j) { 43 int temp = array[i]; 44 array[i] = array[j]; 45 array[j] = temp; 46 } 47 }
1 package Sort; 2 3 /** 4 * Created by lenovo on 2017/9/4. 5 */ 6 public class StraightInsertSort { 7 //直接插入排序 8 public void straightInsertSort(int[] array) { 9 int length = array.length; 10 int soldier, i, j; 11 for (i = 1; i < length; i++) { 12 if (array[i - 1] > array[i]) { 13 soldier = array[i];//设置哨兵 14 for (j = i - 1; j >= 0 && array[j] > soldier; j--) 15 array[j + 1] = array[j]; 16 array[j + 1] = soldier; 17 } 18 } 19 } 20 21 public static void main(String[] args) { 22 StraightInsertSort s = new StraightInsertSort(); 23 int[] a = {5, 3, 4, 6, 2}; 24 s.straightInsertSort(a); 25 for (int n : a) System.out.print(n); 26 } 27 }
1 package Sort; 2 3 /** 4 * Created by lenovo on 2017/9/5. 5 */ 6 public class ShellSort { 7 //希尔排序:设置增量,将序列分割成几个子序列,分别对子序列进行“直接插入排序”,使整个序列基本有序; 8 // 最后increment=1再对整个序列进行直接插入排序。 9 public void shellSort(int[] array) { 10 int length = array.length; 11 int increment = length; 12 int soldier, i, j; 13 do { 14 increment = increment / 3 + 1; 15 for (i = increment; i < length; i += increment) { 16 if (array[i - increment] > array[i]) { 17 soldier = array[i]; 18 for (j = i - increment; j >= 0 && array[j] > soldier; j -= increment) 19 array[j + increment] = array[j]; 20 array[j + increment] = soldier; 21 } 22 } 23 } while (increment > 1); 24 } 25 26 }
1 package Sort; 2 3 /** 4 * Created by lenovo on 2017/9/5. 5 */ 6 public class MergeSort { 7 //归并排序 8 public void mergeSort(int[] array) { 9 int length = array.length; 10 int[] newArray = new int[length]; 11 int k = 1; 12 while (k < length) { 13 //将array序列分割成若干长度为k的子序列,再将这些子序列两两合并,放进序列newArray 14 MergePass(array, newArray, k, length); 15 //长度k加倍 16 k = k * 2; 17 //将进行过一轮排序以及合并后的序列newArray再分割成若干长度为k的子序列,再将这些子序列两两合并,放进序列Array 18 MergePass(newArray, array, k, length); 19 //长度k再加倍 20 k = k * 2; 21 } 22 } 23 24 public void MergePass(int[] array, int[] newArray, int dis, int length) { 25 int i = 0; 26 /* 27 * 情况1:按长度为dis来分割整个序列array,分割过程中当剩余的序列array长度>=2dis时, 28 * 可以进行分割归并,如下 29 * */ 30 while (i <= length - 2 * dis) { 31 Marge(array, newArray, i, i + dis - 1, i + 2 * dis - 1); 32 i = i + dis * 2; 33 } 34 /* 35 *情况2:分割过程中当最后剩余的序列array长度在(dis,2dis)范围时,将序列仅且分为2个子序列, 36 * 一个长度为dis,另一个长度不足dis,将两个子序列进行归并 37 * */ 38 if (i < length - dis) 39 Marge(array, newArray, i, i + dis - 1, length - 1); 40 /* 41 * 情况3:分割过程中当最后剩余的序列array长度在<dis时,那么只有一个长度不足dis的子序列, 42 * 无法进行归并,则剩下的这个子序列直接添加进newArray末尾 43 * */ 44 else 45 for (int j = i; j < length; j++) { 46 newArray[j] = array[j]; 47 } 48 } 49 50 public void Marge(int[] array, int[] newArray, int i, int mid, int last) { 51 int k = i;//newArray下标k 52 int j = mid + 1; 53 //待合并的两个子序列array[i,mid]和array[mid+1,last]的元素逐个进行比较, 54 // 从小到大依次存入newArray数组里面,算是完成了两个子序列的合并 55 for (; i <= mid && j <= last; k++) { 56 if (array[i] < array[j]) newArray[k] = array[i++]; 57 else newArray[k] = array[j++]; 58 } 59 //上述比较过后,将第一个子序列的剩余元素存入newArray 60 if (i <= mid) 61 for (int l = i; l <= mid; l++) 62 newArray[k++] = array[l]; 63 //上述比较过后,将第二个子序列的剩余元素存入newArray 64 if (j <= last) 65 for (int l = j; l <= last; l++) 66 newArray[k++] = array[l]; 67 } 68 }
1 package Sort; 2 3 /** 4 * Created by lenovo on 2017/9/5. 5 */ 6 public class QuickSort { 7 //快速排序 8 //找一个分割点part,part左边的数小于part,右边的数大于part,依次规则, 9 // 将子序列的首个数字设置成一个哨兵,将它与该子序列的其他元素进行比较交换, 10 // 使得整个子序列最终的排序结果为:哨兵左边的数小于哨兵,右边的数大于哨兵,返回part的下标; 11 //然后以part下标为分割点将序列分为左右两个子序列。 12 //依次对左右子序列进行寻找分割点、分割成更小的子序列的操作,直到序列不能再分割。 13 public void quickSort(int[] array) { 14 int length = array.length; 15 QSort(array, 0, length - 1); 16 } 17 18 public void QSort(int[] array, int low, int high) { 19 int partIndex; 20 if (low < high) { 21 partIndex = getPartIndex(array, low, high); 22 QSort(array, low, partIndex); 23 QSort(array, partIndex + 1, high); 24 } 25 } 26 27 public int getPartIndex(int[] array, int low, int high) { 28 int soldier = array[low]; 29 while (low < high) { 30 while (low < high && array[high] >= soldier) 31 high--; 32 swap(array, low, high); 33 while (low < high && array[low] <= soldier) 34 low++; 35 swap(array, low, high); 36 } 37 return low; 38 } 39 public void swap(int[] array, int i, int j) { 40 int temp = array[i]; 41 array[i] = array[j]; 42 array[j] = temp; 43 } 44 45 }
标签:stat pad bubble 插入排序 简单选择 quick 规则 java qsort
原文地址:http://www.cnblogs.com/gxclmx/p/7484234.html