Professional Documents
Culture Documents
21BCS5701 - Navya Jain - DSA - 1.2
21BCS5701 - Navya Jain - DSA - 1.2
Experiment-1.2
1. Start
4. If arr[i]==num
6. Print Index
7. Exit.
Algorithm of Question 2 :
1) Start
4) First =0
5) Last=9
6) Do mid=(first+last)/2
7) If a[mid]==num
8) Print mid
DS LAB WORKSHEET
9) Else if a[mid]<num
13) If (first>last)
14) Print
15) Exit
Program Code:
2) #include <iostream>
3) using namespace std;
4) int main()
5) {
6) int arr[5], num, index;
7) cout << " Enter 5 elements :";
8) for (int i = 0; i < 5; i++)
9) cin >> arr[i];
10) cout << " Enter number to search : ";
11) cin >> num;
12) for (int i = 0; i < 5; i++)
13) {
14) if (arr[i] == num)
15) {
16)
17) index = i; // returning the number what we have founded
18) cout << " Found at index number " << index;
19) break;
20) }
21) }
22)
23) return 0;
24) }
#include <iostream>
DS LAB WORKSHEET
return 0;
}
Output :
DS LAB WORKSHEET
Time and Space complexity: Time complexity for linear search of array is order
of n i.e., O(n). Time-complexity for binary search of array is order of log n. i.e.,
O(log n).