Java-----函数

package Function;

import java.util.Scanner;

import StudentaChievementManagementSystem.Students;

public class Sort {
    
    //冒泡排序
    public static void bubbleSort(int[] arr) {
        for (int i = 0; i < arr.length - 1; i++) {
            for (int j = 0; j < arr.length - i - 1; j++) {
                if (arr[j] > arr[j + 1]) {
                    int tmp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = tmp;
                }
            }
        }
    }
    
    //快速排序
    public static void QuickSort(int[] a,int s,int t)
    {
        int i=s,j=t;
        int tmp;
        if(s<t)
        {
            tmp=a[s];
            while(i!=j)
            {
                while(j>i&&a[j]>tmp)
                    j--;
                a[i]=a[j];
                while(i<j&&a[i]<=tmp)
                    i++;
                a[j]=a[i];
            }
            a[i]=tmp;
            QuickSort(a, s,i-1);
            QuickSort(a, i+1, t);
            }
        }
    
    //选择排序
    public static void selectSort(int a[],int sdnum) {
        for (int i = 0; i < sdnum - 1; i++) {
            for (int j = i + 1; j < sdnum; j++) {
                if (a[i] < a[j]) {
                    int tmp = a[i];
                    a[i] = a[j];
                    a[j] = tmp;
                }
            }
        }
    }
}
package Function;

public class Bit_by_bit {

        /**
         *     将一个字节byte转换成0~255之间的整数(正数)
        将符号位视为数字位,而不是符号位
         */
    public static int _byte(byte b) {
        int i = b & 255;
        return i;
    }
    
    public static void outIntBinaryString(int n) {
        for(int i=31;i>0;i--) {
            System.out.print((n>>i) & 2);
            
        }
        System.out.println();
    }
    
    /*
     * 十进制-->二进制
     */
    public static void toBin(int num) {
        StringBuffer sb = new StringBuffer();
        while(num>0) {
            sb.append(num%2);
            num = num /2;
        }
        System.out.println(sb.reverse());
    }
    
    /*
     * 十进制-->十六进制
     */
    public static void toHex(int num) {
        StringBuffer sb = new StringBuffer();
        for(int x=0; x<8; x++) {
            int temp = num & 15;
            if(temp>9) {
                sb.append((char)(temp-10+'A'));
            }else {
                sb.append(temp);
                num = num >>> 4;
            }
            System.out.println(sb.reverse());
        }
    }
    
    /*
     * 十进制转换任意进制
     */
    public static void trans(int num, int base,int offset) {
        char[] chs= {'0','1','2','3'
                    ,'4','5','6','7'
                    ,'8','9','A','B'
                    ,'C','D','E','F'};
        char[] arr = new char[32];
        int pos = arr.length;
        while(num!=0) {
            int tmp =num &base;
            arr[--pos] = chs[tmp];
            num = num >>>offset;
        }
        
        for(int x=pos;x<arr.length;x++) {
            System.out.print(arr[x]);
        }
    }
}
package Function;

public class Binary_Search {

    public static int Seach(int[] a, int start, int end, int key) {
        int mid = (start + end) / 2;
        while (start <= end) {
            if (key < a[mid])
                return Seach(a, start, mid - 1, key);
            else if (key > a[mid])
                return Seach(a, mid + 1, end, key);
            else
                return mid;
        }
        return -1;
    }
}
package Function;

import java.util.ArrayList;
import java.util.Scanner;

public class WebCrawler {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter a URL");
        String url = sc.nextLine();
        crawler(url);
    }

    public static void crawler(String startingURL) {
        // TODO Auto-generated method stub
        ArrayList<String> listOfPendingURLs = new ArrayList<>();
        ArrayList<String> listOfTraversedURLs = new ArrayList<>();
        
        listOfPendingURLs.add(startingURL);
        while(!listOfPendingURLs.isEmpty() &&
                listOfTraversedURLs.size()<=100) {
            String urlString = listOfPendingURLs.remove(0);
            if( !listOfTraversedURLs.contains(urlString)) {
                listOfTraversedURLs.add(urlString);
                System.out.println("Crawl "+urlString);
                for(String s: getSubURLs(urlString)) {
                    if(!listOfTraversedURLs.contains(s))
                            listOfPendingURLs.add(s);
                }
            }
        }
        
    }

    public static ArrayList<String> getSubURLs(String urlString) {
        // TODO Auto-generated method stub
        ArrayList<String> list = new ArrayList<>();
        try {
            java.net.URL url = new java.net.URL(urlString);
            Scanner sc=new Scanner(url.openStream());
            int current = 0;
            while(sc.hasNext()){
                String line =sc.nextLine();
                current = line.indexOf("https:",current);
                while(current > 0) {
                    int endIndex=line.indexOf("\"",current);
                    if(endIndex > 0) {
                        list.add(line.substring(current,endIndex));
                        current = line.indexOf("https:",endIndex);
                    }
                    else
                        current = -1;
                }
            }
        }
        catch (Exception ex) {
            System.out.println("Error: "+ex.getMessage());
        }
        return list;
    }
}

 

posted @ 2019-05-18 22:57  殇之弑梦  阅读(223)  评论(0编辑  收藏  举报