您的位置:首页 > 其它

基础的排序查找算法

2015-06-30 15:00 453 查看

基础的排序查找算法

冒泡排序、插入排序

快速排序

二分查找

数组合并

1、冒泡排序

package test.arithmetic;
/**
* <p>类说明: 冒泡排序。</p>
* <p>文件名: BubbleSort.java</p>
* <p>创建人及时间: zzc 2015-3-19 下午3:30:54</p>
*
* <p>修改人:</p>
* <p>修改时间:</p>
* <p>修改描述:</p>
**/
public class BubbleSort {

private int[] array = new int[]{37, 16, 48, 52, 29, 61, 15, 25, 18, 26, 55, 39};
/**
* <p>功能描述: 插入排序</p>
*/
public void insertSort() {
print(array);
int len = array.length;
for(int i=0; i<len; i++) {
for(int j=i; j>0; j--) {
//如果后面的数小于前面的数,则交互
if(array[j] < array[j-1]) {
swap(array, j, j-1);
} else {
break;
}
}
}
print(array);
}

/**
* <p>功能描述:冒泡排序。</p>
*/
public void bubble1() {
print(array);
int len = array.length;
for(int i=0; i<len; i++) {
//将最小的冒到最前边去
for(int j=i; j<len; j++) {
if(array[i] > array[j]) {
swap(array, i, j);
}
}
}
print(array);
}

public static void main(String[] args) {
BubbleSort bs = new BubbleSort();
bs.insertSort();
}

/**
* <p>功能描述: 交换。</p>
* @param array
* @param i
* @param j
*/
private void swap(int[] array, int i, int j) {
int temp = array[j];
array[j] = array[i];
array[i] = temp;
}

private void print(int[] array) {
for(int i: array) {
System.out.print(i + ", ");
}
System.out.println("--------------------------");
}
}


2、快速排序

package test.arithmetic;
/**
* <p>类说明: 快速排序。</p>
* <p>文件名: QuickSort.java</p>
* <p>创建人及时间: zzc 2015-3-20 上午10:54:38</p>
*
* <p>修改人:</p>
* <p>修改时间:</p>
* <p>修改描述:</p>
**/
public class QuickSort {

private static int[] array = new int[]{37, 16, 48, 52, 29, 61, 15, 25, 18, 26, 55, 39};

/**
* <p>功能描述:核心算法。</p>
* <p>创建人及时间: zzc 2015-3-20 上午11:13:36</p>
* @param a 数组
* @param low 最低位下标
* @param high 最高位下标
* @return 正确排位的数字 下标(这里让第一个数字排位正确)
*/
private int sortCore(int[] a, int low, int high) {
//指定排序的数字(这里默认排序第一个数字)
int firstValue = a[low];
while(low < high) {
//从最后一位开始向前遍历,如果有比指定数字小的,则进行交换
while(low<high && a[high]>firstValue) {
high--;
}
swap(a, low, high);
//从第一位开始向后遍历,如果有比指定数字大的,则进行交换
while(low<high && a[low]<firstValue) {
low++;
}
swap(a, low, high);
}
return low;
}

/**
* <p>功能描述: 快排。</p>
* <p>创建人及时间: zzc 2015-3-20 上午11:15:31</p>
* @param a
* @param low
* @param high
*/
public void quick(int[] a, int low, int high) {
if(low < high) {
int rightNum_index = sortCore(a, low, high); //排到正确位置上数字的 下标
quick(a, low, rightNum_index-1);
quick(a, rightNum_index+1, high);
}
}

public static void main(String[] args) {
QuickSort qs = new QuickSort();
qs.print(array);
qs.quick(array, 0, array.length-1);
qs.print(array);
}

/**
* <p>功能描述: 交换。</p>
* <p>创建人及时间: zzc 2015-3-20 上午11:21:43</p>
* @param array
* @param i
* @param j
*/
private void swap(int[] array, int i, int j) {
int temp = array[j];
array[j] = array[i];
array[i] = temp;
}

private void print(int[] array) {
for(int i: array) {
System.out.print(i + ", ");
}
System.out.println("--------------------------");
}
}


3、二分查找

package test.arithmetic;
/**
* <p>类说明:二分查找、折半查找算法。</p>
* <p>文件名: BinarySearch.java</p>
* <p>创建人及时间: zzc 2015-3-20 下午2:52:05</p>
*
* <p>修改人:</p>
* <p>修改时间:</p>
* <p>修改描述:</p>
**/
public class BinarySearch {

private int[] array = new int[]{15, 16, 18, 25, 26, 29, 37, 39, 48, 52, 55, 61};

/**
* <p>功能描述:算法实现</p>
* <p>创建人及时间: zzc 2015-3-20 下午2:57:30</p>
* @param num
*/
public void binary(int num) {
int low = 0;
int len = array.length-1;
while(low <= len) {
int middle = (low+len)/2;
if(array[middle] == num) {
System.out.println("您要查找的数" + num +" 是第" + middle +"位!");
return;
} else if(array[middle] < num) {
low = middle+1;
} else if(array[middle] > num) {
len = middle-1;
}
}
System.out.println("对不起,没有您要查找的数:" + num);
}

public static void main(String[] args) {
BinarySearch bs = new BinarySearch();
bs.binary(26);
}
}


4、合并两个有序数组

package test.arithmetic;

/**
* 合并两个有序数组。
* @author zzc
*
*/
public class MergeArray {

private static int[] merge(int[] arrayX, int[] arrayY) {
int i = arrayX.length + arrayY.length;
int[] res = new int[i];
int x = 0;
int y = 0;
for(int j = 0; j < i; j++) {
if(x == arrayX.length) {
res[j] = arrayY[y];
y++;
} else if(y == arrayY.length) {
res[j] = arrayX[x];
x++;
} else {
if(arrayX[x] > arrayY[y]) {
res[j] = arrayX[x];
x++;
}else {
res[j] = arrayY[y];
y++;
}
}
}
return res;
}

public static void main(String[] args) {
int[] x = {6, 3, 1};
int[] y = {7, 5, 2};
int[] res = merge(x, y);
for (int i = 0; i < res.length; i++) {
System.out.println(res[i]);
}
System.out.println(Math.abs(10.5));
System.out.println(Math.floor(10.3));
System.out.println(Math.round(10.4));
System.out.println(Math.ceil(10.5));
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: