package me.knowledge;
/**
* Created by zhongxiankui on 16-4-9.
*/
public class BubbleSortTest {
public static void bubbleSort(int[] array){
int size = array.length;
for(int i = size; i > 0; i--) {
for (int j = 0; j < i - 1; j++) {
if (array[j] > array[j + 1]) {
int tmp = array[j];
array[j] = array[j + 1];
array[j + 1] = tmp;
}
}
}
}
public static void printArray(int[] array){
for (int i = 0; i < array.length; i++){
System.out.print(array[i] + ",");
}
System.out.println();
}
public static void main(String[] args){
int[] array = new int[]{9,3,3,1,0,3,7,15,1,2};
printArray(array);
bubbleSort(array);
printArray(array);
}
}
package me.knowledge;
/**
* Created by zhongxiankui on 16-4-9.
*/
public class ChoseSortTest {
public static void choseSort(int[] array){
int size = array.length;
for(int i = 0; i < size; i++){
int minIndex = i;
int min = array[minIndex];
for(int j = i+1; j < size; j++){
if(array[j] < min){
minIndex = j;
min = array[j];
}
}
if(i != minIndex){
int tmp = array[i];
array[i] = array[minIndex];
array[minIndex] = tmp;
}
}
}
public static void printArray(int[] array){
for (int i = 0; i < array.length; i++){
System.out.print(array[i] + ",");
}
System.out.println();
}
public static void main(String[] args){
int[] array = new int[]{9,3,3,1,0,3,7,15,1,2};
printArray(array);
choseSort(array);
printArray(array);
}
}
package me.knowledge;
/**
* Created by zhongxiankui on 16-4-7.
*/
public class InsertSortTest {
public static void insertSort(int[] array){
int size = array.length;
for(int i = 1; i < size; i++){
//good method
int tmp = array[i];
int j = i;
while(--j >= 0 && array[j] > tmp){
array[j+1] = array[j];
}
array[j+1] = tmp;
/*
//bad method
for(int j = 0; j < i; j++){
if(array[j] > array[i]){
int tmp = array[i];
for (int k = i; k > j; k--){
array[k] = array[k-1];
}
array[j] = tmp;
break;
}
}
*/
}
}
public static void printArray(int[] array){
for (int i = 0; i < array.length; i++){
System.out.print(array[i] + ",");
}
System.out.println();
}
public static void main(String[] args){
int[] array = new int[]{9,3,3,1,0,3,7,15,1,2};
printArray(array);
insertSort(array);
printArray(array);
}
}
package me.knowledge;
/**
* Created by zhongxiankui on 16-4-9.
*/
public class MergeSortTest {
public static void mergeSort(int[] arrayA, int[] arrayB, int[] arrayC){
int sizeA = arrayA.length;
int sizeB = arrayB.length;
int i = 0;
int j = 0;
int k = 0;
while (i < sizeA && j < sizeB){
if(arrayA[i] <= arrayB[j]){
arrayC[k++] = arrayA[i++];
}else {
arrayC[k++] = arrayB[j++];
}
}
if(j == sizeB){
while (i < sizeA){
arrayC[k++]=arrayA[i++];
}
}
if(i == sizeA){
while (j < sizeB){
arrayC[k++]=arrayB[j++];
}
}
}
public static void printArray(int[] array){
for (int i = 0; i < array.length; i++){
System.out.print(array[i] + ",");
}
System.out.println();
}
public static void main(String[] args){
int[] arrayA = new int[]{1,3,9,15,21,29,35};
int[] arrayB = new int[]{2,6,10};
int[] arrayC = new int[arrayA.length + arrayB.length];
printArray(arrayA);
printArray(arrayB);
mergeSort(arrayA, arrayB, arrayC);
printArray(arrayC);
}
}
package me.knowledge;
/**
* Created by zhongxiankui on 16-4-9.
*/
public class QuickSortTest {
public static void quickSort(int[] array, int start, int end){
if(start >= end){
return;
}
int left = start;
int right = end;
int flagIndex = start;
int flag = array[flagIndex];
while(left < right){
while(flag < array[right] && left < right){
right--;
}
if(left < right){
flagIndex=right;
array[left] = array[right];
left++;
}
while(flag > array[left] && left < right){
left++;
}
if(left < right){
flagIndex=left;
array[right] = array[left];
right--;
}
}
array[flagIndex] = flag;
printArray(array);
quickSort(array, start, flagIndex-1);
quickSort(array,flagIndex+1, end);
/*
if(start < flagIndex -1){
quickSort(array, start, flagIndex-1);
}
if(flagIndex+1 < end){
quickSort(array,flagIndex+1, end);
}
*/
}
public static void printArray(int[] array){
for (int i = 0; i < array.length; i++){
System.out.print(array[i] + ",");
}
System.out.println();
}
public static void main(String[] args){
int[] array = new int[]{9,3,3,1,0,3,7,15,1,2};
printArray(array);
quickSort(array, 0, array.length-1);
printArray(array);
}
}
package me.knowledge;
/**
* Created by zhongxiankui on 16-4-7.
*/
public class BinaryTreeTest {
private static class Node{
public Node(int value){
this.value = value;
this.father = null;
this.left = null;
this.right = null;
}
public Node father;
public Node left;
public Node right;
public int value;
}
public static Node scanTree(Node node){
if(null == node){
return null;
}
while (null != node.left){
if(null == scanTree(node.left)){
break;
}
}
if(null == node.left){
System.out.println("get enode:" + node.value);
}
if(null != node.father && node.father.right != node){
System.out.println("get fnode:" + node.father.value);
scanTree(node.father.right);
}
return null;
}
public static void test1(){
Node node1 = new Node(1);
Node node2 = new Node(3);
node1.left = node2;
node2.father = node1;
Node node3 = new Node(5);
node1.right = node3;
node3.father = node1;
Node node4 = new Node(7);
node2.left = node4;
node4.father = node2;
Node node5 = new Node(2);
node2.right = node5;
node5.father = node2;
Node node6 = new Node(10);
node3.left = node6;
node6.father = node3;
Node node7 = new Node(20);
node4.left = node7;
node7.father = node4;
scanTree(node1);
}
public static void main(String[] args){
test1();
}
}
package me.base.knowledge.algorithmic;
/**
* @author : zhongxiankui
* @time : 2016-04,12 10:11:上午10:11
* @project
*/
public class HeapSortTest {
public static void heapSort(int[] array, int index, int num){
int i = index * 2 + 1;
int tmp = array[index];
while(i < num){
if(i + 1 < num && array[i + 1] < array[i]){
i++;
}
if(array[i] > array[index]){
break;
}
array[index] = array[i];
array[i] = tmp;
index = i;
i = index * 2 + 1;
}
}
public static void printArray(int[] array){
for (int i = 0; i < array.length; i++){
System.out.print(array[i] + ",");
}
System.out.println();
}
public static void main(String[] args){
int n = 7;
int[] array = {4,9,1,0,14,41,3};
printArray(array);
for(int i = n /2; i >=0 ; i--){
heapSort(array,i, n);
}
printArray(array);
}
}
package me.base.knowledge.algorithmic;
/**
* @author : zhongxiankui
* @time : 2016-04,14 10:18:上午10:18
*/
public class BinarySearchTest {
public static int binarySearch(int[] array, int start, int end, int value){
if(start >= end){
if(array[start] == value){
return value;
}else if(value > array[start]){
return -(start + 1);
}else {
if(start == 0){
return 0;
}else if(0 == start - 1){
return -1;
}else {
return -(start - 1);
}
}
}
int mid = (start + end) / 2;
if(value == array[mid]) {
return value;
}else if (value < array[mid]){
return binarySearch(array, start , mid - 1, value);
}else {
return binarySearch(array, mid + 1, end, value);
}
}
public static void main(String[] args){
int[] array = {1,3,5,7,9,11};
int findValue = binarySearch(array, 0, array.length - 1, 2);
System.out.println(findValue);
}
}