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.