6种排序的实现

 

1/*插入排序  平均速度为O(n^2)
 * 1.将数组前两个数据进行从小到达的排序
 * 2.将第3个数据和排好序的连个数据进行比较,将第3个数据插入合适的位置
 * 3.将第4个数据插入已经排好序的前3个数据中,不断重复以上过程,直到把最后一个数据插入到合适的位置
 */
2*shell排序   平均速度为O(n^(3/2))
 * 1.将有n个元素的数组分成n/2个数组序列,第一个数据和第n/2+1个数据为一对,......
 * 2.一次循环使每个序列对都拍好顺序
 * 3.将序列变为n/4个序列,再次排序
 * 4.不断重复上诉过程,随着序列减少,最后变为一个,完成排序
 */
3/*快速排序   平均速度为O(nlogn)
 * 1.首先设定一个分界值,通过分界值将数组分成左右两个部分
 * 2。将大于分界值的数组放到分界值的右边,将小于分界值的数组放到分界值左边
 * 3.左边和右边可以独立排序,左边则又可取分界值,将左边数组分成两边,右边同理
 * 4.重复以上 过程,是递归原理
 */
4/*冒泡排序  平均速度为O(n^2)
 * 1.依次比较相邻元素的大小
 * 2.如果前面的元素大于后面的元素,就交换这两个数据,经过第一轮比较可将最小的放最前面
 * 3.用同样方法把所有元素拍好//
 */
5/*选择排序   平均速度为O(n^2)
 * 1.首先从数组中找到最小的数,将其和第一个位置的数据交换
 * 2.从剩下的n-1个数据中选择次小的1个数据,将其和第二个位置的数据交换
 */
6*合并排序   平均速度为O(nlogn)
 * 1。将含有n个节点的待排序数据序列看作由n个长度为1的有序子表组成,然后两两合并,得到长度为2的若干个有序子表
 * 2.再对子表进行两两合并,得到长度为4的若干有序子表
 * 3.重复直到子表长度为n,完成排序过程
 *
堆排序后续
 
public class Testsort {
	static final int size = 30;

	// 插入排序
	public static void insertSort(int[] a) {
		int i, j, t, h;
		for (i = 1; i < a.length; i++) {
			t = a[i];
			j = i - 1;
			while (j >= 0 && t < a[j]) {
				a[j + 1] = a[j];
				j--;
			}
			a[j + 1] = t;
			System.out.println("第" + i + "步排序结果");

			for (int k = 0; k < a.length; k++) {
				System.out.print(" " + a[k]);
			}
			System.out.print("\n");

		}
	}

	// shell排序
	public static void shellSort(int[] arr) {
		int i, j, h;
		int r, temp;
		int x = 0;
		for (r = arr.length / 2; r >= 1; r /= 2) {
			for (i = r; i < arr.length; i++) {
				temp = arr[i];
				j = i - r;
				while (j >= 0 && temp < arr[j]) {
					arr[j + r] = arr[j];
					j -= r;
				}
				arr[j + r] = temp;
			}
			x++;
			System.out.println("第" + x + "步排序结果");

			for (int k = 0; k < arr.length; k++) {
				System.out.print(" " + arr[k]);
			}
			System.out.print("\n");

		}
	}

	// 合并排序
	static void mergeOne(int a[], int b[], int n, int len) {
		int i, j, k, s, e;
		s = 0;
		while (s + len < n) {
			e = s + 2 * len - 1;
			if (e >= n) {
				e = n - 1;
			}
			k = s;
			i = s;
			j = s + len;
			while (i < s + len && j <= e) {
				if (a[i] <= a[j]) {
					b[k++] = a[i++];
				} else {
					b[k++] = a[j++];
				}
			}
			while (i < s + len) {
				b[k++] = a[i++];
			}
			while (j <= e) {
				b[k++] = a[j++];
			}
			s = e + 1;
		}
		if (s < n) {
			for (; s < n; s++) {
				b[s] = a[s];
			}
		}
	}

	static void mergeSort(int a[], int n) {
		int h, count, len, f;
		count = 0;
		len = 1;
		f = 0;
		int[] p = new int[n];
		while (len < n) {
			if (f == 1) {
				mergeOne(p, a, n, len);
			} else {
				mergeOne(a, p, n, len);
			}
			len = len * 2;
			f = 1 - f;
			count++;

			System.out.println("第" + count + "步排序结果");

			for (int k = 0; k < a.length; k++) {
				System.out.print(" " + a[k]);
			}
			System.out.print("\n");

		}
	}

	// 快速排序
	static void quickSort(int[] arr, int left, int right) {
		int f, t;
		int rtemp, ltemp;
		ltemp = left;
		rtemp = right;
		f = arr[(left + right) / 2];// 分界值
		while (ltemp < rtemp) {
			while (arr[ltemp] < f) {
				++ltemp;
			}
			while (arr[rtemp] > f) {
				--rtemp;
			}
			if (ltemp <= rtemp) {
				t = arr[ltemp];
				arr[ltemp] = arr[rtemp];
				arr[rtemp] = t;
				--rtemp;
				++ltemp;
			}
		}
		if (ltemp == rtemp) {
			ltemp++;

		}
		if (left < rtemp) {
			quickSort(arr, left, ltemp - 1);
		}
		if (ltemp > right) {
			quickSort(arr, rtemp + 1, right);
		}
	}

	// 冒泡排序
	static void bubbleSort(int[] a) {
		int temp;
		for (int i = 1; i < a.length; i++) {
			for (int j = 0; j < a.length - i; j++) {
				if (a[j] > a[j + 1]) {
					temp = a[j];
					a[j] = a[j + 1];
					a[j + 1] = temp;
				}
			}
			System.out.println("第" + i + "步排序结果");

			for (int k = 0; k < a.length; k++) {
				System.out.print(" " + a[k]);
			}
			System.out.print("\n");
		}
	}

	// 选择排序
	static void selectSort(int[] a) {
		int index, temp;
		for (int i = 0; i < a.length - 1; i++) {
			index = i;
			for (int j = i + 1; j < a.length; j++) {
				if (a[j] < a[index]) {
					index = j;// 记录最小值
				}
			}
				if (index != i) {
					temp = a[i];
					a[i] = a[index];
					a[index] = temp;
				}
				System.out.print("第" + i + "步排序结果:");
				for (int k = 0; k < a.length; k++) {
					System.out.print(" " + a[k]);
				}
				System.out.print("\n");

			}
		}
	

	// 输出
	static void print(int arr[]) {
		System.out.println("排序后的数组为:");
		for (int i = 0; i < size; i++) {
			System.out.print(arr[i] + " ");
		}
	}

	public static void main(String[] args) {
		int shuzu[]=new int[size];
	      int i;
	      for( i=0;i<size;i++){
	    	  shuzu[i]=(int)(100+Math.random()*(100+1));
	      }
	      System.out.println("输出排序前的数组");
	      for(i=0;i<size;i++){
	    	  System.out.print(shuzu[i]+" ");
	      }
	      System.out.print("\n");
	      System.out.println("---------------------插入排序-----------------");
	      long start1Time=System.currentTimeMillis();
	      insertSort(shuzu);
	      print(shuzu);
	      long end1Time=System.currentTimeMillis();
	      System.out.println("运行时间:"+(end1Time-start1Time)+"ms");
	      System.out.println("---------------------shell排序---------------");
	      long start2Time=System.currentTimeMillis();
	      shellSort(shuzu);
	      print(shuzu);
	      long end2Time=System.currentTimeMillis();
	      System.out.println("运行时间:"+(end2Time-start2Time)+"ms");
	      System.out.println("-----------------------合并排序---------------");
	      long start3Time=System.currentTimeMillis();
	      mergeSort(shuzu,size);
	      print(shuzu);
	      long end3Time=System.currentTimeMillis();
	      System.out.println("运行时间:"+(end3Time-start3Time)+"ms");
	      System.out.println("---------------------快速排序---------------");
	      long start4Time=System.currentTimeMillis();
	      quickSort(shuzu,0,size-1);
	      print(shuzu);
	      long end4Time=System.currentTimeMillis();
	      System.out.println("运行时间:"+(end4Time-start4Time)+"ms");
	      System.out.println("---------------------冒泡排序---------------");
	      long start5Time=System.currentTimeMillis();
	      bubbleSort(shuzu);
	      print(shuzu);
	      long end5Time=System.currentTimeMillis();
	      System.out.println("运行时间:"+(end5Time-start5Time)+"ms");
	      System.out.println("---------------------选择排序---------------");
	      long start6Time=System.currentTimeMillis();
	      selectSort(shuzu);
	      print(shuzu);
	      long end6Time=System.currentTimeMillis();
	      System.out.println("运行时间:"+(end6Time-start6Time)+"ms");
	}

}

其中的size可自行确定,当然数据越大,其运行时间长短越明显

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值