`
jiang5495
  • 浏览: 88676 次
  • 性别: Icon_minigender_1
  • 来自: 湖南
社区版块
存档分类
最新评论

常用排序算法,支持升序与降序

阅读更多

package com.algorithem.sort;

/*
 *各种排序算法之我理解:
 *选择排序:我认为这是最容易掌握的一种排序算法,其核心思想就是每一次都是从当前位置起到最后位置止,选取最大的或最小的放在当前位置;
 *冒泡排序:进行N-1次冒泡,每一次冒泡都将该范围内最大或量小的往上送,其实现手段是通过交换相邻位置的数来实现的;
 *插入排序:其核心思想是假定前面的数已经是有序的,只需把当前数插入前面的有序序列即可。其核心操作是数组的插入运算;
 *快速排序:其核心思想是分治,是二分思想的应用,实现手段递归,是平均时间效率最高的排序算法. 
 * 
 */

//常用排序算法小集,分别给出了升序与降序的方法,由flag进分升降区别
public class Sort {

	// 选择排序,flag用来标志是进行升序排序还是降序排序
	public static void chooseSort(int[] a, int flag) {
		int n = a.length - 1; // length是数组的一个属性,而不是通过方法获取
		for (int i = 0; i < n; i++) {
			int max = a[i], k = i;
			for (int j = i + 1; j <= n; j++) {
				if (flag >= 0) {
					if (a[j] >= max) {
						k = j;
						max = a[j];
					}
				} else {
					if (a[j] <= max) {
						k = j;
						max = a[j];
					}
				}

			}
			if (k != i) {
				int t = a[k];
				a[k] = a[i];
				a[i] = t;
			}
		}
	}

	// 冒泡排序,将大水泡或小水泡往上冒
	public static void maopaoSort(int[] a, int flag) {
		int n = a.length - 1;
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n - i; j++) {
				if (flag >= 0) {
					if (a[j] <= a[j + 1]) {
						int t = a[j];
						a[j] = a[j + 1];
						a[j + 1] = t;
					}
				} else {
					if (a[j] >= a[j + 1]) {
						int t = a[j];
						a[j] = a[j + 1];
						a[j + 1] = t;
					}
				}

			}
		}
	}

	// 插入法排序
	public static void insertSort(int[] a, int flag) {
		int n = a.length - 1, i, j, k, temp;
		for (i = 0; i <= n; i++) {
			for (j = 0; j <= i - 1; j++) {

				if (flag <= 0) {
					if (a[i] <= a[j]) {
						break;
					}
				} else {
					if (a[i] >= a[j]) {
						break;
					}
				}

			}
			temp = a[i];
			for (k = i - 1; k >= j; k--) {
				a[k + 1] = a[k];
			}
			if (j >= 0 && j <= n)
				a[j] = temp;

		}
	}

	// 快速排序,其中划分函数是重点
	public static void quickSort(int[] a, int flag) {
		quicksort(a, 0, a.length - 1, flag);
	}

	public static void quicksort(int[] a, int low, int high, int flag) {
		if (low <= high) {
			int point = divide(a, low, high, flag);
			quicksort(a, low, point - 1, flag);
			quicksort(a, point + 1, high, flag);
		}
	}

	public static int divide(int[] a, int low, int high, int flag) {
		int point = a[low];
		while (low < high) {
			if (flag <= 0) {
				while (a[high] >= point && high > low)
					high--;
				a[low] = a[high];
				while (a[low] <= point && low < high)
					low++;
				a[high] = a[low];
			} else {
				while (a[high] <= point && high > low)
					high--;
				a[low] = a[high];
				while (a[low] >= point && low < high)
					low++;
				a[high] = a[low];
			}

		}
		a[high] = point;
		return high;
	}

	public static void main(String[] args) {
		int[] a = { 1, 3, 4, 6, 7, 9, 9, 12, 44, 3, 6, 34 };
		// Sort.chooseSort(a, -1);
		// for (int i : a) {
		// System.out.print(i + "  ");
		// }
		// System.out.println();
		// Sort.chooseSort(a, 1);
		// for (int i : a) {
		// System.out.print(i + "  ");
		// }
		// System.out.println();
		// Sort.maopaoSort(a,1);
		// for (int i : a) {
		// System.out.print(i + " ");
		// }
		// System.out.println();
		// Sort.insertSort(a,1);
		// for (int i : a) {
		// System.out.print(i + " ");
		// }
		// System.out.println();
		// 测试快排升序
		Sort.quickSort(a, -1);
		for (int i : a) {
			System.out.print(i + " ");
		}
		System.out.println();
		// 测试快排降序
		Sort.quickSort(a, 1);
		for (int i : a) {
			System.out.print(i + " ");
		}
		System.out.println();

	}

}
1
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics