C # Array Brief

Source: Internet
Author: User

Array declaration and assignment: int[] a=new int[6];

Array initializer: int[] a=new int[2]{1,3};

Array initializer simplified version: int[] a={1,3};

Array Initial value: Number type: 0

Bool:false
Char:
String:null

Suppose there is an array of nums, with a length of 5, to sort it in ascending order

Array length: A. Length;

Array sorting:

First, Exchange sort

Sorting ideas: 

    1. In the range of subscript 0-4, the smallest number in the range is referred to subscript 0
    2. In the range of subscript 1-4, the smallest number in the range is referred to subscript 1
    3. In the range of subscript 2-4, the smallest number in the range is referred to subscript 2
    4. In the range of subscript 3-4, the smallest number in the range is referred to subscript 3
    5. Sort done!

Implementation code:

  

 for(inti =0; I < Nums. Length-1; i++){    //in the I-(nums. LENGTH-1) within the range, the smallest number in the range is referred to I     for(intj = i +1; J < Nums. Length; J + +)    {        if(Nums[i] >Nums[j]) {        //Exchange            inttemp =Nums[i]; Nums[i]=Nums[j]; NUMS[J]=temp; }    }}                    

Second, bubble sort

Sorting ideas:

    • Sink the largest number to the bottom

Or

    • Take the smallest number to the top

Implementation code:

 for(inti = Nums. Length-1; i >0; i--){    //within the range of 0-i, the largest number in the range is sunk to I     for(intj =0; J < I; J + +)    {        if(Nums[j] > nums[j+1])        {            //Exchange            inttemp =Nums[j]; NUMS[J]= nums[j+1]; Nums[j+1] =temp; }    }}

  

C # Array Brief

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.