Data sorting, 99 multiplication table, Yang Hui triangle

Source: Internet
Author: User
Tags array length

1. Select sortThe choice of sorting is a simple sort, the idea is: First number is marked as the maximum number, its position is the maximum number of positions, and then exclude the first number, using the first number and the remainder of the comparison, if the remaining number is greater than the first number, then continue to compare until the maximum number is found Finally, the position of the actual maximum number is determined by the default maximum number, if not, the position of the first number and the maximum number of positions are exchanged, then the first number is the actual maximum number. And so on, compare the remaining number, get descending order, and vice versa in ascending order.
        int[] Nums = {23, 12, 15, 9 };  for(inti = 0; i < nums.length; i++) {            intindex =i;  for(intj = i + 1; J < Nums.length; J + +) {                if(Nums[i] >Nums[j]) {Index=J; }} System.out.println ("First" + (i + 1) + "Pre-order:");            System.out.println (arrays.tostring (nums)); inttemp =Nums[i]; Nums[i]=Nums[index]; Nums[index]=temp; System.out.println ("First" + (i + 1) + "after Order:");        System.out.println (arrays.tostring (nums)); }

2. Bubble sortBubble sort (Bubble sort) is a simple sort algorithm. It repeatedly visited the sequence to sort, comparing two elements at a time, and swapping them out if they were wrong in the order. The work of the sequence of visits is repeated until no more need to be exchanged, that is, the sequence is sorted. The algorithm is named because the smaller elements will slowly "float" through the switch to the top of the sequence. The bubbling Sorting algorithm works as follows:
    1.   compare adjacent elements. If the first one is bigger than the second one, swap them both.
    2.   does the same work for each pair of adjacent elements, starting with the last pair from the first pair to the end. At this point, the last element should be the maximum number.
    3.   Repeat the above steps for all elements, except for the last one.
    4.   repeats the above steps each time for less and fewer elements until there are no pairs of numbers to compare.
1         int[] Nums = {23, 12, 15, 9 };2          for(inti = 0; i < nums.length; i++) {3System.out.print ("First" + (i + 1) + "Pre-order:");4 System.out.println (arrays.tostring (nums));5 6              for(intj = 0; J < nums.length-i-1; J + +) {7                 if(Nums[j] < Nums[j + 1]) {8                     inttemp =Nums[j];9NUMS[J] = nums[j + 1];TenNums[j + 1] =temp; One                 } A             } -  -System.out.print ("First" + (i + 1) + "after Order:"); the System.out.println (arrays.tostring (nums)); -}

3.99 Multiplication Table
1      Public Static voidPrintchengfabiao (inti) {2          for(intA = 1; a <= i; a++) {3              for(intb = 1; b <= A; b++) {4System.out.print (b + "*" + A + "=" + b * A + "\ T");5             }6 System.out.println ();7         }8}

4. Yang Hui triangle
1         int[] Nums =New int[8][];2          for(inti = 0; I <= nums.length-1; i++) {//number of control wheels3Nums[i] =New int[i + 1];//Open Space address, loop to each array assignment, array length is not fixed4              for(intj = 0; J <= I; J + +) {5                 if(i = = 0 | | j = 0 | | j = =i) {6NUMS[I][J] = 1;7}Else {8NUMS[I][J] = nums[i-1][j-1] + nums[i-1][j];9                 }TenSystem.out.print (Nums[i][j] + "\ T"); One             } A System.out.println (); -}

Data sorting, 99 multiplication table, Yang Hui triangle

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.