常考算法笔试题及源码

1. 冒泡排序

public static void bubbleSort(int[] arr) {
	for (int i = 0; i < arr.length; i++) {
		for (int j = 0; j < arr.length - i - 1; j++) {
		    // 每次把最大的数放到最右边
			if (arr[j] > arr[j + 1]) {
				int tmp = arr[j];
				arr[j] = arr[j+1];
				arr[j+1] = tmp;
			}
		}
	}
}

注意:j < arr.length-i-1 , 是-1不是+1, 不然会数组下标越界。

2. 二分法查找

前提:数据必须有序
要求:查找指定的值在有序的数组中,返回对应数组元素下标。
算法:用当前元素与中间大小元素比较,若小于,则取左边子数组的中间元素做比较。

public static int binarySearch(int[] arr, int key) {
	int start = 0;
	int end = arr.length - 1;
	if (key < arr[start] || key > arr[end]) {
		return -1;
	}
	
	while (start <= end) {
		int middle = (start + end) / 2;
		if (key == arr[middle]) {
			return middle;
		} else if (key > arr[middle]) {
			start = middle + 1;
		} else {
			end = middle - 1;
		}
	}
	
	return -1;
}

注意:

  1. while (start <= end) , 注意<=不是<
  2. 注意不要把arr[index]和下标index搞混。

3. 快速排序(高频考题)

快速排序的原理:选择一个关键值作为基准值。比基准值小的都在左边序列(一般是无序的),比基准值大的都在右边(一般是无序的)。一般选择序列的第一个元素作为基准值。

public class QuickSort {
	public static int getBase(int[] arr, int low, int high) {
		int base = arr[low];

		while (low < high) {
			// 从右边数,找到比基准元素小的元素
			while (low < high && base <= arr[high]) {
				high--;
			}
			arr[low] = arr[high];
			
			// 从左边数,找到比基准元素大的元素
			while (low < high && arr[low] <= base) {
				low++;
			}
			arr[high] = arr[low];
		}

		arr[low] = base;
		return low;
	}

	public static void sort(int[] arr, int low, int high) {
		if (low < high) {
			int base = getBase(arr, low, high);
			sort(arr, low, base);
			sort(arr, base + 1, high);
		}
	}

	public static void main(String[] args) {
		int arr[] = { 49, 38, 65, 97, 76, 13, 27, 49, 78, 34, 12, 64, 5, 4, 62, 99, 98, 54, 56, 17, 18, 23, 34, 15, 35,
				25, 53, 51 };
		sort(arr, 0, arr.length - 1);
		for (int e : arr) {
			System.out.print(e + " ");
		}
	}
}

注意:1. getBase和sort的入参都是int[] arr, int low, int high
2. sort时,不要忘记放在if(low 3. getBase时,规律性较强,记住在while(low 4. while(low 5. 最后调用sort时:
int base = getBase(arr, low, high);
sort(arr, low, base);
sort(arr, base+1, high);
注意最后是base+1而不是base。

4. 二叉树逐层打印

import java.util.LinkedList;
import java.util.Queue;

public class cengci {
	public static void print(BinaryTree root) {
		Queue<BinaryTree> queue = new LinkedList<>();
		queue.add(root);
		BinaryTree currentLineLastNode = null;
		BinaryTree last = root;
		
		while(queue.size() > 0) {
			BinaryTree currentNode = queue.poll();
			if (currentNode.hasLeftNode()) {
				queue.add(currentNode.getLeftNode());
				currentLineLastNode = currentNode.getLeftNode();
			}
			if (currentNode.hasRightNode()) {
				queue.add(currentNode.getRightNode());
				currentLineLastNode = currentNode.getRightNode();
			}
			System.out.print(" " + currentNode.getValue());
			if (last.equals(currentNode)) {
				System.out.println();
				last = currentLineLastNode;
			}
		}
	}

	public static void main(String[] args) {
		BinaryTree root = new BinaryTree();
        root.setValue("root");
        BinaryTree left01 = new BinaryTree("left01");
        BinaryTree right01 = new BinaryTree("right01");
        root.setLeftNode(left01);
        root.setRightNode(right01);
        BinaryTree left11 = new BinaryTree("left11");
        BinaryTree right11 = new BinaryTree("right11");
        left01.setLeftNode(left11);
        left01.setRightNode(right11);
        BinaryTree left12 = new BinaryTree("left12");
        BinaryTree right12 = new BinaryTree("right12");
        right01.setLeftNode(left12);
        right01.setRightNode(right12);
        print(root);
	}
}

你可能感兴趣的:(数据结构与算法,面试题)