Q37.Let `LA` be a sorted list of integers in ascending order, and `X` be an integer. The procedure `insert(LA, X)` returns a list `LB` where `X` is added to `LA` such that `LB` remains sorted. The procedure may have mistakes. Identify all such mistakes, if any.
```
Procedure insert(LA, X)
LB = []
flag = True
foreach A in LA {
if(flag) {
if(X <= A) {
LB = LB ++ [A]
flag = False
}
}
LB = LB ++ [A]
}
if(not flag) {
LB = LB ++ [X]
}
return(LB)
End insert
```
Save
Check
Details
Q37
6 Aug 2023
Q37.Let `LA` be a sorted list of integers in ascending order, and `X` be an integer. The procedure `insert(LA, X)` returns a list `LB` where `X` is added to `LA` such that `LB` remains sorted. The procedure may have mistakes. Identify all such mistakes, if any.
```
Procedure insert(LA, X)
LB = []
flag = True
foreach A in LA {
if(flag) {
if(X <= A) {
LB = LB ++ [A]
flag = False
}
}
LB = LB ++ [A]
}
if(not flag) {
LB = LB ++ [X]
}
return(LB)
End insert
```