数据结构之排序算法整理(2)
2014-09-08 13:50
176 查看
首先,看一下两种交换排序。
1.冒泡排序
2.快速排序
package com.QuickSort;
import java.util.Arrays;
/**
* 交换排序中的快速排序
* 基本思想:
* 从待排序的数组中任取一个数据作为分界值,所有比其小的数据元素一律放到左边,所有比其大的元素一律放到右边。
* 经过一趟遍历,该数组形成左右两个子序列,左边序列中数据元素的值都比分界值小,右边序列中的数据元素值都比分界值大。
* (分界值的选取是关键点,简单起见,一般都是选择一个值)。
* 然后对左右两个子数组进行递归,对两个子序列重新选择分界值并以此进行调整,知道每个子数组只有一个元素。
* @author tcj
*
*/
public class QuickSortImp {
private static void QuickSort(int[] a,int start,int end){
if(start < end){
int j = Partition(a,start,end);
QuickSort(a,start,j);
QuickSort(a,j+1,end);
}
}
private static int Partition(int[] a,int start,int end){
if(a == null){
return -1;
}
int i = start;
int j = end;
int pivot = a[start];
while(i < j){
while(i < j && a[i] < pivot){
i++;
}
while(i < j && a[j] > pivot){
j--;
}
if(i < j){
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
// int temp = pivot;
// pivot = a[j];
// a[j] = temp;
return j;
}
public static int partition(int n[], int left, int right) {
int pivot = n[left];
while (left < right) {
while (left < right && n[right] >= pivot)
right--;
if (left < right)
n[left++] = n[right];
while (left < right && n[left] <= pivot)
left++;
if (left < right)
n[right--] = n[left];
}
n[left] = pivot;
return left;
}
public static void main(String[] args){
int[] a = {1,2,4,6,8,0,3,5,7,9};
System.out.println(partition(a,0,9));
QuickSort(a,0,9);
System.out.println(Arrays.toString(a));
// int[] b = {3,4,1,2,5};
// System.out.println(Partition(b,0,4));
// System.out.println(Arrays.toString(b));
}
}
1.冒泡排序
package com.BubbleSort; import java.util.Arrays; /** * 交换排序中的冒泡排序 * 对于一个包含n个元素的数组,需要进行n-1趟比较。 第一趟:依次比较数组索引为0和1,1和2,……,n-2和n-1的元素,如果发现前一个数据大于后一个数据,则进行交换。经过第一趟,最大(最小)元素排到最后; 第一趟:依次比较数组索引为0和1,1和2,……,n-3和n-2的元素,如果发现前一个数据大于后一个数据,则进行交换。经过第二趟,最大(最小)元素排到倒数第二位; …… 第n-1趟:依次比较数组索引为0和1,如果发现前一个数据大于后一个数据,则进行交换。 冒泡排序的每趟结束后,不仅能将当前最大(最小)值排列出来,还能部分理顺数组中其他元素;一旦某一趟遍历是没有发生元素交换,则可提前结束排序。 算法时间复杂度O(n*n);空间复杂度O(1);稳定 * @author tcj * */ public class BubbleSort { public static void bubbleSort(int[] datas){ //边界检查 if(datas == null){ return; } int length = datas.length - 1; if(length == -1){ return; } for(int i = 0;i < length;i++){ //一趟冒泡中有没有做交换 boolean flag = false; for(int j = 0;j < length - i;j++){ //交换 if(datas[j] > datas[j+1]){ int tmp = datas[j]; datas[j] = datas[j+1]; datas[j+1] = tmp; flag = true; } } // System.out.println(Arrays.toString(datas)); //如果某次遍历未发生交换,结束遍历 if(!flag){ break; } } } //测试 public static void main(String[] args){ // int[] array = {22,30,49,30,16,9}; // BubbleSort.bubbleSort(array); // System.out.println(Arrays.toString(array)); // int[] a = {}; // BubbleSort.bubbleSort(a); // System.out.println(Arrays.toString(a)); // int[] b = null; // BubbleSort.bubbleSort(b); // System.out.println(Arrays.toString(b)); int[] c = {1,2,3,4,5}; BubbleSort.bubbleSort(c); System.out.println(Arrays.toString(c)); } }
2.快速排序
package com.QuickSort;
import java.util.Arrays;
/**
* 交换排序中的快速排序
* 基本思想:
* 从待排序的数组中任取一个数据作为分界值,所有比其小的数据元素一律放到左边,所有比其大的元素一律放到右边。
* 经过一趟遍历,该数组形成左右两个子序列,左边序列中数据元素的值都比分界值小,右边序列中的数据元素值都比分界值大。
* (分界值的选取是关键点,简单起见,一般都是选择一个值)。
* 然后对左右两个子数组进行递归,对两个子序列重新选择分界值并以此进行调整,知道每个子数组只有一个元素。
* @author tcj
*
*/
public class QuickSortImp {
private static void QuickSort(int[] a,int start,int end){
if(start < end){
int j = Partition(a,start,end);
QuickSort(a,start,j);
QuickSort(a,j+1,end);
}
}
private static int Partition(int[] a,int start,int end){
if(a == null){
return -1;
}
int i = start;
int j = end;
int pivot = a[start];
while(i < j){
while(i < j && a[i] < pivot){
i++;
}
while(i < j && a[j] > pivot){
j--;
}
if(i < j){
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
// int temp = pivot;
// pivot = a[j];
// a[j] = temp;
return j;
}
public static int partition(int n[], int left, int right) {
int pivot = n[left];
while (left < right) {
while (left < right && n[right] >= pivot)
right--;
if (left < right)
n[left++] = n[right];
while (left < right && n[left] <= pivot)
left++;
if (left < right)
n[right--] = n[left];
}
n[left] = pivot;
return left;
}
public static void main(String[] args){
int[] a = {1,2,4,6,8,0,3,5,7,9};
System.out.println(partition(a,0,9));
QuickSort(a,0,9);
System.out.println(Arrays.toString(a));
// int[] b = {3,4,1,2,5};
// System.out.println(Partition(b,0,4));
// System.out.println(Arrays.toString(b));
}
}
相关文章推荐
- 数据结构之排序算法整理(1)
- 数据结构之排序算法整理(3)
- 数据结构之排序算法整理(4)
- 数据结构之排序算法整理(5)
- DB2 V9 常用SQL整理 - Part 1
- asp.net原理(总结整理 2)
- 测试工具整理(5)——性能测试
- Centos下Nginx+PHP+MySql的安装和配置(综合整理)
- linux 常用高级操作指令整理(不定期更新)
- java访问权限整理
- 如鹏网JAVA培训笔记1(晓伟整理)
- Git 常用命令整理
- 20160429待整理
- 《理解 ES6》阅读整理:函数(Functions)(四)Arrow Functions
- 使用Base64将图片转化为字符串(后期详细整理)
- CSS一些命名整理
- 【java基础资料整理】
- [整理]常用正则表达式
- 整理 IE 7、8预览本地图片和获取本地图片大小
- hadoop作业调优参数整理及原理