What is the output of the code given below? [code] void setup() { Serial.begin(9600); } void loop() { int i, j, n=3, temp;

Tam ca

New member
What is the output of the code given below?
Code:
void setup() {
    Serial.begin(9600);
}
void loop() {
    int i, j, n=3, temp;
    int a[3]={2, 1, 3};
    for(i=0;i<n-1;i++) {
        for(j=0;j<n-1-I;j++) {
            if(a[j]>a[j+1]) {
                temp=a[j];
                a[j]=a[j+1];
                a[j+1]=temp;
            }
        }
    }
    for(i=0;i<n;i++) {
        Serial.print(a[i]);
        Serial.print(“ ”);
    }
}
A. 1 2 3
B. 3 2 1
C. 1 3 2
D. 2 1 3
 
Answer: A
Explanation: The above code demonstrates the Bubble Sort Technique using the concept of nested for-loop. Bubble Sort is a sorting technique. The version which we have used here sorts the contents (numbers) of the array in ascending order.
 

Tìm kiếm

Back
Top