交换排序-快速排序

public static void main(String[] args) {
		int a[] = { 3, 1, 5, 7, 2, 4, 9, 6, 10, 8 };
		System.out.print("初始值:");
		print(a);

		shellSort(a);
		System.out.print("\n排序后:");
		print(a);
	}

	public static void print(int a[], int... n) {
		outer:for (int i = 0; i < a.length; i++) {
			if (i == 0) {
				System.out.print("[");
			}
			for (int j : n) {
				if(i==j){
					System.out.print("*" + a[i] + "*");					
					if (i == a.length - 1) {
						System.out.print("]");
					}
					continue outer;
				}
			}
			System.out.print(" " + a[i] + " ");
			if (i == a.length - 1) {
				System.out.print("]");
			}
		}
	}

	public static void shellSort(int[] a) {
		int dk = a.length / 2;
		System.out.println("\r\n交换过程:");
		while (dk >= 1) {
			System.out.println("  开始一轮比对:"); // 输出过程
			ShellInsertSort(a, dk);
			dk = dk / 2;
			System.out.println();
		}
	}

	// 类似插入排序,只是插入排序增量是1,这里增量是dk,把1换成dk就可以了
	public static void ShellInsertSort(int[] a, int dk) {
		for (int i = dk; i < a.length; i++) {
			System.out.print("   当前数组:"); // 输出过程
			print(a,i,i-dk); // 输出过程
			System.out.println("\t比较元素:" + "a[" + i + "]:" + a[i] + " \t" + "a[" + (i - dk) + "]:" + a[i - dk]); // 输出过程
			if (a[i] < a[i - dk]) {
				int j;
				int x = a[i];// x为待插入元素
				a[i] = a[i - dk];
				for (j = i - dk; j >= 0 && x < a[j]; j = j - dk) {// 通过循环,逐个后移一位找到要插入的位置。
					a[j + dk] = a[j];
				}
				a[j + dk] = x;// 插入
				System.out.println("  交换成功!");
			}
		}
	}

解析版本:

      解析版本不用看,直接复制到eclipse中,运行,辅助理解。

public static void main(String[] args) {
		int a[] = { 3, 1, 5, 7, 10, 4, 9, 6, 2, 8 };
		System.out.println("初始值:");
		print(a);
		
		System.out.println("\r\n交换过程:");
		int h = a.length - 1;
		quickSort(a, 0, h);
		
		System.out.println("\n排序后:");
		print(a);
	}
	public static void print(int a[]){
		for (int i = 0; i < a.length; i++) {
			if (i == 0) {
				System.out.print("[{");
			}
			System.out.print(" " + a[i] + " ");
			if (i == a.length - 1) {
				System.out.print("]");
			}
		}
	}
	public static void print(int a[], int... n) {
		outer:for (int i = 0; i < a.length; i++) {
			if (i == 0) {
				System.out.print("[");
			}
			
			if(i==n[1]){
				System.out.print("{ ");	
			}
			System.out.print(" ");
			if(i==n[0]){
				System.out.print("} *");
			}
			System.out.print(a[i]);
			if(i==n[0]){
				System.out.print("* {");
			}
			System.out.print(" ");
			if(i==n[2]){
				System.out.print("} ");
			}
						
			if (i == a.length - 1) {
				System.out.print("]");
			}
		}
	}

	public static void quickSort(int[] a, int low, int high) {	//迭代进行快速排序
		if (low < high) {
			// 如果不加这个判断递归会无法退出导致堆栈溢出异常
			int middle = getMiddle(a, low, high);
			
			
			quickSort(a, 0, middle - 1);
			// 递归对低子表递归排序
			quickSort(a, middle + 1, high);
			// 递归对高子表递归排序
		}
	}

	public static int getMiddle(int[] a, int low, int high) {
		int l1 = low;
		int h1 = high;
		int key = a[low]; // 基准元素,排序中会空出来一个位置
		while (low < high) {
			while (low < high && a[high] >= key) {
				// 从high开始找比基准小的,与low换位置
				high--;
			}
			a[low] = a[high];
			while (low < high && a[low] <= key) {
				// 从low开始找比基准大,放到之前high空出来的位置上
				low++;
			}
			a[high] = a[low];
		}
		a[low] = key;// 此时low=high 是基准元素的位置,也是空出来的那个位置
		
		System.out.print("   当前数组:"); // 输出过程
		print(a,low,l1,h1); // 输出过程
		System.out.println("   middle值:" + "a[" + low + "]:" + key+"  low="+l1+",high="+h1); // 输出过程
		
		return low;
	}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值