3:冒泡排序
4:归并排序5:基数排序6:快速排序package cn.sort;import java.util.Scanner;//各种控排序算法public class Test { public static void main(String[] args) { int array[] = {22, 11, 9, 8,90, 7, 42}; System.out.println("没有排序之前:"); Sortall.print(array); System.out.println(); int num =1; System.out.println("请输入需要排序的方法序号:"); System.out.println("1:简单选择排序"); System.out.println("2:堆排序"); System.out.println("3:冒泡排序"); System.out.println("4:归并排序"); System.out.println("5:基数排序"); System.out.println("6:快速排序"); System.out.println("0:退出程序"); boolean loop = true; while(loop){ Scanner sc = new Scanner(System.in); num = sc.nextInt(); switch(num){ case 0: loop=false; System.out.println("退出程序"); break; case 1://简单选择排序 Sortall.SelectSort(array); System.out.println(); break; case 2: //堆排序 Sortall.HeapSort(array); System.out.println(); break; case 3: //冒泡排序 Sortall.BubbleSort(array); System.out.println(); break; case 4: //归并排序 Sortall.MergeSort(array); System.out.println(); break; case 5: //基数排序 Sortall.BaseSort(array); System.out.println(); break; case 6: //快速排序 Sortall.QuickSort(array); System.out.println(); break; default: System.out.println("无效输入\n"); break; } } }}
方法实现:
package cn.sort;import java.util.ArrayList;import java.util.List;public class Sortall{ //重写print方法输出数组 static void print(int array[]) { for (int i = 0; i < array.length; i++) { System.out.print(array[i]+" "); } } //交换函数 static void Swap(int a [],int i,int j){ int temp = a[i]; a[i] = a[j]; a[j] = temp; } //1.选择排序 public static void SelectSort(int []array){ System.out.println("----------------1.使用选择排序之后: ----------------------"); for(int i=0;ii ; j--) { if (array[j] = 0; i--) { //k保存正在判断的节点 int k = i; //如果当前k节点的子节点存在 while (k*2+1 <= lastIndex) { //k节点的左子节点的索引 int biggerIndex = 2*k + 1; //如果biggerIndex小于lastIndex,即biggerIndex代表的k节点的右子节点存在 if (biggerIndex < lastIndex) { //如果右子节点的值较大 if (data[biggerIndex] < data[biggerIndex + 1]) { //biggerIndex总是记录较大子节点的索引 biggerIndex++; } } //如果k节点的值小于其较大的子节点的值 if (data[k] 0) { quick(array, 0, array.length-1); } print(array);}static void quick(int a[], int low, int high){ if (low < high) { int middle = getMiddle(a, low, high); quick(a, 0, middle-1); quick(a, middle+1, high); }}static int getMiddle(int a[],int low,int high){ int temp = a[low]; while(low < high){ while(low < high && a[low]<= temp){ low ++; } a[high] = a[low]; } a[low] = temp; return low;}//归并排序public static void MergeSort(int[] array) { System.out.println("----------------4.使用归并排序之后: -----------------------"); if (array.length>0) { mergeSort(array, 0, array.length - 1); } print(array);}private static void mergeSort(int[] a, int left, int right) { if(left 0){ max = max/10; times++; } //建立十个队列 List queue = new ArrayList (); for (int i = 0; i < 10; i++) { ArrayList queue1 = new ArrayList(); queue.add(queue1); } //进行times次分配和收集 for (int i = 0; i < times; i++) { //分配 for (int j = 0; j < array.length; j++) { int x = array[j]%(int)Math.pow(10, i+1)/(int)Math.pow(10, i); ArrayList queue2 = queue.get(x); queue2.add(array[j]); queue.set(x,queue2); } //收集 int count = 0; for (int j = 0; j < 10; j++) { while(queue.get(j).size()>0){ ArrayList queue3 = queue.get(j); array[count] = queue3.get(0); queue3.remove(0); count++; } } } print(array);}}