您的位置:首页 > 编程语言 > Java开发

基本算法的java实现

2014-09-28 09:34 441 查看
private static void  insertSort(int[] a){
		for(int i=1;i<a.length;i++){
			int j= i-1;
			while(j>=0 && a[j] > a[j+1]){
				int temp = a[j+1];
				a[j+1] = a[j];
				a[j] = temp;
				j--;
			}
		}
	}

这是插入排序的代码;

private static int partion(int[] a,int low,int high){
		int temp = a[low];
		while(low < high){
		while(low < high && a[high] >= temp)
			high --;
		a[low] = a[high];
		while(low < high && a[low] <= temp)
			low++;
		a[high] = a[low];
		}
		a[low] = temp;
		return low;
	}
	private static void qiuckSort(int[] a,int low,int high){
		if(low < high){
			int temp = partion(a, low, high);
			qiuckSort(a, low, temp-1);
			qiuckSort(a, temp+1, high);
			
		}
	}


这是快速排序

private static void bubbleSort(int[] a){
		for(int i = 0;i<a.length-1 ;i++){
			for(int j=0;j<a.length-1-i;j++){
				if(a[j] > a[j+1]){
					int temp = a[j];
					a[j] = a[j+1];
					a[j+1] = temp;
				}
			}
		}
	}


这是冒泡排序

private static void selectSort(int[] a){
		for(int i=0;i<a.length-1;i++){
			int min = a[i];
			int minIndex=i;
			for(int j= i;j<a.length;j++){
				if(a[j] < min){
					min = a[j];
					minIndex = j;
				}
			}
			a[minIndex] = a[i];
			a[i] = min;
		}
	}
这是选择排序;

以上排序方法均由本人亲自编写测试!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: