Lab1 185
Lab1 185
Sem-Sec-Group: 5 C 2
Linear Search:
Source Code:
namespace std;
== target) { return i; }
return -1;
if (result != -1) {
cout << "Element found at index " << result << endl;
} else {
return 0;
}
Output:
Binary Search:
Source Code:
#include <iostream>
main ()
" Define the size of the array: " << endl; cin >>
num;
cout << " Enter the values in sorted array either ascending or descending order: "
<< endl;
} st = 0;
end = num - 1;
cout << " Define a value to be searched from sorted array: " << endl;
mid = ( st + end ) / 2;
if (arr[mid] == tgt)
cout << " Element is found at index " << (mid + 1);
return 0;
Output:
int n)
i++)
{ int min_idx = i;
{ if (arr[j] <
arr[min_idx])
min_idx = j;
} if (min_idx != i)
swap(arr[min_idx], arr[i]);
sizeof(arr) / sizeof(arr[0]);
Output: