数据结构基础(三)

排序

  • 实现归并排序
public class MergeSort {

    public static void merge(int[] a, int start, int mid, int end) {
        int[] tmp = new int[a.length];
        System.out.println("merge " + start + "~" + end);
        int i = start, j = mid + 1, k = start;
        while (i != mid + 1 && j != end + 1) {
            if (a[i] < a[j])
                tmp[k++] = a[i++];
            else
                tmp[k++] = a[j++];
        }
        while (i != mid + 1)
            tmp[k++] = a[i++];
        while (j != end + 1)
            tmp[k++] = a[j++];
        for (i = start; i <= end; i++)
            a[i] = tmp[i];
        for (int p : a)
            System.out.print(p + " ");
        System.out.println();
    }

    public static void mergeSort(int[] a, int start, int end) {
        if (start < end) {
            int mid = (start + end) / 2;
            mergeSort(a, start, mid);// 左边有序
            mergeSort(a, mid + 1, end);// 右边有序
            merge(a, start, mid, end);
        }
    }

    public static void main(String[] args) {
        int[] a = {4,2,5,6,8,2,10,16,13};
        mergeSort(a, 0, a.length - 1);
    }
}
  • 插入排序与快速排序 (链表方式)
public class Solution {

    public static ListNode insertSort(ListNode head) {
        if (head == null || head.next == null) {
            return head;
        }
        ListNode p = head.next;
        ListNode start = new ListNode(0);
        start.next = head;
        while (p != null) {
//            p指向当前结点的后继节点
//            tmp指向当前结点
            ListNode tmp = start.next;
//            指向前驱的pre
            ListNode pre = start;
//            找到插入位置
            System.out.println(tmp != p && p.val >= tmp.val);
            while (tmp != p && p.val >= tmp.val) {
                tmp = tmp.next;
//                让tmp指向当前节点的后继节点 循环判断
                pre = pre.next;
//                pre指向当前结点的前驱节点
            }
            if (tmp == p) {
//                说明当前节点值小于后继节点值 升序正确 不用交换
                head = p;
            } else {
//                进行交换
                head.next = p.next;
                p.next = tmp;
                pre.next = p;
            }
            p = head.next;
        }
        head = start.next;
        return head;
    }

    public static ListNode getPartition(ListNode start, ListNode end) {
        int key = start.val;
        ListNode p = start;
        ListNode tmp = new ListNode(0);
        for (ListNode i = start.next; i != end; i = i.next) {
            if (i.val < key) {
                p = p.next;
                tmp.val = i.val;
                i.val = p.val;
                p.val = tmp.val;
            }
        }
        tmp.val = p.val;
        p.val = start.val;
        start.val = tmp.val;
        return p;
    }

    public static void quickSortHelper(ListNode head, ListNode tail) {
        if (head != tail && head.next != tail) {
            ListNode mid = getPartition(head, tail);
            quickSortHelper(head, mid);
            quickSortHelper(mid.next, tail);
        }
    }

    public static ListNode quickSort(ListNode head) {
        if (head == null || head.next == null) {
            return head;
        }
        quickSortHelper(head, null);
        return head;
    }

    public static void main(String[] args) {
        ListNode head = new ListNode(3);
        head.next = new ListNode(5);
        head.next.next = new ListNode(4);
        head.next.next.next = new ListNode(1);
//        ListNode sort = insertSort(head);
        ListNode sort = quickSort(head);
        while (sort != null) {
            System.out.print(sort.val + " ");
            sort = sort.next;
        }
    }
}
  • 冒泡排序
public class BubbleSort {
    public static void main(String[] args) {
        int[] arr = {3,5,4,4,6,8,1};
        System.out.println("排序前数组为:");
        for (int num : arr) {
            System.out.print(num + " ");
        }
        for (int i = 0; i < arr.length - 1; i++) {//外层循环控制排序趟数
            for (int j = 0; j < arr.length - 1 - i; j++) {//内层循环控制每一趟排序多少次
                if (arr[j] > arr[j + 1]) {
                    int temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
            }
        }
        System.out.println();
        System.out.println("排序后的数组为:");
        for (int num : arr) {
            System.out.print(num + " ");
        }
    }
}

  • 选择排序
public class SelectSort {
    public static void selectSort(int[] arr) {
        int n = arr.length;
        for (int i = 0; i < n; i++) {
            int k = i;
            // 找出最小值的下标
            for (int j = i + 1; j < n; j++) {
                if (arr[j] < arr[k]) {
                    k = j;
                }
            }
            // 将最小值放到未排序记录的第一个位置
            if (k > i) {
                int tmp = arr[i];
                arr[i] = arr[k];
                arr[k] = tmp;
            }
        }
    }

    public static void main(String[] args) {
        int[] a = {3, 5, 4, 4, 6, 8, 1};
        selectSort(a);
        for (int i : a
        ) {
            System.out.print(i + " ");
        }
    }
}
  • 堆排序(选做)
  • (完成leetcode上的返回滑动窗口中的最大值(239),这是上一期第三天的任务进行保留(涉及队列可以对第二天进行整理复习))
  • 编程实现 O(n) 时间复杂度内找到一组数据的第 K 大元素

二分查找

  • 实现一个有序数组的二分查找算法
public class BinarySearch {
    public static int binarySearch(int[] array, int a) {
        int lo = 0;
        int hi = array.length - 1;
        int mid;
        while (lo <= hi) {
            mid = (lo + hi) / 2;
            if (array[mid] == a) {
                return mid + 1;
            } else if (array[mid] < a) {
                lo = mid + 1;
            } else {
                hi = mid - 1;
            }
        }
        return -1;
    }

    public static void main(String[] args) {
        int[] a = {1, 3, 5, 6, 7, 8};
        System.out.println(binarySearch(a, 5));
    }
}
  • 实现模糊二分查找算法(比如大于等于给定值的第一个元素)

事情有点多先打卡。。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值