冒泡,选择,二分查询之java实现
2016-03-08 16:36
501 查看
1.选择法排序
转载出自:http://blog.csdn.net/wentasy/article/details/7438064
选择法排序的基本思想是:首先从待排序的n个数中找出最小的一个与array[0]对换;再将array [1]到array
中的最小数与array [1]对换,依此类推。每比较一轮,找出待排序数中最小的一个数进行交换,共进行n-1次交换便可完成排序。选择法排序每执行一次外循环只进行一次数组元素的交换,可使交换的次数大大减少。
下图演示这一过程:
![](http://my.csdn.net/uploads/201204/08/1333874794_6907.jpg)
代码实现:
[java] view
plaincopy
public class SelectSort{
public static void main(String [] args){
int a[] = {1, 2, 3, 56, 45, 22, 22, 26, 89, 99, 100};
System.out.println("排序前:");
for (int i = 0; i < a.length; ++ i){
System.out.print(a[i] + " ");
}
selectSort(a);
System.out.println("\n");
System.out.println("排序后:");
for (int i = 0; i < a.length; ++ i){
System.out.print(a[i] + " ");
}
}
public static void selectSort(int a[]){
int min = 0;
int temp = 0;
for (int i = 0; i < a.length - 1; ++ i){
min = i;
for (int j = i + 1; j < a.length; ++ j){
if (a[min] > a[j]){
min = j;
}
}
if (min != i){
temp = a[min];
a[min] = a[i];
a[i] = temp;
}
}
}
}
执行效果如图:
![](http://my.csdn.net/uploads/201204/08/1333874798_1092.jpg)
2.冒泡排序
冒泡排序的关键点是从后向前对相邻的两个数组元素进行比较,若后面元素的值小于前面元素的值,则将这两个元素交换位置,否则不进行交换。依次进行下去,第一趟排序可将数组中值最小的元素移至下标为0的位置。对于有n个元素的数组,循环执行n-1趟扫描便可完成排序。(当然,也可以从前向后对相邻的两个数组元素进行比较,但此时应注意将大数向后移,与小者前移的冒泡法相对应,可将这种大者后移的排序称为下沉法)。
下图演示了有6个元素的数组实施冒泡法排序(小数前移)的前两趟比较与交换过程。可以看出,第一趟排序后最小数12已移到了下标为0的正确位置;第二趟排序后次小数17移到了下标为1的正确位置。
![](http://my.csdn.net/uploads/201204/08/1333876365_1497.jpg)
代码实现:
[java] view
plaincopy
public class BubbleSort{
public static void main(String[] args){
int a[] = {1, 23, 45, 6, 0, 99, 100, 89, 34, 56};
System.out.println("排序前:");
for (int i = 0; i < a.length; ++ i)
System.out.print(a[i] + " ");
bubbleSort(a);
System.out.println();
System.out.println("排序后:");
for (int i = 0; i < a.length; ++ i)
System.out.print(a[i] + " ");
}
public static void bubbleSort(int a[]){
int temp = 0;
for (int i = 0; i < a.length - 1; ++ i){
for (int j = i; j < a.length; ++ j){
if (a[i] > a[j]){
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
}
}
执行效果如图:
![](http://my.csdn.net/uploads/201204/08/1333876294_3609.jpg)
3.二分查询
算法剖析:
二分查找是在一个有序表(数据是按其值由小到大或由大到小依次存放的,这里我们以值由小到大排列为例)中,每次都与中间的那个元素比较,若相等则查找成功;否则,调整查找范围,若中间那个元素的值小于待查值,则在表的后一半中查找;若中间那个元素的值大于待查值,则在表的前一半中查找;如此循环,每次只与一半中的一个元素比较,可使查找效率大大提高。
![](http://my.csdn.net/uploads/201204/13/1334299270_1581.jpg)
代码:
[java] view
plaincopy
import java.util.*;
public class FindSearch{
public static void main(String [] args){
int a[] = {2, 4, 7, 18, 25, 34, 56, 68, 89};
System.out.println("打印原始数据:");
for (int i = 0; i < a.length; ++ i){
System.out.print(a[i] + " ");
}
System.out.println();
System.out.println("请输入要查找的整数:");
Scanner scan = new Scanner(System.in);
int num = scan.nextInt();
int pos = 0;
pos = binaryFind(a, num);
if (-1 != pos)
System.out.println("所查整数在数组中的位置下标是:" + pos);
else
System.out.println("没找到待查数据!");
}
public static int binaryFind(int a[],int num){
int low, mid, high;
low = 0;//low是第一个数组元素的下标
high = a.length - 1;//high是最后一个数组元素的下标
mid = (low + high) / 2;//mid是中间那个数组元素的下标
while (low <= high){
if (num == a[mid]){
return mid;
}else if (num > a[mid]){
low = mid + 1;//要找的数可能在数组的后半部分中
mid = (low + high) / 2;
}else{
high = mid - 1;//要找的数可能在数组的前半部分中
mid = (low + high) / 2;
}
}
//mid是数组元素下标,若为-1则表示不存在要查的元素
if (mid != ((low + high) / 2))
return mid;
else
return -1;
}
}
运行效果截图:
转载出自:http://blog.csdn.net/wentasy/article/details/7438064
选择法排序的基本思想是:首先从待排序的n个数中找出最小的一个与array[0]对换;再将array [1]到array
中的最小数与array [1]对换,依此类推。每比较一轮,找出待排序数中最小的一个数进行交换,共进行n-1次交换便可完成排序。选择法排序每执行一次外循环只进行一次数组元素的交换,可使交换的次数大大减少。
下图演示这一过程:
![](http://my.csdn.net/uploads/201204/08/1333874794_6907.jpg)
代码实现:
[java] view
plaincopy
public class SelectSort{
public static void main(String [] args){
int a[] = {1, 2, 3, 56, 45, 22, 22, 26, 89, 99, 100};
System.out.println("排序前:");
for (int i = 0; i < a.length; ++ i){
System.out.print(a[i] + " ");
}
selectSort(a);
System.out.println("\n");
System.out.println("排序后:");
for (int i = 0; i < a.length; ++ i){
System.out.print(a[i] + " ");
}
}
public static void selectSort(int a[]){
int min = 0;
int temp = 0;
for (int i = 0; i < a.length - 1; ++ i){
min = i;
for (int j = i + 1; j < a.length; ++ j){
if (a[min] > a[j]){
min = j;
}
}
if (min != i){
temp = a[min];
a[min] = a[i];
a[i] = temp;
}
}
}
}
执行效果如图:
![](http://my.csdn.net/uploads/201204/08/1333874798_1092.jpg)
2.冒泡排序
冒泡排序的关键点是从后向前对相邻的两个数组元素进行比较,若后面元素的值小于前面元素的值,则将这两个元素交换位置,否则不进行交换。依次进行下去,第一趟排序可将数组中值最小的元素移至下标为0的位置。对于有n个元素的数组,循环执行n-1趟扫描便可完成排序。(当然,也可以从前向后对相邻的两个数组元素进行比较,但此时应注意将大数向后移,与小者前移的冒泡法相对应,可将这种大者后移的排序称为下沉法)。
下图演示了有6个元素的数组实施冒泡法排序(小数前移)的前两趟比较与交换过程。可以看出,第一趟排序后最小数12已移到了下标为0的正确位置;第二趟排序后次小数17移到了下标为1的正确位置。
![](http://my.csdn.net/uploads/201204/08/1333876365_1497.jpg)
代码实现:
[java] view
plaincopy
public class BubbleSort{
public static void main(String[] args){
int a[] = {1, 23, 45, 6, 0, 99, 100, 89, 34, 56};
System.out.println("排序前:");
for (int i = 0; i < a.length; ++ i)
System.out.print(a[i] + " ");
bubbleSort(a);
System.out.println();
System.out.println("排序后:");
for (int i = 0; i < a.length; ++ i)
System.out.print(a[i] + " ");
}
public static void bubbleSort(int a[]){
int temp = 0;
for (int i = 0; i < a.length - 1; ++ i){
for (int j = i; j < a.length; ++ j){
if (a[i] > a[j]){
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
}
}
执行效果如图:
![](http://my.csdn.net/uploads/201204/08/1333876294_3609.jpg)
3.二分查询
算法剖析:
二分查找是在一个有序表(数据是按其值由小到大或由大到小依次存放的,这里我们以值由小到大排列为例)中,每次都与中间的那个元素比较,若相等则查找成功;否则,调整查找范围,若中间那个元素的值小于待查值,则在表的后一半中查找;若中间那个元素的值大于待查值,则在表的前一半中查找;如此循环,每次只与一半中的一个元素比较,可使查找效率大大提高。
![](http://my.csdn.net/uploads/201204/13/1334299270_1581.jpg)
代码:
[java] view
plaincopy
import java.util.*;
public class FindSearch{
public static void main(String [] args){
int a[] = {2, 4, 7, 18, 25, 34, 56, 68, 89};
System.out.println("打印原始数据:");
for (int i = 0; i < a.length; ++ i){
System.out.print(a[i] + " ");
}
System.out.println();
System.out.println("请输入要查找的整数:");
Scanner scan = new Scanner(System.in);
int num = scan.nextInt();
int pos = 0;
pos = binaryFind(a, num);
if (-1 != pos)
System.out.println("所查整数在数组中的位置下标是:" + pos);
else
System.out.println("没找到待查数据!");
}
public static int binaryFind(int a[],int num){
int low, mid, high;
low = 0;//low是第一个数组元素的下标
high = a.length - 1;//high是最后一个数组元素的下标
mid = (low + high) / 2;//mid是中间那个数组元素的下标
while (low <= high){
if (num == a[mid]){
return mid;
}else if (num > a[mid]){
low = mid + 1;//要找的数可能在数组的后半部分中
mid = (low + high) / 2;
}else{
high = mid - 1;//要找的数可能在数组的前半部分中
mid = (low + high) / 2;
}
}
//mid是数组元素下标,若为-1则表示不存在要查的元素
if (mid != ((low + high) / 2))
return mid;
else
return -1;
}
}
运行效果截图:
![](http://my.csdn.net/uploads/201204/13/1334299264_7153.jpg)
相关文章推荐
- java对世界各个时区(TimeZone)的通用转换处理方法(转载)
- java-注解annotation
- java-模拟tomcat服务器
- java-用HttpURLConnection发送Http请求.
- java-WEB中的监听器Lisener
- Android IPC进程间通讯机制
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- 介绍一款信息管理系统的开源框架---jeecg
- 聚类算法之kmeans算法java版本
- java实现 PageRank算法
- PropertyChangeListener简单理解
- c++11 + SDL2 + ffmpeg +OpenAL + java = Android播放器
- 插入排序
- 冒泡排序
- 堆排序
- 快速排序
- 二叉查找树