C # sorting-Small Test

Source: Internet
Author: User
Code

Int [] source = new int [] {1, 34, 3, 45, 3 };
Printint (source );
// Sort data in ascending order
/* // Select
For (INT I = 0; I <source. length; I ++)
{
For (Int J = I + 1; j <source. length; j ++)
{
// If the front part is greater than the back part, change the location
If (source [I]> source [J])
{
Int temp = source [I];
Source [I] = source [J];
Source [J] = temp;
}
}
}*/
/* // Bubble
Int J = 1;
Bool done = false;
While (j <source. Length &&! Done)
{
Done = true;
For (INT I = 0; I <source. Length-1; I ++)
{
// [Two adjacent comparisons] If the front is greater than the back, the front and back positions are changed.
If (source [I]> source [I + 1])
{
Done = false;
Int temp = source [I];
Source [I] = source [I + 1];
Source [I + 1] = temp;
}
}
J ++;
}*/
// Sort by insert
For (INT I = 1; I <source. length; I ++)
{
Int temp = source [I];
Int J = I;
While (j> 0 & source [J-1]> temp)
{
Source [J] = source [J-1];
J --;
}
Source [J] = temp;
}

Test the sorting.

1. Array definition, and initialize int [] source = new int [] {1, 34, 3, 45, 3 };

2. Print the array and print it in a for loop.

3. Sequencing [from small to large examples]

(1) I = 0; I <length; I ++; {J = I + 1; j <length; j ++. compare all (j) after I and I. If I is greater than the latter, change the position ---- one round, I places the smallest unordered value. -- select sorting

(2) j = 1; flag = fasle while (j <length and flag is false) {for (I = 1; I <length-1; I ++) which of the two adjacent ones (I and I + 1) is relatively small? Put the first one ---- one round down, and the biggest one is put to the end. --- called Bubble Sorting

(3) I = 1; I <length, I + {write down the value of I, j = I while (before I (before J) if there is greater than I, so that the value of I is equal to a large value.) J is equal to the original value of I. ---- after a round, I is the largest of the sorted I values -- insert sorting.

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.