몇 가지 흔히 볼 수 있 는 [정렬] 과 [데이터 구조]

(1) 일반적인 정렬

import java.util.Arrays;

public class Sort {
//    
    private static int partition(int[] arr, int low, int hight) {
        int pivotkey = arr[low];
        while (low < hight) {
            while (low < hight && pivotkey <= arr[hight])
                --hight;
            int temp1 = arr[low];
            arr[low]=arr[hight];
            arr[hight]=temp1;
            //arr[low] = arr[hight];
            while (low < hight && pivotkey >= arr[low])
                ++low;
            int temp2 = arr[low];
            arr[low]=arr[hight];
            arr[hight]=temp2;
            //arr[hight] = arr[low];
        }
        return low;
    }
//    
    public static void qSort(int[] arr, int low, int hight) {
        if (low < hight) {
            int pivotkey = partition(arr, low, hight);
            qSort(arr, low, pivotkey - 1);
            qSort(arr, pivotkey + 1, hight);
        }
    }
//    
    public static int[] insertOrder(int a[]){
        for (int i = 1; i < a.length; i++) {
            int k = a[i];
            int j;
            for (j = i - 1; j >= 0 && k < a[j]; j--) {
                a[j + 1] = a[j];
            }
            a[j + 1] = k;
            //System.out.println(Arrays.toString(a));
        }
        return a;
    }
    //    
    public static int[] maopao(int[] a){

        for(int i=0;i<a.length-1;i++){//i     
            for(int j=0;j<a.length-i-1;j++){//j j+1      
                if(a[j]>a[j+1]){
                    int temp = a[j];
                    a[j]= a[j+1];
                    a[j+1]=temp;
                }
            }
            //System.out.println(Arrays.toString(a));
            //Arrays.sort(a);
        }
        return a;
    }
    //    
    public int[] selectOrder(int arr[] ) {
        for (int i = 0; i <arr.length; i++) {
            int max = arr[i];
            for (int j = i + 1; j < arr.length; j++) {
                if (arr[j] < max) {
                    int temp = arr[j];
                    arr[j] = max;
                    max = temp;
                    arr[i] = max;
                }
            }
        }
        return arr;
    }

    //     
        public void binarySearch() {
            int b[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
            int start = 0;
            int end = b.length - 1;
            int mid;
            int target = 1;
            while (true) {
                mid = ((end + start) / 2);
                if (b[mid] == target) {
                    System.out.println(mid);
                    break;
                }
                if (target < b[mid]) {
                    end = mid;
                }
                if (target > b[mid]) {
                    start = mid + 1;
                }

            }
        }

    public static void main(String args[]) {
        int Arr[] = { 10, 30, 20, 50, 90, 80, 60, 40, 70 };
        System.out.println(Arrays.toString(Arr));
        qSort(Arr, 0, Arr.length -1);
        System.out.println(Arrays.toString(Arr));
    }
}
    :
    

            

         
    ,          
        [ 13 14 94 33 82 25 59 94 65 23 45 27 73 25 39 10 ] 
,        53115,          , 
13 14 94 33 82 
25 59 94 65 23 
45 27 73 25 39 
10
         
10 14 73 25 23 
13 27 94 33 39 
25 59 94 65 82 
45

       ,           :[ 10 14 73 25 23 13 27 94 33 39 25 59 94 65 82 45 ].  10310 14 13 
25 23 33 
27 25 59 
39 65 73 
45 94 82 
94 
-   1      (            )。

public static void shellPass(Integer[] array, int d) { for (int i = d; i < array.length; i++) { int temp = array[i]; int j = i - d; while (j >= 0 && array[j] > temp) { array[j + d] = array[j]; j -= d; } array[j + d] = temp; } }
public static void shellSort(Integer[] data) { int increment = 12; do { increment = increment / 3 + 1; shellPass(data, increment); } while (increment > 1);
}

( )
2.1 graph
2.1.1

public class GraphNode {
    int val;
    GraphNode next;
    GraphNode[] neighbors;
    boolean visited;

    GraphNode(int x) {
        val = x;
    }

    GraphNode(int x, GraphNode[] n){
        val = x;
        neighbors = n;
    }

    public String toString(){
        return "value: "+ this.val; 
    }
}

2.1.2


public class Queue {
     GraphNode first, last;

        public void enqueue(GraphNode n){
            if(first == null){
                first = n;
                last = first;
            }else{
                last.next = n;
                last = n;
            }
        }

        public GraphNode dequeue(){
            if(first == null){
                return null;
            }else{
                GraphNode temp = new GraphNode(first.val, first.neighbors);
                first = first.next;
                return temp;
            }   
        }
}

2.1.3

public class GraphTest {
    public static void main(String[] args) {
        GraphNode n1 = new GraphNode(1); 
        GraphNode n2 = new GraphNode(2); 
        GraphNode n3 = new GraphNode(3); 
        GraphNode n4 = new GraphNode(4); 
        GraphNode n5 = new GraphNode(5); 

        n1.neighbors = new GraphNode[]{n2,n3,n5};
        n2.neighbors = new GraphNode[]{n1,n4};
        n3.neighbors = new GraphNode[]{n1,n4,n5};
        n4.neighbors = new GraphNode[]{n2,n3,n5};
        n5.neighbors = new GraphNode[]{n1,n3,n4};

        breathFirstSearch(n1, 5);
    }

    public static void breathFirstSearch(GraphNode root, int x){
        if(root.val == x)
            System.out.println("find in root");

        Queue queue = new Queue();
        root.visited = true;
        queue.enqueue(root);

        while(queue.first != null){
            GraphNode c = (GraphNode) queue.dequeue();
            for(GraphNode n: c.neighbors){

                if(!n.visited){
                    System.out.print(n + " ");
                    n.visited = true;
                    if(n.val == x)
                        System.out.println("Find "+n);
                    queue.enqueue(n);
                }
            }
        }
    }
}

2.2link
2.2.1

public class Node {
    int val;
    Node next;

    Node(int x) {
        val = x;
        next = null;
}
}

2.3Queue
2.3.1

public class Node {
    int val;
    Node next;

    Node(int x) {
        val = x;
        next = null;
}
}

2.3.2


public class Queue {
    Node first, last;

    public void enqueue(Node n){
        if(first == null){
            first = n;
            last = first;
        }else{
            last.next = n;
            last = n;
        }
    }

    public Node dequeue(){
        if(first == null){
            return null;
        }else{
            Node temp = new Node(first.val);
            first = first.next;
            return temp;
        }   
    }
}

2.4stack
2.4.1

public class Node {
    int val;
    Node next;

    Node(int x) {
        val = x;
        next = null;
}
}

2.4.2


public class Stack {
    Node top; 

    public Node peek(){
        if(top != null){
            return top;
        }

        return null;
    }

    public Node pop(){
        if(top == null){
            return null;
        }else{
            Node temp = new Node(top.val);
            top = top.next;
            return temp;    
        }
    }

    public void push(Node n){
        if(n != null){
            n.next = top;
            top = n;
        }
    }
}

2.5Tree
2.5.1

public class TreeNode {
    int value;
    TreeNode left;
    TreeNode right;
}

좋은 웹페이지 즐겨찾기