import java.util.Arrays; /** * Created by zoupeng on 2017/12/19 0019 15:11 * 排序实现类 * @author Administrator */ public class ArraySort { public static void main(String[] args) { Integer[] tmp = {23, 45, 62, 3, 6, 1, 3, 7, 3, 723, 76, 4, 3, 2, 9}; System.out.println(Arrays.toString(tmp)); System.out.println("冒泡排序:"+Arrays.toString(ArraySort.bubblingSort(tmp))); System.out.println("选择排序:"+Arrays.toString(ArraySort.selectionSort(tmp))); System.out.println("插入排序:"+Arrays.toString(ArraySort.insertionSort(tmp))); System.out.println("希尔排序:"+Arrays.toString(ArraySort.shellSort(tmp))); System.out.println("快速排序:"+Arrays.toString(ArraySort.recQuickSort(tmp, 0, tmp.length-1))); } /** * 冒泡排序方法 * @param elems 需要排序的数组 * @return 返回排序后的数据 */ public static Integer[] bubblingSort(Integer[] elems){ Integer [] tmp = new Integer [elems.length]; //复制数组 System.arraycopy(elems, 0 , tmp, 0, elems.length); //遍历全部数据 for (int i = 0; i < tmp.length; i++) { //遍历当前元素后面的数组 for (int j = i; j < tmp.length ; j++) { //符合条件进行交换 if(tmp[i].compareTo(tmp[j]) > 0){ swap(i, j, tmp); } } } return tmp; } /** * 选择排序方法 * @param elems 需要排序的数组 * @return 返回排序后的数据 */ public static Integer[] selectionSort (Integer [] elems){ Integer [] tmp = new Integer [elems.length]; Integer tmpInt; //复制数组 System.arraycopy(elems ,0 , tmp, 0, elems.length); //遍历当前数组 for (int i = 0; i < tmp.length; i++) { //把当前数据下标缓存 tmpInt = i; for (int j = i; j < tmp.length ; j++) { //符合条件记录当前下标 if(tmp[tmpInt].compareTo(tmp[j]) > 0){ tmpInt = j; } } //找到条件的数据后进行转换 swap(i,tmpInt,tmp); } return tmp; } /** * 插入排序方法 * @param elems 需要排序的数组 * @return 返回排序后的数据 */ public static Integer[] insertionSort (Integer [] elems){ Integer [] tmp = new Integer [elems.length]; Integer tmpInt; Integer tmpInt2; //复制数组 System.arraycopy(elems ,0 ,tmp,0,elems.length); //遍历当前数组 for (int i = 1; i < tmp.length; i++) { tmpInt2 = tmp[i]; tmpInt = i; //循环把相邻的数据进行比较,相邻符合条件的数据进行转换 while (tmpInt > 0 && tmp[tmpInt - 1].compareTo(tmpInt2) > 0){ //把前一个下标数据赋值给当前下标数据 tmp[tmpInt] = tmp[tmpInt - 1]; //下标值减1 --tmpInt; } tmp[tmpInt] = tmpInt2; } return tmp; } /** * 希尔排序方法 * @param elems 需要排序的数组 * @return 返回排序后的数据 */ public static Integer[] shellSort (Integer [] elems){ Integer [] tmp = new Integer [elems.length]; Integer inner; Integer temp; int h = 1; //复制数组 System.arraycopy(elems , 0, tmp, 0, elems.length); //计算出最大H值 while (h <= tmp.length / 3){ h = h * 3 + 1; } while(h>0){ //把当前区间的数据进行遍历 for (int i = h; i < tmp.length; i++) { temp = tmp[i]; inner = i; //判断是否替换区间内数据 while (inner > h - 1 && tmp[inner - h].compareTo(temp) > 0 ){ tmp[inner] = tmp[inner - h]; inner -= h; } tmp[inner] = temp; } h = (h - 1) / 3; } return tmp; } /** * 快速排序方法 * @param elems 需要排序的数组 * @param left 左边的下标 * @param right 右边的下标 * @return 返回排序后的数据 */ public static Integer[] recQuickSort (Integer [] elems, int left , int right){ Integer [] tmp = new Integer [elems.length]; //复制数组 System.arraycopy(elems , 0, tmp, 0, elems.length); if(left >= right){ return tmp; }else{ dealPivot(left, right, tmp); //对当前数组进行分隔 int partition = partitionIt(left, right, tmp); //对分隔后的左边数据进行分隔 tmp = recQuickSort(tmp, left , partition - 1); //对分隔后的右边数据进行分隔 tmp = recQuickSort(tmp, partition + 1 , right ); } return tmp; } /** * 按最后一个数来划分区间 * @param left * @param right * @return */ public static Integer partitionIt (int left , int right,Integer [] tmp){ int pivot = tmp[right]; while(left < right){ //查找从左边比中间小的值 while(left < right && tmp[left] <= pivot){ ++left; } if(left < right){ swap(left,right,tmp); right--; } //查找从右边比中间小的值 while(left < right && tmp[right] >= pivot){ --right; } if(left < right){ swap(left,right,tmp); left++; } } return left; } /** * 对数组进行三数取中,并放到最后 * @param left * @param right * @param tmp */ public static void dealPivot( int left, int right, Integer[] tmp) { int mid = (left + right) / 2; if (tmp[left] > tmp[mid]) { swap(left, mid, tmp); } if (tmp[left] > tmp[right]) { swap(left, right, tmp); } if (tmp[right] < tmp[mid]) { swap(right, mid, tmp); } swap(right - 1, mid, tmp); } /** * 两个变量相互交换方法 * @param elems1 需要交换的下标 * @param elems2 需要交换的下标 * @param tmp 需要交换的数组 */ public static void swap(Integer elems1, Integer elems2, Integer [] tmp){ Integer tmpInt = tmp[elems1] ; tmp[elems1] = tmp[elems2]; tmp[elems2] = tmpInt; } }
java 简单排序
最新推荐文章于 2022-01-22 21:50:27 发布