java面试-数据结构和算法

1.排序

1.1 冒泡排序

package sort;

/**
 * Created by david on 2018/8/16
 * 冒泡排序
 */
public class BubbleSort {
    private static int[] bubbleSort(int[] a) {
        int len = a.length;
        for (int i = 1; i < len - 1; i++) {
            for (int j = 1; j < len - 1-i; j++) {
                if (a[j + 1] < a[j]) {
                    swap(a, j + 1, j);
                }
            }
        }
        return a;
    }

    //交换方法
    private static void swap(int[] a, int i, int j) {
        int tmp = a[i];
        a[i] = a[j];
        a[j] = tmp;
    }

    //测试
    public static void main(String[] args) {
        int[] a = {1, 4, 6, 8, 99, 9, 2, 99};
        int[] sort = bubbleSort(a);
        for (int s : sort) {
            System.out.print(s + " ");
        }

    }
}

1.2快速排序

package sort;

/**
 * Created by david on 2018/8/16
 * 快速排序
 * 不稳定,时间复杂度 最理想 O(nlogn) 最差时间O(n^2)
 */
public class QuickSort {
    private static int[] quickSort(int[] a, int low, int high) {
        //中心点
        int mid = 0;
        if (low < high) {
            mid = partition(a, low, high);
            quickSort(a, low, mid - 1);
            quickSort(a, mid + 1, high);
        }
        return a;
    }

    private static int partition(int[] a, int low, int high) {
        int b = a[low];
        while (low < high) {
            while (low < high && a[high] >= b) {
                high--;
            }
            a[low] = a[high];
            while (low < high && a[low] <= b) {
                low++;
            }
            a[high] = a[low];
        }
        a[low] = b;
        return low;
    }

    //测试
    public static void main(String[] args) {
        int[] a = {1, 14, 6, 8, 99, 9, 2, 99};
        int[] sort = quickSort(a, 0, 7);
        for (int s : sort) {
            System.out.print(s + " ");
        }
    }
}

1.3 二分查找

package sort;

/**
 * Created by david on 2018/8/16
 * 查找前的数据必须是已经排好序的, 然后得到数组的开始位置start和结束位置end,
 * 取中间位置mid的数据a[mid]跟待查找数据key进行比较, 若 a[mid] > key, 则取end = mid - 1;
 * 若 a[mid] < key, 则取start = mid + 1; 若 a[mid] = key 则直接返回当前mid为查找到的位置.
 * 依次遍历直到找到数据或者最终没有该条数据
 */
public class BinarySearch {
    public static int binarySearch(int[] a, int key) {
        int start = 0;
        int end = a.length - 1;
        int mid = -1;
        while (start <= end) {
            mid = (start + end) / 2;
            if (a[mid] == key) {
                return mid;
            } else if (a[mid] > key) {
                end = mid - 1;
            } else if (a[mid] < key) {
                start = mid + 1;
            }
        }
        return -1;
    }

    // 测试
    public static void main(String[] args) {
        int[] a = {1, 4, 6, 8, 99};
        int i = binarySearch(a, 99);
        System.out.println(i);
    }
}

1.3 String与Array转换

import java.util.Arrays;

/**
 * Created by david on 2018/8/16
 * String/Array转换
 */
public class Convert {
    public static void main(String[] args) {
        String str = "we are family";
        //String转成Array
        char[] chars = str.toCharArray();
        //排序
        Arrays.sort(chars);
        //转成String
        String s = Arrays.toString(chars);
        //根据index获得char
        char c = str.charAt(7);
        //长度
        str.length();
        int length = chars.length;
        //子串
        String substring1 = str.substring(1, 4);
        String substring2 = str.substring(3);
        //int转string
        Integer integer = Integer.valueOf("3");
        //string转int
        String value = String.valueOf(3);

    }
}

1.4 单链表反转

public class Node {
    //为了方便,这两个变量都使用public,而不用private就不需要编写get、set方法了。
    //存放数据的变量,简单点,直接为int型
    public int data;
    //存放结点的变量,默认为null
    public Node next;
    
    //构造方法,在构造时就能够给data赋值
    public Node(int data){
        this.data = data;
    }
}
package link;

/**
 * Created by david on 2018/8/16
 * 单链表反转
 */
public class NodeRe {

    public static void main(String[] args) {
        Node head = new Node(0);
        Node node1 = new Node(1);
        Node node2 = new Node(2);
        Node node3 = new Node(3);
        head.setNext(node1);
        node1.setNext(node2);
        node2.setNext(node3);
        // 调用反转方法
        head = reverse(head);

        // 打印反转后的结果
        while (null != head) {
            System.out.print(head.getData() + " ");
            head = head.getNext();
        }
    }

    public static Node reverse(Node head){
        // head看作是前一结点,head.getNext()是当前结点,
        // reHead是反转后新链表的头结点
        if(head == null || head.getNext() == null){
            // 若为空链或者当前结点在尾结点,则直接还回
            return head;
        }
        Node reHead = reverse(head.getNext());
        // 将当前结点的指针域指向前一结点
        head.getNext().setNext(head);
        // 前一结点的指针域令为null;
        head.setNext(null);
        // 反转后新链表的头结点
        return reHead;
    }
}

1.5 双向链表反转

双向链表反转

1.6 多线程

多线程

猜你喜欢

转载自blog.csdn.net/a1032818891/article/details/81747370