`
mdxdjh2
  • 浏览: 11992 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

JAVA 排序算法(快速、冒泡)

 
阅读更多

    快速排序:

public static void quiteSort(int[] o, int low, int hight) {
	if (low < hight) {
		int povitePosition = adjust(o, low, hight);
		quiteSort(o, low, povitePosition - 1);
		quiteSort(o, povitePosition + 1, hight);
	}
}

private static int adjust(int[] o, int low, int hight) {// 选定枢轴为low所对应的值
	int pivote = o[low];
	while (low < hight) {
		while (hight > low && pivote <= o[hight] ) {// 从右边开始查找比轴小的值
			hight--;
		}
		o[low] = o[hight];
		while (hight > low && pivote >= o[low]) { // 从左边开始查找比轴大的值
			low++;
		}
		o[hight] = o[low];
	}
	o[low] = pivote;
	return low;
}

public static void main(String[] args) {
	int[] i = { 26, 53, 48, 15, 13, 46, 32, 16 };
	quiteSort(i, 0, i.length - 1);
	for (int ii : i) {
		System.out.print(ii + " ");
	}
}

 

    冒泡排序:

public static void main(String[] args) {
	int[] array = new int[]{8,4,5,7,6,1};
	BubbleSort(array);
}

public static void BubbleSort(int[] array) {
	for (int i=0;i<array.length-1;i++) {
		for (int j=0;j<array.length-i-1;j++) {
			if (array[j]>array[j+1]) {
				int temp = array[j];
				array[j] = array[j+1];
				array[j+1] = temp;
			}
		}
	}
	
	for (int l=0;l<array.length;l++) {
		System.out.println(array[l]);
	}
}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics