线程死锁,算法,排序算法

moon1313 贡献于2016-06-16

作者 dell  创建于2016-05-06 08:50:00   修改者Administrator  修改于2016-06-01 01:15:00字数3913

文档摘要:
关键词:

一. 线程死锁 public class ThreadLock implements Runnable{ private static Object o1 = new Object(); private static Object o2 = new Object();; private int flag_num; public ThreadLock(int num){ this.flag_num = num; } public void run() { if(this.flag_num == 1){ synchronized (o1) { System.out.println("o1锁定"); try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } synchronized (o2) { System.out.println("o2锁定 结束"); } } }else if(this.flag_num == 0){ synchronized (o2) { System.out.println("o2 锁定"); try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } synchronized (o1) { System.out.println("o1锁定 结束"); } } }else{ System.out.println("wrong"); return; } } public static void main(String[] args) { ThreadLock t1 = new ThreadLock(1); ThreadLock t2 = new ThreadLock(0); Thread t11 = new Thread(t1); Thread t22 = new Thread(t2); t11.start(); t22.start(); } } 二. 各种排序 1. 插入排序 /** * 插入排序 * 时间复杂度为O(N^2) */ public static int[] insertSort(int array[]){ if(array.length == 1){ return array; }else if(array.length == 0){ System.out.println("array is null"); return null; } for(int i = 1;i=0;j--){ //比较过程中插入值和排好序的数组依次比较 if(array[j] > insert_Value){ array[j+1] = array[j]; array[j] = insert_Value; }else{ break; } } } return array; } 2. 希尔排序(缩小增量排序) public static int[] shellSort(int array[]){ if(array.length == 1){ return array; }else if(array.length == 0){ System.out.println("array is null"); return null; } boolean flag = true; int d1 = array.length/2; while(flag){ for(int i = 0;iarray[i+d1]){ array[i] = array[i+d1]; array[i+d1] = temp; } } d1 = d1/2; if(d1%2 == 0){ d1 += 1; } if(d1 == 1){ flag = false; } } //最后进行插入排序 return insertSort(array); } 3.冒泡排序 public static int[] maoPaoSort(int array[]){ for(int i = 0;iarray[j+1]){ int temp = array[j]; array[j] = array[j+1]; array[j+1] = temp; } } } return array; } 4. 选择排序 public static int[] easyChooseSort(int array[]){ for(int i = 0;i array[j]){ smallIndex = j; } } if(smallIndex != i){ int temp = array[i]; array[i] = array[smallIndex]; array[smallIndex] = temp; } } return array; } 5. 二分排序Binary Sort public static void halfSort(int[] array) {     int low, high, mid;     int tmp, j;     for (int i = 1; i < array.length; i++) {       tmp = array[i];       low = 0;       high = i - 1;       while (low <= high) {         mid = low + (high - low) / 2;         if (array[mid] > tmp)           high = mid - 1;         else           low = mid + 1;       }       for (j = i - 1; j > high; j--) {         array[j + 1] = array[j];       }       array[high + 1] = tmp;     }   } 三. 常用算法 1.递推法:求k(1,2,3,4,5....,n)得阶乘的所有数字 public static void recursive_1(int k){ if(0 == k){ System.out.println("1 return"); return; } int temp = 1; for(int i = 1;i<=k;i++){ temp = temp*i; System.out.println(temp); } } 2. 递归法:求K!和 斐波那契数列 public static int recursion_1(int n){ if(0 == n || 1 == n){ return 1; }else{ return n*recursion_1(n-1); } } public static int feiBoNaQie(int n){ if(0 == n || 1 == n){ return 1; }else if(n>1){ return feiBoNaQie(n-1)+feiBoNaQie(n-2); }else{ return 0; } } 3. 分治法:实现二分查找 public static int TwoFen(int data[],int target,int beginIndex,int endIndex){ int midIndex = (beginIndex+endIndex)/2; if(beginIndex > endIndex || midIndex > endIndex || beginIndex > midIndex){ return -1; } if(data[midIndex] > target){ return TwoFen(data,target,beginIndex,midIndex); }else if(data[midIndex] < target){ return TwoFen(data,target,midIndex,endIndex); }else{ return midIndex; } } 4.动态规划:求最大子段 public static List maxSubSection(int[] data,int j,int bj_1,List resultSet){ int temp[] = new int[3]; //第一次 if(0 == j){ temp[0] = 0; temp[1] = 0; temp[2] = data[j]; resultSet.add(temp); return maxSubSection(data,j+1,data[j],resultSet); } // temp 起始位置,终了位置,和的最大值,当上一次的bj<0则从当前bj开始 if(bj_1 < 0 && j < data.length-1){ temp[2] = data[j]; temp[0] = j; temp[1] = j; resultSet.add(temp); return maxSubSection(data,j+1,data[j],resultSet); }else if(bj_1 > 0 && j < data.length-1){ int bj = data[j] + bj_1; temp[2] = bj; temp[1] = j; int[] t = (int[]) resultSet.get(j-1); temp[0] = t[0]; resultSet.add(temp); return maxSubSection(data,j+1,bj,resultSet); }else if(j == data.length-1){ //排序 Collections.sort(resultSet,new Comparator(){ public int compare(int[] arg0, int[] arg1) { return -(arg0[2]-arg1[2]); } }); return resultSet; }else{ System.out.println("wrong"); return null; } }

下载文档到电脑,查找使用更方便

文档的实际排版效果,会与网站的显示效果略有不同!!

需要 10 金币 [ 分享文档获得金币 ] 0 人已下载

下载文档