排序方法

1、递归计算n的阶乘

	/**
	 * 递归计算n的阶乘
	 * @param n
	 * @return
	 */
	public static int RecursionCalculate(int n){
		if (n == 0){
			return 1;
		}
		return n * RecursionCalculate(n - 1);
	}
	
	public static void main(String[] args) {
		int value = RecursionCalculate(4);
		System.out.println("递归:"+value);
	}	
	

2、迭代计算n的阶乘

	/**
	 * 迭代计算n的阶乘
	 * @param n
	 * @return
	 */
	public static int IterationCalculate(int n){
		int result = 1;
		for (int i =1; i <= n; i++){
			result*= i;
		}
		return result;
	}
	public static void main(String[] args) {
		int value2 = IterationCalculate(4);
		System.out.println("迭代:"+value2);
	}	

3、求和

	/**
	 * 求和
	 * @param n
	 * @return
	 */
	public static int Sum(int n	){
		if (n == 1){
			return 1;
		}
		return n + Sum(n -1);
	}
	public static void main(String[] args) {
		int value3 = Sum(100);
		System.out.println("求和:"+value3);
	}	

4、有序数组a、b合并成一个新的有序数组

	/**
	 * 有序数组a、b合并成一个新的有序数组
	 * @param a
	 * @param b
	 * @return
	 */
	public static int[] mergeArray(int[] a, int[] b) {
		int result[] = new int[a.length + b.length];
		if (checkSort(a) && checkSort(b)) {
			int i = 0, j = 0, k = 0;
			while (i < a.length && j < b.length) {
				if (a[i] <= b[j]) {
					result[k++] = a[i++];
				} else {
					result[k++] = b[j++];
				}
			}
			while (i < a.length) {
				result[k++] = a[i++];
			}
			while (j < b.length) {
				result[k++] = b[j++];
			}
		}
		return result;
	}

	public static void main(String[] args) {
		int[] a = { 1, 3, 4 };
		int[] b = { 2, 3, 5, 6, 7, 8 };
		System.out.print("数组合并排序:");
		int[] c = mergeArray(a, b);
		for (int n : c) {
			System.out.print(n + " ");
		}
	}	

5、插入排序

	/**
	 * 插入排序
	 * @param a
	 */
	public static void insertSort(int[] a) {
		int i, j, insertNote;
		for (i = 1; i < a.length; i++) {
			insertNote = a[i];
			j = i - 1;
			// 如果要插入的元素小于第j个元素,就将第j个元素向后移动
			while (j >= 0 && insertNote < a[j]) {
				a[j + 1] = a[j];
				j--;
			}
			// 直到要插入的元素不小于第j个元素,将insertNote插入到数组中
			a[j + 1] = insertNote;
		}
	}
	
	public static void main(String[] args) {
		System.out.print("插入排序:");
		int t[] = { 10, 1, 8, 20, 9, 67, 73, 91 };
		insertSort(t);
		for (int n : t) {
			System.out.print(n + ",");
		}
	}
	

7、冒泡排序

	/**
	 * 冒泡排序
	 * @param array
	 */
	public static void bubbleSort(int[] array){
		for (int i = 0; i < array.length - 1; i++) {
			for (int j = 0; j < array.length - 1 - i; j++) {
				if (array[j] > array[j+1]){
					int temp = array[j];
					array[j] = array[j+1];
					array[j+1] = temp;
				}
			}
		}
	}

	public static void main(String[] args) {
		System.out.print("冒泡排序:");
		int[] f = {2,12,23,1,89,56,88,24,42,9,88,9,24};
		bubbleSort(f);
		for (int n : f) {
			System.out.print(n + ",");
		}
	}

8、 选择排序

	/**
	 * 选择排序
	 * @param array
	 */
	public static void selectSort(int[] array){
		if (array == null || array.length < 2){
			return;
		}
		int index;
		for (int i = 0; i < array.length; i++) {
//			System.out.print("i["+i+"]"+array[i]+",   ");
			index = i;
			for (int j = i+1; j < array.length; j++) {
//				System.out.print("j["+j+"]"+array[j]+",   ");
				if (array[j] < array[index]){
					index = j;
				}
			}
			if (index != i){
				int temp = array[index];
				array[index] = array[i];
				array[i] = temp;
			}
			System.out.println();
		}
	}

	public static void main(String[] args) {
		System.out.print("选择排序:");
		int[] s = {2,12,23,1,89,56,88,24,42,9,88,9,24};
		selectSort(s);
		for (int n : s) {
			System.out.print(n + ",");
		}
	}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值