冒泡排序,快速排序
2016-03-09 11:16
260 查看
public class TestSort {
public static void main(String[] args) {
int nums[] = { 7, 5, 3, 8, 4, 6, 1, 2 };
// bubbleSort(nums);
quickSort(nums, 0, nums.length - 1);
printNums(nums);
}
/**
* 冒泡排序
*/
public static void bubbleSort(int[] nums) {
int temp;
for (int i = 0; i < nums.length - 1; i++) {
for (int j = 0; j < nums.length - 1 - i; j++) {
if (nums[j] > nums[j + 1]) {
temp = nums[j];
nums[j] = nums[j + 1];
nums[j + 1] = temp;
}
}
}
}
/**
* 快速排序
*/
public static void quickSort(int[] nums, int left, int right) {
if (left >= right) {
return;
}
int i = left, j = right, x = nums[left];
while (i < j) {
while (i < j) {
if (nums[j] < x) {
nums[i] = nums[j];
break;
}
j--;
}
if (i < j) {
i++;
}
while (i < j) {
if (nums[i] > x) {
nums[j] = nums[i];
break;
}
i++;
}
if (i < j) {
j--;
}
}
nums[i] = x;
quickSort(nums, left, i - 1);
quickSort(nums, i + 1, right);
}
public static void printNums(int[] nums) {
StringBuilder sb = new StringBuilder();
for (int num : nums) {
sb.append(num + ", ");
}
System.out.println(sb.toString());
}
}
相关文章推荐
- 交换排序:冒泡排序,快速排序
- 排序算法(冒泡排序、选择排序、插入排序、归并排序、快速排序、希尔排序)
- 直接插入排序、希尔排序、 冒泡排序 、快速排序 、直接选择排序、 堆排序 、归并排序 、基数排序的比较
- 选择排序、快速排序、冒泡排序、插入排序
- 各种排序算法实现——基数排序、归并排序、插入排序、冒泡排序、选择排序、快速排序、堆排序、希尔排序
- 用Java语言实现的各种排序,包括插入排序、冒泡排序、选择排序、Shell排序、快速排序、归并排序、堆排序、SortUtil等...
- 快速排序 冒泡排序
- 排序算法java版,速度排行:冒泡排序、简单选择排序、直接插入排序、折半插入排序、希尔排序、堆排序、归并排序、快速排序...
- 交换排序——冒泡排序和快速排序——C语言描述
- 快速排序 冒泡排序
- 10种算法原理(冒泡排序,选择排序,快速排序,堆排序,希尔排序,桶排序等)
- 冒泡排序-插入排序-快速排序-选择排序---飞天博客
- 快速排序,冒泡排序,插入排序 完整示例
- 4000 排序算法之桶排序,冒泡排序,快速排序
- 七大内部排序算法总结(插入排序、希尔排序、冒泡排序、简单选择排序、快速排序、归并排序、堆排序)
- 常用的选择排序.Shell排序.快速排序.冒泡排序.插入排序的算法
- 黑马程序员_C语言基础_数组之冒泡排序、快速选择排序、折半查找
- 快速排序,冒泡排序,直接选择排序的算法实现
- php冒泡排序、快速排序、快速查找、二维数组去重实例分享
- java必须知道的八大种排序算法:冒泡排序、 选择排序、插入排序、快速排序、希尔算法、归并排序算法、基数排序、堆排序算法