冒泡排序
从前面开始比较
private static void bubbleSort(int[] sortNum){
int temp = 0;
for (int i = 0; i < sortNum.length-1; i++) {
for (int j = 0; j < sortNum.length-1-i; j++) {
if(sortNum[j+1]<sortNum[j]){
temp = sortNum[j];
sortNum[j] = sortNum[j+1];
sortNum[j+1] = temp;
}
}
}
}
从后面开始比较
private static void bubbleSort2(int[] sortNum){
int temp = 0;
for (int i = 0; i < sortNum.length-1; i++) {
for (int j = sortNum.length-1; j > i; j--) {
if(sortNum[j-1]>sortNum[j]){
temp = sortNum[j-1];
sortNum[j-1] = sortNum[j];
sortNum[j] = temp;
}
}
}
}
添加flag在while循环,判断无冒泡交换则退出for循环,提高程序效率
private static void bubbleSort3(int[] sortNum){
boolean flag = true;
while(flag){
flag = false;
int temp = 0;
for (int i = 0; i < sortNum.length-1; i++) {
for (int j = 0; j < sortNum.length-1-i; j++) {
if(sortNum[j+1]<sortNum[j]){
temp = sortNum[j];
sortNum[j] = sortNum[j+1];
sortNum[j+1] = temp;
flag = true;
}
}
if(!flag){
break;
}
}
}
}
二分查找法
- 在一个排序后的数组中查找一个数值的索引位置,每次仅查找数组的一半,返回-1表示未找到
递归方式
public static int binSearch(int srcArray[], int start, int end, int key) {
int mid = (end - start) / 2 + start;
if (srcArray[mid] == key) {
return mid;
}
if (start >= end) {
return -1;
} else if (key > srcArray[mid]) {
return binSearch(srcArray, mid + 1, end, key);
} else if (key < srcArray[mid]) {
return binSearch(srcArray, start, mid - 1, key);
}
return -1;
}
while循环方式
public static int binSearch(int srcArray[], int key) {
int mid;
int start = 0;
int end = srcArray.length - 1;
while (start <= end) {
mid = (end - start) / 2 + start;
if (key < srcArray[mid]) {
end = mid - 1;
} else if (key > srcArray[mid]) {
start = mid + 1;
} else {
return mid;
}
}
return -1;
}
二叉树
构建二叉树
public class BinTreeTraverse {
private int arr[]={1,2,3,4,5,6,7};
private static List<Node> nodeList=null;
private static class Node{
Node leftchild;
Node rightchild;
int data;
Node(int newdata){
leftchild=null;
rightchild=null;
data=newdata;
}
}
public void createBintree(){
nodeList=new LinkedList<Node>();
for (int nodIndex = 0; nodIndex < arr.length; nodIndex++) {
nodeList.add(new Node(arr[nodIndex]));
}
for (int parentIndex = 0; parentIndex < arr.length/2-1; parentIndex++) {
nodeList.get(parentIndex).leftchild=nodeList.get(parentIndex*2+1);
nodeList.get(parentIndex).rightchild=nodeList.get(parentIndex*2+2);
}
int lastParentIndex=arr.length/2-1;
nodeList.get(lastParentIndex).leftchild=nodeList.get(lastParentIndex*2+1);
if (arr.length%2==1) {
nodeList.get(lastParentIndex).rightchild=nodeList.get(lastParentIndex*2+2);
}
}
先序遍历
public static void preOrderTraverse(Node node) {
if (node == null)
return;
System.out.print(node.data + " ");
preOrderTraverse(node.leftchild);
preOrderTraverse(node.rightchild);
}
中序遍历
public static void inOrderTraverse(Node node) {
if (node == null)
return;
inOrderTraverse(node.leftchild);
System.out.print(node.data + " ");
inOrderTraverse(node.rightchild);
}
后序遍历
public static void postOrderTraverse(Node node) {
if (node == null)
return;
postOrderTraverse(node.leftchild);
postOrderTraverse(node.rightchild);
System.out.print(node.data + " ");
}