java常用算法的原理_Java常用算法

(0)回调

01.定义回调接口和事件

02.实现回调接口

03.在运行类传入回调接口

04.调起类传入 实现的回调接口对象

1.冒泡排序

原理:依次递归,两两交换

int[] ss=new int[]{11,43,45,12,34,45,13,99,9,1};for (int i = 0; i < ss.length; i++) {for (int j = 0; j

if(ss[j]>ss[j+1]){int s=ss[j];//遍历两两交换

ss[j]=ss[j+1];

ss[j+1]=s;

}

}

}

2.快速排序

原理:选取一个基值,把数小的放在基值左边,大的放在基值右边,如此后再分治递归

if(i

int pivot =quickSort(array, i, j);//分治递归

partation(array, i, pivot-1);

partation(array, pivot+1, j);

}private static int quickSort(int[] array,int i, intj) {//一般默认设置基值为首位的值

int pivot =array[i];while(i

while(ipivot) {

j--;

}

array[i]=array[j];//当基值大于第一位的值,则将i的值加1,得到第一个比基值大的值,然后覆盖

while(i

i++;

}

array[j]=array[i];

}

array[i]= pivot; //位置换成基值,左边比基值小,右边比基值大

returni;

}

3.二分法

原理:每次从最大数和最小数的中小开始往左右遍历,在遍历过程中不断缩小low和high

public static int BinarySearch(int[] array, intT)

{intlow, high, mid;

low= 0;

high= array.length - 1;while (low <=high)

{

mid= (low + high) / 2; //重新计算mid

if (array[mid]

{

low= mid + 1;

}else if (array[mid]>T)

{

high= mid - 1;

}else{returnmid;

}

}return -1;

}

4.二叉树递归

bb7e500ab83de6fb5e3a35309fe79741.png

先序遍历:

1 2 4 8 9 5 3 6 7

中序遍历:

8 4 9 2 5 1 6 3 7

后序遍历:

8 9 4 5 2 6 7 3 1

/*** 先序遍历

**/

public static voidpreOrderTraverse(Node node) {if (node == null)return;

System.out.print(node.data+ " ");

preOrderTraverse(node.leftChild);

preOrderTraverse(node.rightChild);

}/*** 中序遍历

**/

public static voidinOrderTraverse(Node node) {if (node == null)return;

inOrderTraverse(node.leftChild);

System.out.print(node.data+ " ");

inOrderTraverse(node.rightChild);

}/*** 后序遍历

**/

public static voidpostOrderTraverse(Node node) {if (node == null)return;

postOrderTraverse(node.leftChild);

postOrderTraverse(node.rightChild);

System.out.print(node.data+ " ");

}

5.二叉树非递归

//先序遍历非递归

public static voidpreOrder2(BinTree t) {

Stack s = new Stack();while (t != null || !s.empty()) {while (t != null) {

System.out.print(t.date);

s.push(t);//入栈

t =t.lchild;

}if (!s.empty()) {

t= s.pop(); //获取出栈元素后出栈

t =t.rchild;

}

}

}//中序遍历非递归

public static voidInOrder2(BinTree t) {

Stack s = new Stack();while (t != null || !s.empty()) {while (t != null) {

s.push(t);

t=t.lchild;

}if (!s.empty()) {

t=s.pop();

System.out.print(t.date);

t=t.rchild;

}

}

}//后序遍历非递归

public static voidPostOrder2(BinTree t) {

Stack s = new Stack();

Stack s2 = new Stack();

Integer i= new Integer(1);while (t != null || !s.empty()) {while (t != null) {

s.push(t);

s2.push(new Integer(0));

t=t.lchild;

}while (!s.empty() &&s2.peek().equals(i)) {

s2.pop();

System.out.print(s.pop().date);

}if (!s.empty()) {

s2.pop();

s2.push(new Integer(1));

t=s.peek();

t=t.rchild;

}

}

}

302

public String firstRequest(String url) throwsIOException {

System.out.println("访问地址:" +url);

URL serverUrl= newURL(url);

HttpURLConnection conn=(HttpURLConnection) serverUrl

.openConnection();

conn.setRequestMethod("GET");//必须设置false,否则会自动redirect到Location的地址

conn.setInstanceFollowRedirects(false);

conn.addRequestProperty("Accept-Charset", "UTF-8;");

conn.addRequestProperty("User-Agent","Mozilla/5.0 (Windows NT 6.1; WOW64; rv:49.0) Gecko/20100101 Firefox/49.0");

conn.addRequestProperty("Referer", "http://jenkins.rnd.meizu.com/login?from=%2F");

conn.connect();

System.out.println(conn.getHeaderField(7));//根据返回header中key组数,获取登录cookie

String[] cookies = conn.getHeaderField(7).split("\\;");

String cookie= cookies[0];

String location= conn.getHeaderField("Location");

serverUrl= newURL(location);

conn=(HttpURLConnection) serverUrl.openConnection();

conn.setRequestMethod("GET");

conn.addRequestProperty("Accept-Charset", "UTF-8;");

conn.addRequestProperty("User-Agent","Mozilla/5.0 (Windows NT 6.1; WOW64; rv:49.0) Gecko/20100101 Firefox/49.0");

conn.addRequestProperty("Referer", "http://jenkins.rnd.meizu.com/login?from=%2F");

conn.connect();

System.out.println("跳转地址:" +location);return(cookie);

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值