一些知识点总结(HeapSort, MergeSort, QuickSort, PrefixTree, TopologicalSort)

HeapSort

package RandomPractice;

public class HeapSort {
	public static int heapSize = 0;
	public static void buildHeap(int[] A){
		heapSize = A.length;
		for(int i = A.length / 2 - 1; i >= 0; i--)
			MaxHeapify(A, i);
	}
	
	public static void MaxHeapify(int[] A, int i){
		int left = 2 * i + 1;
		int right = 2 * (i + 1);
		int largest;
		if(left < heapSize && A[left] > A[i])
			largest = left;
		else
			largest = i;
		if(right < heapSize && A[right] > A[i])
			largest = right;
		if(largest != i){
			swap(A, i, largest);
			MaxHeapify(A, largest);
		}
	}
	
	public static void swap(int[] A, int i, int j){
		int temp = A[i];
		A[i] = A[j];
		A[j] = temp;
	}
	
	public static void sort(int[] A){
		buildHeap(A);
		for(int i = A.length - 1; i > 0; i--){
			swap(A, 0, i);
			heapSize--;
			MaxHeapify(A, 0);
		}
	}
	
	public static void main(String[] args){
		int[] A = {8, 5, 6, 3, 4, 0};
		sort(A);
		for(int i = 0; i < A.length; i++)
			System.out.print(A[i]);
	}
}

MergeSort

public class MergeSort {
	public static void main(String[] args) {
		int[] inputArray = { 5, 4, 7, 3, 1 };
		mergeSort(inputArray, 0, inputArray.length - 1);
		for (int j = 0; j < inputArray.length; j++)
			System.out.println(inputArray[j] + " ");
	}

	public static void mergeSort(int[] array, int low, int high) {
		if (low < high) {
			int mid = low + (high - low) / 2;
			mergeSort(array, low, mid);
			mergeSort(array, mid + 1, high);
			merge(array, low, mid, high);
		}
	}

	public static void merge(int[] array, int low, int mid, int high) {
		int[] temp = new int[high - low + 1];
		int i = low, j = mid + 1, k = 0;
		while(i <= mid && j <= high){
			if(array[i] <= array[j])
				temp[k++] = array[i++];
			else
				temp[k++] = array[j++];
		}
		while(i <= mid)
			temp[k++] = array[i++];
		while(j <= high)
			temp[k++] = array[j++];
		for(k = low; k <= high; k++)
			array[k] = temp[k - low];
	}
}

QuickSort

package RandomPractice;

public class QuickSort {
	public static void swap(int A[], int x, int y) {
		int temp = A[x];
		A[x] = A[y];
		A[y] = temp;
	}

	public static int partition(int A[], int p, int r) {
		int pivot = A[r];
		int i = p - 1;
		for(int j = p; j < r; j++){
			if(A[j] <= pivot){
				i++;
				swap(A, i, j);
			}
		}
		swap(A, i + 1, r);
		return i + 1;
	}

	public static void Quicksort(int A[], int p, int r) {
		if (p < r) {
			int pivot_index = partition(A, p, r);
			Quicksort(A, p, pivot_index - 1);
			Quicksort(A, pivot_index + 1, r);
		}
	}

	public static void main(String args[]) {
		int A[] = { 6, 5, 8, 3, 4, 9 };
		Quicksort(A, 0, A.length - 1);
		for (int i = 0; i < A.length; i++)
			System.out.print(A[i] + " ");
	}
}

Prefix Tree (Trie Tree)

package RandomPractice;

public class PrefixTree {
	public static TrieNode createTree() {
		return new TrieNode('\0', false);
	}

	public static void insertWord(TrieNode root, String word) {
		int offset = 97;
		char[] letters = word.toCharArray();
		TrieNode curNode = root;
		for (int i = 0; i < word.length(); i++) {
			int pos = letters[i] - offset;
			if (curNode.links[pos] == null)
				curNode.links[pos] = new TrieNode(letters[i], false);
			curNode = curNode.links[pos];
		}
		curNode.isWord = true;
	}

	public static boolean find(TrieNode root, String word) {
		char[] letters = word.toCharArray();
		int offset = 97;
		TrieNode curNode = root;
		int i = 0;
		for (; i < word.length(); i++) {
			if (curNode == null)
				return false;
			curNode = curNode.links[letters[i] - offset];
		}
		if (curNode != null && curNode.isWord)
			return true;
		return false;
	}

	public static void printTree(TrieNode root, int level, char[] branch) {
		if (root == null)
			return;

		for (int i = 0; i < root.links.length; i++) {
			branch[level] = root.letter;
			printTree(root.links[i], level + 1, branch);
		}

		if (root.isWord) {
			for (int j = 1; j <= level; j++)
				System.out.print(branch[j]);
			System.out.println();
		}
	}

	public static void main(String[] args) {
		TrieNode root = PrefixTree.createTree();
		PrefixTree.insertWord(root, "phone");
		PrefixTree.insertWord(root, "abc");
		System.out.println(PrefixTree.find(root, "phone"));
		System.out.println(PrefixTree.find(root, "phoned"));
		System.out.println(PrefixTree.find(root, "h"));
		PrefixTree.printTree(root, 0, new char[50]);
	}
}
package RandomPractice;

public class TrieNode {
	char letter;
	TrieNode[] links;
	boolean isWord;
	TrieNode(char letter, boolean isWord){
		this.letter = letter;
		this.isWord = isWord;
		links = new TrieNode[26];
	}
}

Topological Sort

你可能感兴趣的:(一些知识点总结(HeapSort, MergeSort, QuickSort, PrefixTree, TopologicalSort))