排序算法

11 篇文章 0 订阅

冒泡

public class BubbleSort {
    public static int[] bubbleSort(int[] array){
        int len=array.length;
        if(len==0||array==null)
            return null;
        int temp;
        for(int i=0;i<len-1;i++){
            for(int j=0;j<len-i-1;j++){
                if(array[j]>array[j+1]){
                    temp=array[j];
                    array[j]=array[j+1];
                    array[j+1]=temp;
                }
            }
        }
        return array;
    }
}

插入

public class InsertionSort {
    public static int[] insertionSort(int[] array) {
        int len = array.length;
        if (len == 0 || array == null)
            return null;
        int temp, i, j;
        for (i = 1; i < len; i++) { //将各元素插入已经排序的序列中
            if (array[i] < array[i - 1]) { //如果array[i]<他的前驱,则说明需要重排列
                temp = array[i];    //暂存array[i]的值
                for (j = i-1; j >= 0 && array[j] > temp; --j) {
                    array[j+1] = array[j];//将大于array[i]的值依次后移
                }
                array[j+1] = temp;//将array[i]的值插入正确的位置
            }
        }
        return array;
    }

归并

public class MergeSort {
    public static void merge(int[] arr, int low, int mid, int high) {
        int[] B = new int[arr.length];
        int i, j, k;
        for (k = low; k <= high; k++) {
            B[k] = arr[k];
        }
        for (i = low, j = mid + 1, k = i; i <= mid && j <= high; k++) {
            if (B[i] < B[j]) {
                arr[k] = B[i++];
            } else {
                arr[k] = B[j++];
            }
        }
        while (i <= mid) arr[k++] = B[i++];
        while (j <= high) arr[k++] = B[j++];
    }



    public static void mergeSort(int[] arr, int low, int high) {
        if (low < high) {//跳出递归的条件
            int mid = (low + high) / 2;
            mergeSort(arr, low, mid);
            mergeSort(arr, mid + 1, high);
            merge(arr, low, mid, high);
        }
    }

快排

   public static int partition(int[] arr, int low, int high) {
        int pivot = arr[low];//设定数组第一个元素为基准
        while (low < high) {
            while (low < high && arr[high] >= pivot)
                high--;
            arr[low] = arr[high];//比基准小的元素移到基准左边
            while (low < high && arr[low] <= pivot)
                low++;
            arr[high] = arr[low];//比基准大的元素移到基准右边
        }
        arr[low] = pivot;//基准存放的最终位置
        return low;//返回基准位置
    }

    public static void quickSort(int[] arr, int low, int high) {
        if(low<high){ //跳出递归的条件
            int index=partition(arr, low, high);
            quickSort(arr, low, index-1);
            quickSort(arr, index+1,high);
        }
    }

堆排序

 /**
     * 基于大根堆实现的堆排序
     *
     * @param a
     */
    public static void heapSort(int[] a) {
        int temp;
        buildMaxHeap(a);
        for (int i = 0; i < a.length; i++) {
            //a[0]总是为根节点,最大值,每次交换a[0]和堆中最后一个元素,即可实现排序
            temp = a[0];
            a[0] = a[a.length - 1 - i];
            a[a.length - 1 - i] = temp;
            //每次交换结束都要重新调整大根堆
            headAdjust(a, 0, a.length - 1 - i);
        }
    }

    /**
     * 建堆方法
     *
     * @param a
     */
    public static void buildMaxHeap(int[] a) {
        int len = a.length;
        for (int i = (len - 1) / 2; i >= 0; i--) {
            headAdjust(a, i, len);
        }
    }

    /**
     * 调整堆的方法
     *
     * @param a
     * @param k:待调整节点的索引
     * @param len
     */
    static void headAdjust(int[] a, int k, int len) {
        int temp = a[k];//暂存当前调整的节点
        int s = 2 * k + 1;//s为当前k节点的左节点,s+1为右节点
        while (s < len) {
            if (s + 1 < len && a[s] < a[s + 1]) {//找出左右节点中较大的一个节点
                s = s + 1;
            }
            if (a[s] > a[k]) {//子节点大于当前要调整的节点
                a[k] = a[s];//将子节点的值赋给当前节点
                k = s;//重新设置下一个结点的索引
                s = 2 * k + 1;
            } else {//如果当前节点大于他的子节点则不需要调整
                break;
            }
            a[k] = temp;//将当前节点的值赋给比他大的子节点上,相当于互换位置
        }
    }

死锁

public static void main(String[] args) {
        CreatThread creatThread = new CreatThread();
        Thread t1 = new Thread(creatThread, "窗口1");
        Thread t2 = new Thread(creatThread, "窗口2");
        Thread t3 = new Thread(creatThread, "窗口3");
        t1.start();
        t2.start();
        t3.start();
    }

    static class CreatThread implements Runnable {
        private int ticket = 10;
        Object lock = new Object();

        @Override
        public void run() {
            String name = Thread.currentThread().getName();
            while (true) {
                if ("窗口1".equals(name)) {
                    synchronized (lock) {//窗口1拿到lock锁
                        sell(name);
                    }
                }
                sell(name);
                if (ticket <= 0)
                    break;
            }
        }

        //存在可能窗口2拿到this锁后想获取lock锁,窗口1拿到lock锁想获取this锁,此时死锁产生
        private synchronized void sell(String name) {
            try {
                Thread.sleep(10);
            } catch (Exception e) {
                e.printStackTrace();
            }
            synchronized (lock) {
                if (ticket > 0) {
                    System.out.println(name + ":" + ticket);
                    ticket--;
                }
            }
        }
    }
}

结束进程

  private static boolean flag=true;
    public static void main(String[] args)throws InterruptedException{
        Thread t1=new Thread(new Runnable() {
            @Override
            public void run() {
                while(flag){
                    System.out.println("线程开始执行");
                    try {
                        Thread.sleep(1001);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
        t1.start();
        Thread.sleep(10001);
        flag=false;
        System.out.println("线程结束");
    }

BFS

public class BFS {
    public void BFSWithQueue(TreeNode node){
        Queue<TreeNode> queue=new LinkedList<>();
        if (node!=null)
            queue.offer(node);
        while (!queue.isEmpty()){
            TreeNode treeNode = queue.poll();
            System.out.println(treeNode.val);//在此处理每个节点
            if(treeNode.left!=null)
                queue.offer(treeNode.left);
            if(treeNode.right!=null)
                queue.offer(treeNode.right);
        }
    }
}

链表环

/**
 * 关于链表环的两个结论:
 * 1.设置快慢指针,假如有环,他们最后一定相遇。(快指针一次两步,慢指针一次一步)
 * 2.两个指针分别从链表头和相遇点继续出发,每次走一步,最后一定相遇在环入口。
 */
public class EntryNodeOfLoop {
    public ListNode entryNodeOfLoop(ListNode pNode) {
        ListNode low = pNode;
        ListNode fast = pNode;
        while (fast != null && fast.next != null) {
            low = low.next;
            fast = fast.next.next;
            if (low == fast)
                break;//链表存在环
        }
        if (fast == null || fast.next == null)//链表没有环
            return null;
        low = pNode;
        while (low != fast) {
            low = low.next;
            fast = fast.next;
        }
        return low;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值