Java常用的排序查找算法
public static void main(String[] args) {
// bubbleSort();
// int[] a = {20,2,10,8,12,17,4,25,11,6,33,13,24};
// int start = 0;
// int end = a.length-1;
// sort(a,start,end);
// for(int i = 0; i<a.length; i++){
// System.out.print(a[i]+",");
// }
// choseSort();
// nsertSort();
// sequential();
binarySearch();
}
/**冒泡排序*/
public static void bubbleSort(){
int[] score = {20,2,10,8,12,17,4,25,11,6,33,13,24};
//冒泡排序
for (int i = 0; i < score.length -1; i++){ //最多做n-1趟排序
for(int j = 0 ;j < score.length - i - 1; j++){
//对当前无序区间score[0......length-i-1]进行排序
//(j的范围很关键,这个范围是在逐步缩小的)
if(score[j] > score[j + 1]){ //把小的值交换到后面
int temp = score[j];
score[j] = score[j + 1];
score[j + 1] = temp;
}
}
}
System.out.print("最终排序结果:");
for(int a = 0; a < score.length; a++){
System.out.print(score[a] + ",");
}
}
/**快速排序*/
public static void sort(int[] a,int low,int high){
int start = low;
int end = high;
int key = a[low];
while(end>start){
//从后往前比较
while(end>start&&a[end]>=key) //如果没有比关键值小的,比较下一个,直到有比关键值小的交换位置,然后又从前往后比较
end--;
if(a[end]<=key){
int temp = a[end];
a[end] = a[start];
a[start] = temp;
}
//从前往后比较
while(end>start&&a[start]<=key)//如果没有比关键值大的,比较下一个,直到有比关键值大的交换位置
start++;
if(a[start]>=key){
int temp = a[start];
a[start] = a[end];
a[end] = temp;
}
//此时第一次循环比较结束,关键值的位置已经确定了。左边的值都比关键值小,右边的值都比关键值大,但是两边的顺序还有可能是不一样的,进行下面的递归调用
}
//递归
if(start>low) sort(a,low,start-1);//左边序列。第一个索引位置到关键值索引-1
if(end<high) sort(a,end+1,high);//右边序列。从关键值索引+1到最后一个
}
/**选择排序*/
public static void choseSort(){
int[] toBeSorted = {20,2,10,8,12,17,4,25,11,6,33,13,24};
for(int i = 0; i < toBeSorted.length; i++){
for(int j = i+1; j < toBeSorted.length; j++){
if(toBeSorted[i] > toBeSorted[j]){
int temp = toBeSorted[i];
toBeSorted[i] = toBeSorted[j];
toBeSorted[j] = temp;
}
}
}
for(int i = 0; i <toBeSorted.length; i++){
System.out.print(toBeSorted[i]+",");
}
}
/**插入排序*/
public static void nsertSort(){
int[] arr = {20,2,10,8,12,17,4,25,11,6,33,13,24};
int i, j;
int n = arr.length;
int target;
//假定第一个元素被放到了正确的位置上
//这样,仅需遍历1 - n-1
for (i = 1; i < n; i++){
j = i;
target = arr[i];
while (j > 0 && target < arr[j - 1]){
arr[j] = arr[j - 1];
j--;
}
arr[j] = target;
}
for(int a = 0; a < arr.length; a++){
System.out.print(arr[a] + ",");
}
}
/**顺序查找*/
public static void sequential(){
int[] arr = {20,2,10,8,12,17,4,25,11,6,33,13,24};
int key = 6;
for(int i=0;i<arr.length;i++){
if(key == arr[i]){
System.out.print("第"+(i+1)+"个");
}
}
}
/**折半查找*/
public static void binarySearch(){
int[] arr = {20,2,10,8,12,17,4,25,11,6,33,13,24};
int low = 0;
int high = arr.length - 1;
int key = 6;
while ((low <= high) && (low <= arr.length - 1)
&& (high <= arr.length - 1)) {
int middle = (high + low) >> 1;
if (key == arr[middle]) {
System.out.print("第"+(middle+1)+"个");
break;
} else if (key < arr[middle]) {
high = middle - 1;
} else {
low = middle + 1;
}
}
}
Java常用的排序查找算法的更多相关文章
- Java数据结构 遍历 排序 查找 算法实现
请查看:http://blog.csdn.net/zhanghao_hulk/article/details/35372571#t13
- 常用的STL查找算法
常用的STL查找算法 <effective STL>中有句忠告,尽量用算法替代手写循环:查找少不了循环遍历,在这里总结下常用的STL查找算法: 查找有三种,即点线面: 点就是查找目标为单个 ...
- Java进阶(三十九)Java集合类的排序,查找,替换操作
Java进阶(三十九)Java集合类的排序,查找,替换操作 前言 在Java方向校招过程中,经常会遇到将输入转换为数组的情况,而我们通常使用ArrayList来表示动态数组.获取到ArrayList对 ...
- Java学习之二分查找算法
好久没写算法了.只记得递归方法..结果测试下爆栈了. 思路就是取范围的中间点,判断是不是要找的值,是就输出,不是就与范围的两个临界值比较大小,不断更新临界值直到找到为止,给定的集合一定是有序的. 自己 ...
- Java实现的二分查找算法
二分查找又称折半查找,它是一种效率较高的查找方法. 折半查找的算法思想是将数列按有序化(递增或递减)排列,查找过程中采用跳跃式方式查找,即先以有序数列的中点位置为比较对象,如果要找的元素值小 于该中点 ...
- javascript排序 查找算法大全
在pptv的实习结束了, 忙着找工作的事,顺便把数据结构的那本书重新复习了一遍.为了加深印象,特意把里面的常用的排序.查找算法用js写了一遍 具体的实例在我的github上,大家可以访问的: http ...
- java基础---数组的查找算法(2)
一.查找的基本概念 查找分为有序查找和无序查找,这里均以数组为对象,有序查找指的是数组元素有序排列,无序查找指的是数组元素有序或无序排列 平均查找长度(Average Search Length,AS ...
- 深入JDK源码之Arrays类中的排序查找算法(转)
原文出处: 陶邦仁 binarySearch()方法 二分法查找算法,算法思想:当数据量很大适宜采用该方法.采用二分法查找时,数据需是排好序的. 基本思想:假设数据是按升序排序的,对于给定值x,从序列 ...
- Java常用的排序算法三
Merge Sort :归并排序:用递归的思想,分解成单个元素的排序,在归并 代码: import java.util.*; public class MergeSort { public stati ...
随机推荐
- Python爬虫开发【第1篇】【爬虫案例】
案例一:网站模拟登录 # douban.py from selenium import webdriver from selenium.webdriver.common.keys import Key ...
- UISlider无法拖动进度条的问题解决
UISlider无法拖动进度条的问题解决 最近业务中的视频播放使用到了UISlider,但是有一个奇怪的问题,就是在Modar出来的控制器中UISlider是可以正常使用的,但是在Push出来的控制器 ...
- <s:property>的用法(jsp获取action中的值或者方法)
1,访问Action值栈中的普通属性: <s:property value="attrName"/> 2,访问Action值栈中的对象属性(要有get set方法) ...
- What's the difference between HEAD, working tree and index, in Git?
What's the difference between HEAD, working tree and index, in Git?
- YTU 2875: 倒霉蛋买饭去
2875: 倒霉蛋买饭去 时间限制: 1 Sec 内存限制: 128 MB 提交: 22 解决: 17 题目描述 早春星期天的某个早晨,大风呼呼地刮.一个宿舍n个人,谁也不想起床买饭去.他们定了一 ...
- bash编程 将一个目录里所有文件存为一个array 并分割为三等分——利用bash array切片
files=(a b c d e f g h i j k l m n o p)cnt="${#files[@]}"let cnt1="($cnt+2)/3"le ...
- HDU4704:Sum(欧拉降幂公式)
Input 2 Output 2 Sample Input 2 由公式,ans=2^(N-1)%Mod=2^((N-1)%(Mod-1)+(Mod-1)) %Mod. 注意:降幂的之后再加一个Mod- ...
- HEOI2016 树
传送门 这道题还是很简单的,可以树剖,然后还有看大佬暴力模拟AC的????!! 我们就执行俩操作,一个是单点修改,这个随便修,然后就是查询一个点,离他最近的被打过标记过的祖先.这个可以这么想,我们先q ...
- pymemcache get start
Getting started! A comprehensive, fast, pure-Python memcached client library. Basic Usage from pymem ...
- linux WEB服务器***
Apache sudo apt-get install apache2 PHP sudo apt-get install php5 sudo apt-get install php5-gd / ...