New users seek and kill

Source: Internet
Author: User

Sort the two arrays and merge them into another array. The merged array is also ordered (Jie Won't let the bubble, so the old man writes it with blood !).


Int main (INT argc, const char * argv [])
{
// Define two 10-sized arrays and randomly allocate any value within 100;
Int str1 [10] = {0 };
Int str2 [10] = {0 };
Int A4 = 0; // used to store the intermediate number in the array str1
Int B4 = 0; // used to store the intermediate number in the str2 Array
Printf ("the two arrays are \ n ");
For (INT I = 0; I <10; I ++ ){
Str1 [I] = arc4random () % 100;
Printf ("%-2D", str1 [I]); // generate a random array str1 and print
}
Printf ("\ n ");
For (INT I = 0; I <10; I ++ ){
Str2 [I] = arc4random () % 100;
Printf ("%-2D", str2 [I]); // generate a random array str2 and print
}
Printf ("\ n ");
For (INT I = 0; I <9; I ++) {// bubble sort the array str1
For (Int J = 0; j <9-I; j ++ ){
If (str1 [J]> str1 [J + 1]) {
A4 = str1 [J];
Str1 [J] = str1 [J + 1];
Str1 [J + 1] = A4;
}
}
}
For (INT I = 0; I <9; I ++) {// bubble sort the array str2
For (Int J = 0; j <9-I; j ++ ){
If (str2 [J]> str2 [J + 1]) {
B4 = str2 [J];
Str2 [J] = str2 [J + 1];
Str2 [J + 1] = B4;
}
}
}
Printf ("the sorted results are :");
Printf ("\ n ");
For (INT I = 0; I <10; I ++ ){
Printf ("%-2D", str1 [I]);
}
Printf ("\ n ");
For (INT I = 0; I <10; I ++ ){
Printf ("%-2D", str2 [I]);
}
Printf ("\ n"); // output the sorted result and wrap it
// Merge the two Arrays
Int str3 [20] = {0}; // used to store the new generated Array
Int A = 0, B = 0, c = 0; // records the number of elements in three groups.
If (str1 [9] <str2 [9]) {// determine the size of the last number of two arrays, this If statement is used to generate and sort the new array when the last value of the str1 array is the largest.
While (A <10 ){
If (str1 [a] <str2 [B]) {
Str3 [c] = str1 [a];
A ++;
C ++;
} Else {
Str3 [c] = str2 [B];
B ++;
C ++;
}
}
While (A = 10 & C <20 ){
Str3 [c] = str2 [B];
B ++;
C ++;
}
}
If (str1 [9]> str2 [9]) {// This If statement is used to generate and sort the new array when the last value of the str2 array is the largest.
While (B <10 ){
If (str1 [a] <str2 [B]) {
Str3 [c] = str1 [a];
A ++;
C ++;
} Else {
Str3 [c] = str2 [B];
B ++;
C ++;
}
}
While (B = 10 & C <20 ){
Str3 [c] = str1 [a];
A ++;
C ++;
}
}
Printf ("the new array is :");
For (INT I = 0; I <20; I ++ ){
Printf ("% d", str3 [I]);
}
Return 0;
}

New users seek and kill

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.