常见的数据结构和算法(上)
2017.05.25
codewindy
 热度
℃
TreeMap 源码里面有红黑树,要学会自我总结和归纳知识,反思避免错误
红黑树BST的实现
具体图形化帮助理解请参考,旧金山大学usfca的算法演示
从简单的开始BubbleSort–>SelectSort–>BinarySort–>QuickSort–>DualPivotQuickSort
HashMap(自己实现一个简单的)–>TreeMap–>Red_Black_Tree
一. 冒泡排序BubbleSort[2种方式实现]
package com.codewindy.sorting; public class Demo1_BubbleSort { public static void main(String[] args) { int[] arr = { 66, 11, 22, 33, 1, 55 }; bubbleSort(arr); System.out.println(arr2String(arr)); }
public static void selectSort(int[] arr){ for (int i = 0; i < arr.length-1; i++) {
for (int j = 0; j < arr.length-1-i; j++) {
if(arr[j]>arr[j+1]){
Demo1_BubbleSort.swap(arr,j, j+1); } } } } public static void bubbleSort(int[] arr) { boolean swapped = true; int j = 0; int temp; while (swapped) { swapped = false; j++; for (int i = 0; i < arr.length - j; i++) { if (arr[i] > arr[i + 1]) { temp = arr[i]; arr[i] = arr[i + 1]; arr[i + 1] = temp; swapped = true; } } } }
public static String arr2String(int[] arr) { StringBuilder sb = new StringBuilder(); sb.append("["); for (int i = 0; i < arr.length; i++) { if (i == arr.length - 1) { sb.append(arr[i]).append("]"); } else { sb.append(arr[i]).append(", "); } } return sb.toString(); }
public static void swap(int[] arr, int i, int j) { int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } }
|
二. 选择排序的简单实现
package com.codewindy.sorting; public class Demo2_selectSort { public static void main(String[] args){
int[] arr={66,55,44,33,22,11}; selectSort(arr); String arr2String = Demo1_BubbleSort.arr2String(arr); System.out.println("选择排序打印输出结果:" +arr2String); }
public static void selectSort(int[] arr){ for (int i = 0; i < arr.length-1; i++) {
for (int j = i+1; j < arr.length; j++) {
if(arr[i]>arr[j]){
Demo1_BubbleSort.swap(arr, i, j); } } } } }
|
三 .BinarySearch[Arrays参考源码]二分查找法
package com.codewindy.sorting; public class Demo3_BinarySearch { public static void main(String[] args) { int[] arr = { 11, 22, 33, 44, 55, 66, 77 }; System.out.println("--------------通过jdk方式实现--------------------"); System.out.println(binarySearch1(arr, 22)); System.out.println(binarySearch1(arr, 66)); System.out.println(binarySearch1(arr, 888)); } public static int binarySearch1(int[] a, int key) { return binarySearch0(a, 0, a.length, key); } private static int binarySearch0(int[] a, int fromIndex, int toIndex, int key) { int low = fromIndex; int high = toIndex - 1; while (low <= high) { int mid = (low + high) >>> 1; int midVal = a[mid]; if (midVal < key) low = mid + 1; else if (midVal > key) high = mid - 1; else return mid; } return -(low + 1); }
public static int binarySearch(int[] arr, int key) { int min = 0; int max = arr.length - 1; int mid = (max + min) >>> 1; while (arr[mid] != key) { if (arr[mid] > key) { max = mid - 1; } else if (key > arr[mid]) { min = mid + 1; } if (min > max) { return -1; } mid = (max + min) >>> 1; } return mid; } }
|
四 .快速排序是对冒泡排序的优化
思想是: 先取数组的pivot left /right 然后进行刷选,数组中 <pivot 的值往左边丢, > pivot 的数组的值往右边丢,最终递归实现排序nlog2N 的时间复杂度和 二叉树的效率基本类似
package com.codewindy.sorting; import java.util.Arrays; public class Demo08_MyquickSort { public static void main(String[] args) { int[] arr={7,5,3,2,5,8,98,3}; quicksort(arr, 0, arr.length-1); System.out.println(arr2String(arr)); }
public static String arr2String(int[] arr){ if(arr==null) return "null"; int iMax=arr.length-1; if(iMax==-1) return "[]";
StringBuilder sb = new StringBuilder(); sb.append("["); for (int i = 0; ; i++) { sb.append(arr[i]); if(i==iMax){ return sb.append("]").toString(); } sb.append(", "); } }
public static void swap(int[] arr,int l, int r){ int temp=arr[l]; arr[l]=arr[r]; arr[r]=temp;
}
public static void quicksort(int[] arr,int left,int right){
int l=left; int r=right;
int pivot=arr[(l+r)>>1]; while(l<r){ while(arr[l]<pivot) l++; while(pivot<arr[r]) r--;
if(l>=r) break;
swap(arr, l, r);
if(arr[l]==pivot) --r; if(arr[r]==pivot) ++l; } if(l==r){ l++; r--;
}
if(left<r) quicksort(arr, left, r); if(l<right) quicksort(arr,l,right);
}
}
package jp.codewindy.producer.consumer;
import java.util.LinkedList; import java.util.concurrent.TimeUnit; import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock;
public class MyContainer<T> { final private LinkedList<T> lists = new LinkedList<>(); final private int MAX = 10; private int count = 0;
private Lock lock = new ReentrantLock(); private Condition producer = lock.newCondition(); private Condition consumer = lock.newCondition();
public void put(T t) { try { lock.lock(); while(lists.size() == MAX) { producer.await(); }
lists.add(t); ++count; consumer.signalAll(); } catch (InterruptedException e) { e.printStackTrace(); } finally { lock.unlock(); } }
public T get() { T t = null; try { lock.lock(); while(lists.size() == 0) { consumer.await(); } t = lists.removeFirst(); count --; producer.signalAll();
} catch (InterruptedException e) { e.printStackTrace(); } finally { lock.unlock(); } return t; }
public static void main(String[] args) { MyContainer<String> c = new MyContainer<>(); for(int i=0; i<10; i++) { new Thread(()->{ for(int j=0; j<5; j++) System.out.println(c.get()); }, "c" + i).start(); }
try { TimeUnit.SECONDS.sleep(2); } catch (InterruptedException e) { e.printStackTrace(); }
for(int i=0; i<2; i++) { new Thread(()->{ for(int j=0; j<25; j++) c.put(Thread.currentThread().getName() + " " + j); }, "p" + i).start(); } } }
|