大数据开发-01

来源:互联网 发布:star法则简历模板java 编辑:程序博客网 时间:2024/04/30 16:51

1、位操作,显示二进制方法:

public static void toBinary(int num) {
int[] arr = new int[32];
// 0000 0010 & 0000 0001   ===> 0 
for(int i = 31; i >= 0; i--) {
arr[i] = (num >> i) & 1;
System.out.print(arr[i]);
}
}


2、插入排序法

    publicstaticint[] doInsertionSort(int[] input){
         
        inttemp;
        for(inti = 1; i < input.length; i++) {
            for(intj = i ; j > 0; j--){
                if(input[j] < input[j-1]){
                    temp = input[j];
                    input[j] = input[j-1];
                    input[j-1] = temp;
                }
            }
        }
        returninput;
    }

3、冒泡排序法,跟相邻的值进行比较,替换
for (int m = n; m >= 0; m--) {            for (int i = 0; i < n - 1; i++) {                k = i + 1;                if (array[i] > array[k]) {                    swapNumbers(i, k, array);                }            }            printNumbers(array);        }
}
4、选择排序法
for(int i = 0; i < data.length; i++) {//假设第一个数十最小数;int index = i;for(int j = i + 1; j < data.length; j++) {if (data[j] < data[index]) {//如果index = j;}temp = data[index];data[index] = data[i];data[i] = temp;}}

    privatevoidquickSort(intlowerIndex,inthigherIndex) {
         
        inti = lowerIndex;
        intj = higherIndex;
        // calculate pivot number, I am taking pivot as middle index number
        intpivot = array[lowerIndex+(higherIndex-lowerIndex)/2];
        // Divide into two arrays
        while(i <= j) {
            /**
             * In each iteration, we will identify a number from left side which
             * is greater then the pivot value, and also we will identify a number
             * from right side which is less then the pivot value. Once the search
             * is done, then we exchange both numbers.
             */
            while(array[i] < pivot) {
                i++;
            }
            while(array[j] > pivot) {
                j--;
            }
            if(i <= j) {
                exchangeNumbers(i, j);
                //move index to next position on both sides
                i++;
                j--;
            }
        }
        // call quickSort() method recursively
        if(lowerIndex < j)
            quickSort(lowerIndex, j);
        if(i < higherIndex)
            quickSort(i, higherIndex);
    }
 
    privatevoidexchangeNumbers(inti,intj) {
        inttemp = array[i];
        array[i] = array[j];
        array[j] = temp;
    }




0 0
原创粉丝点击