算法学习

最近又复习了一下算法,发现不长写容易忘啊。在此记一下:

排序

冒泡排序

 private static void sort(int [] arr){
        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;
                }
            }
        }
    }

插入排序

private static void sort(int [] arr){
        for (int i=1;i<arr.length;i++){
            int temp = arr[i];
            int key = i-1;
            while (key>=0&&arr[key]>temp){
                arr[key+1]=arr[key];
                key=key-1;
            }

            arr[key+1]=temp;
        }
    }

希尔排序

public static void sort(int[] arr) {
        // i表示希尔排序中的第n/2+1个元素(或者n/4+1)
        // j表示希尔排序中从0到n/2的元素(n/4)
        // r表示希尔排序中n/2+1或者n/4+1的值
        int i, j, r, tmp;
        // 划组排序
        for(r = arr.length / 2; r >= 1; r = r / 2) {
            for(i = r; i < arr.length; i++) {
                tmp = arr[i];
                j = i - r;
                // 一轮排序
                while(j >= 0 && tmp < arr[j]) {
                    arr[j+r] = arr[j];
                    j -= r;
                }
                arr[j+r] = tmp;
            }
            System.out.println(i + ":" + Arrays.toString(arr));
        }
    }

快速排序

 private static int partion(int[] arr, int left, int right, int poiont) {
        int leftPoion = left - 1;
        int rightPoint = right;
        int poton1 = arr[poiont];
        while (true) {
            while (leftPoion < rightPoint && arr[++leftPoion] < poton1) ;
            while (leftPoion < rightPoint && arr[--rightPoint] > poton1) ;
            if (leftPoion < rightPoint) {
                int temp = arr[leftPoion];
                arr[leftPoion] = arr[rightPoint];
                arr[rightPoint] = temp;
            } else {
               int temp =arr[poiont];
               arr[poiont]= arr[leftPoion];
               arr[leftPoion]=temp;
               return leftPoion;
//                break;
            }
        }

//        return 0;
    }

    private static void sort(int [] arr,int left,int right,int poiont){
        if(left>right){
            return;
        }

        int poion1 = partion(arr,left,right,poiont);

        sort(arr,left,poion1-1,poion1-1);
        sort(arr,poion1+1,right,right);
    }


public class MyStack {
    private int top = -1;

    private long arr[];

    public MyStack() {
        arr = new long[20];
    }

    public void push(long value) {
        arr[++top] = value;
    }

    public long pop() {
        long value = arr[top--];
        return value;
    }

    public boolean isEmpty(){

        return top==-1;
    }

    public boolean isFull(){
        return top==arr.length;
    }
}

队列

public class MyQueue {

    private int front;
    private int end;
    private int elements;
    private long arr[];

    //循环列表
    public MyQueue() {
        front = 0;//头
        end = -1;//尾
        elements = 0;//个数
        arr = new long[3];
    }


    public void insert(long value) {
        if (isFull()) {
            return;
        }
        if (end == arr.length - 1) {
            end = -1;
        }
        arr[++end] = value;
        elements++;
    }


    public Long remove() {
        if (isEmpty()) {
            return null;
        }
        if (front == arr.length) {
            front = 0;
        }
        elements--;
        return arr[front++];
    }

    public boolean isEmpty() {

        return elements == 0;
    }

    public boolean isFull() {
        return elements == arr.length;
    }
}

链表

public class Node {
    private long data;
    private Node next;


    public Node(long data) {
        this.data = data;
    }

    public void display(){
        System.out.println(data);
    }


    public void setNext(Node next) {
        this.next = next;
    }

    public Node getNext() {
        return next;
    }

    public long getData() {
        return data;
    }

    public void setData(long data) {
        this.data = data;
    }
}


public class LinkedTable {

    private Node first;

    public void addFirst(long data) {
        if (first == null) {
            first = new Node(data);
            return;
        }

        Node temp = new Node(data);
        temp.setNext(first);
        first = temp;
    }


    public void display() {
        if (first == null) {
            return;
        }
        first.display();
        Node node = first.getNext();
        while (node != null) {
            node.display();
            node = node.getNext();
        }
    }


    public Node removeFirst() {
        if (first == null) {
            return null;
        }
        Node temp = first;
        first = first.getNext();
        return temp;
    }


    public Node addEnd(long data) {
        if (first == null) {
            first = new Node(data);
            return first;
        }
        Node temp = first;
        while (temp.getNext() != null) {
            temp = temp.getNext();
        }

        temp.setNext(new Node(data));
        return temp;
    }


    public void removeEnd(){
        if(first==null){
            return;
        }

        if(first.getNext()==null){
            first = null;

            return;
        }
        Node priousNode = first;
        Node next = first.getNext();
       while (next.getNext()!=null){
           priousNode = next;
           next= next.getNext();
       }
       priousNode.setNext(null);
    }

    public Node find(long data){
        if(first==null){
            return null;
        }

        Node temp = first;

        while (temp.getData()!=data){
            temp = temp.getNext();
            if(temp==null){
                return null;
            }
        }

        return temp;
    }


    public void remove(long data){
        if(first==null){
            return;
        }
        if(first.getData()==data){
            first = first.getNext();
            return;
        }

        Node prious = first;
        Node current = first;

        while (current.getData()!=data){
            prious= current;
            current = current.getNext();
            if(current==null){
                return;
            }
        }

        prious.setNext(current.getNext());

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值