Java Interview Algorithm questions

Source: Internet
Author: User

/** * with n individuals in a circle, from the 1th person began to count, number to the first person out of the column, and then the next person from the column began to count off, the number to the M-person and the column, ..., so repeatedly to all the people out listed as * stop.     The number of the n individuals is 1,2,...,n, the order of the dequeue is printed, and the Java implementation is required.        */@org. Junit.test public void Test3 () {//Initialize entire team int[] PEOs = new INT[9];        for (int i = 1; i < peos.length+1; i++) {peos[i-1] = i;        } System.out.println (Arrays.tostring (PEOs));        Out of team step int m = 2;        Out of team record int[] out = new int[9];        int n = 0;            for (int i = 0; Peos.length! = 0; i++) {//each time the queue is out, the current serial number.            n = m + n-1;                while (n >= peos.length) {//if n subscript exceeds the length of the array, it is counted from the beginning.            n = n-peos.length;            } Out[i] = Peos[n]; /* * Move all elements that follow the array n items forward one item.             Can be replaced with the System.arraycopy method. * System.arraycopy (PEOs, n + 1, PEOs, N, peos.length-1-N);
* Create a new array after the team */for (int j = n; j < Peos.length-1; J + +) {Peos[j] = peos[j + 1]; } PEOs = arrays.copyof (PEOs, peos.length-1); } System.out.println (Arrays.tostring (out)); }

  

/**     * Insertion sorting principle refer to Playing cards, take out a card each time, set as AI, inserted into the order should be in, do not need to create a new array.     * Starting with the second element of the array, compare the AI to the previous element (the ordered I element),     * If it is ascending, you should find the first time AI encounters a position greater than its own, set to J,ai sort this cycle should be located at J,     * And all elements from J to i-1 move backward one position, filling the original AI (i), the AI value inserted into the space after the shift of J     * * This implementation of a card from I, inserted into the exact position. Loop through the process sequentially, and each element of the loop is eventually inserted into the position it should be in, thus sorting is achieved.     * [1, 2, 4, 5, 6, 7, 9, 3, 8] * [1, 2, 3, 4, 5, 6     , 7, 9, 8]     * @param array     * @param    up */Public static void Insertsort (Int[] Array, Boolean up) {for        (int. i=1;i<array.length;i++) {            int current = array[i];< C13/>int from=0,to=i-1;            for (int j=from;j<=to;j++) {                if (current <= array[j]) {for                    (int n=to;n>=j;n--) {                        array[n+1] = Array[n];                    }                    ARRAY[J] = current;                    break;                }            }            System.out.println (arrays.tostring (array));        }    }

  

/** * Bubble Sort
* Select the first element, and then, in turn, compare it to the following element, and if it is larger than the subsequent element, swap it down, and ensure that the largest element is placed on the last side. * Loop This process */public static void Maopaosort (Int[] Array, Boolean up) {if (UP) {for (int j = 0;j<array. length-1;j++) {int current = array[0]; for (int i=0;i< array.length-j-1;i++) {if (current> array[i+1]) {array[i]= A RRAY[I+1]; Array[i+1]=current; }else if (current< array[i+1]) {current = array[i+1]; }}}}else{for (int j = 0;j<array.length-1;j++) {int Curren t = array[0]; for (int i=0;i< array.length-j-1;i++) {if (current < array[i+1]) {array[i]= ARRAY[I+1]; Array[i+1]=current; }else if (current > Array[i+1]) {current = array[i+1]; } }}} System.out.println (Arrays.tostring (array)); }

  

Java Interview Algorithm questions

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.