各种排序算法汇总(JAVA版,不定期更新)

参考网址:

http://blog.csdn.net/pzhtpf/article/details/7560294

1. 快速排序

package com.zby.quicksort;

public class QuickSort {
	int a[] = { 49, 38, 65, 97, 76, 13, 27, 49, 78, 34, 12, 64, 5, 4, 62, 99,
			98, 54, 56, 17, 18, 23, 34, 15, 35, 25, 53, 51 };
	public QuickSort() {
		quick(a);
		for (int i = 0; i < a.length; i++)
			System.out.println(a[i]);
	}
	
	public int getMiddle(int[] list, int low, int high) {
		int tmp = list[low]; // 数组的第一个作为中轴
		while (low < high) {
			while (low < high && list[high] >= tmp) {
				high--;
			}
			list[low] = list[high]; // 比中轴小的记录移到低端
			while (low < high && list[low] <= tmp) {
				low++;
			}
			list[high] = list[low]; // 比中轴大的记录移到高端
		}
		list[low] = tmp; // 中轴记录到尾
		return low; // 返回中轴的位置
	}

	public void _quickSort(int[] list, int low, int high) {
		if (low < high) {
			int middle = getMiddle(list, low, high); // 将list数组进行一分为二
			_quickSort(list, low, middle - 1); // 对低字表进行递归排序
			_quickSort(list, middle + 1, high); // 对高字表进行递归排序
		}
	}

	public void quick(int[] a2) {
		if (a2.length > 0) { // 查看数组是否为空
			_quickSort(a2, 0, a2.length - 1);
		}
	}
	public static void main(String[] args) {
		QuickSort qs = new QuickSort();
	}
}

2. 冒泡排序

package com.zby.bubblesort;

public class BubbleSort {
	public BubbleSort(){
		int[] a = {49,38,65,97,76,13,27,49,78,34,12,64,5,4,62,99,98,54,56,17,18,23,34,15,35,25,53,51}; 
		int temp = 0;
		for(int i = 0; i < a.length; i++)
			for(int j = 0; j < a.length - i - 1; j++){
				if(a[j] > a[j+1]){
					temp = a[j];
					a[j] = a[j+1];
					a[j+1]=temp;
				}
			}
		for( int i = 0; i < a.length; i++)
			System.out.println(a[i]);
	}
	
	public static void main(String[] args) {
		BubbleSort bs = new BubbleSort();
	}
}

3. 二分查找

package com.zby.jianzhioffer;

public class BinarySearch {
	public static int binarySearch(int[] arr, int goal) {
		int start = 0;
		int end = arr.length - 1;
		while (start <= end) {
			int middle = (start + end)/2;
			if(arr[middle] == goal)
				return middle + 1;
			else if(arr[middle] > goal)
				end = middle - 1;
			else if(arr[middle] < goal)
				start = middle + 1;
		}
		return -1;
	}
	public static void main(String[] args) {
		int[] a = {1,2,4,7,9,10,14,45,68,69,78,88,92,100,144,156,167};
		System.out.println(binarySearch(a, 78));
	}
}

4. 归并排序

参考链接:

http://blog.csdn.net/middlekingt/article/details/8446552

package com.zby.jianzhioffer;

public class MergeSort {
	public static int[] sort(int[] nums, int low, int high) {
		int mid = (low + high) / 2;
		if(low < high) {
			//左边
			sort(nums, low, mid);
			//右边
			sort(nums, mid + 1, high);
			//左右归并
			merge(nums, low, mid, high);
		}
		return nums;
	}
	public static void merge(int[] nums, int low, int mid, int high) {
		int[] temp = new int[high - low + 1];
		int i = low;//左指针
		int j = mid + 1;//右指针
		int k = 0;
		
		//除了最后的一个大数,把所有的数转移到新数组中
		while(i <= mid && j <= high) {
			if(nums[i] < nums[j]) {
				temp[k++] = nums[i++];
			}
			else {
				temp[k++] = nums[j++];
			}
		}
		
		//把左边剩余的数移入数组
		while(i <= mid) {
			temp[k++] = nums[i++];
		}
		
		//把右边剩余的数移入数组
		while(j <= high) {
			temp[k++] = nums[j++];
		}
		
		//把新数组中的数覆盖nums数组
		for(int k2 = 0; k2 < temp.length; k2++) {
			nums[k2 + low] = temp[k2];
		}
	}
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值