Java Log--2016.6.3

Source: Internet
Author: User

1) static initialization of two-dimensional arrays:

int[][] arr = new int[][] {{}, {4,5}, {6,7}}

2) Yang Hui triangle:


/**
* Requirements: Calculate and simulate Yang Hui triangle
such as
* 1
* 1 1
* 1 2 1
* 1 3 3 1
*/
public class Pascaltriangle {

Number of rows to simulate
public static int row = 10;

public static void Main (string[] args) {

int[][] arr = new int[row][];
for (int i = 0; i < arr.length; i++) {
int len = i + 1;
Arr[i] = new Int[len];
for (int j = 0; J < Len; J + +) {
if (j = = 0 | | j = = i) {
ARR[I][J] = 1; Head and tail number
} else {
ARR[I][J] = arr[i-1][j]//Middle number
+ arr[i-1][j-1];
}
}
}

Verifying the correctness of numerical simulations
For (int[] ele:arr) {
System.out.println (java.util.Arrays.toString (ele));
//        }

Print Yang Hui triangle
Printpascaltriangle (arr);

}

/**
* Requirement: Print Yang Hui triangle in original form
* such as: 1
* 1 1
* 1 2 1
* 1 3 3 1
* 1 4 6 4 1
*/
public static void Printpascaltriangle (int[][] arr) {
if (arr = = NULL | | arr.length = = 0) return;

int rows = Arr.length;

for (int i = 0; i < arr.length; i++) {
if (arr[i] = = NULL | | arr[i].length = = 0) return;

int left = Row-i;

while (true) {
if (left--= = 0) break;
System.out.print ("\ t");
}

for (int j = 0; J < Arr[i].length; J + +) {
System.out.print (Arr[i][j]);
System.out.print ("\t\t");
}

System.out.println ("\ n");
}
}
}

3) Hanoi:

private void Hannuo (int n, char from,
Char to, char depend) {
if (n==1) {
Move (n, from, to);
} else {
Hannuo (n-1, from, depend, to);
Move (n, from, to);
Hannuo (n-1, depend, to, from);
}
}

private void Move (int n, char from, char to) {
System.out.println (...);
}
4) for Each loop trap: For arrays of primitive types, the contents of the array cannot be changed inside the loop, but for reference (object) arrays or multidimensional arrays, the original content can be modified inside the loop.

5) Sorting algorithm:

1. Select sort: Direct selection and heap sorting

2. Exchange sort: bubble sort, quick sort

3. Insert Sort: Direct insert, binary sort, shell sort

4. Merge sort

6) Bubble Sort thinking model (Bubble sort):

This is the simplest sort method, the basic idea:

The unsorted elements are compared to the next two element size relationship, if larger than the swap position, after the first round of comparison sorted by the maximum value, and then use the same method to compare the remaining elements one by one.

It can be seen that if there are n elements, then a total of N-1 wheel comparison, the first m-wheel to be n-m times comparison. (if 6 elements, to make 6-1 round comparison, the first round compared 6-1 times, the third round compared 6-3 times).

7) Select sort (Selection sort):

Basic idea: Select an index position of the element, and then compare with the following elements, if greater than the swap position, after the first round of comparison sort can draw the minimum value, and then use the same method to compare the remaining elements one by one.

You can see the selection sort, the first round will pick the minimum value, the second round will pick the second small value, until the end.

The first round is compared from the arr[0] and later elements, the second round from the arr[1] and the subsequent elements, and so on. n the number of N-1 wheels to be carried out. Select sort each round is exchanged only once, with a higher efficiency relative to the bubbling sort.

8) Search Method:

1) Sequential search: Low efficiency

2) Binary search: high efficiency, but only in ordered sequence of search

Java Log--2016.6.3

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.