Data Structures Labbweek4: ENROLL: 20103181 Name: Sumit Singh Batch: B7

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

DATA STRUCTURES

LAB B WEEK 4
ENROLL: 20103181
NAME: SUMIT SINGH
BATCH: B7

#include <iostream>

using namespace std;

int main()

int search,n=5,i;

int arr[5];

cout << "Enter a 5 value array in increasing order:" << endl;

for(int a = 0; a < 5; a++)

cin >> arr[a];

cout << "Enter value for searching in the given array" <<endl;

cin >> search;

for(i = 0; i < n; i++)

if(arr[i] == search)

cout <<"The number is present at:" <<i << endl;

if(i == n)

{
cout << "The number is not present." << endl;

#include<bits/stdc++.h>

using namespace std;

int binarysearch(vector<int> nums, int n, int target)

int low = 0, high = n - 1;

while (low <= high)

int mid = (low + high)/2;

if (target == nums[mid]) {

cout<<"Present at position "<<mid+1;

return 1;

else if (target < nums[mid]) {

high = mid - 1;

else {

low = mid + 1;
}

return 0;

int main()

int s;

cout<<"Input size of array:";

cin>>s;

cout<<endl;

vector<int> arr(s);

cout<<"\nElements should be sorted!!!!!!\n";

cout<<"Input elements :";

for(int i=0;i<s;i++)

cin>>arr[i];

cout<<endl;

cout<<"Input element you want to search:";

int n;

cin>>n;

cout<<endl;

if(!binarysearch(arr,arr.size(),n))

cout<<"Not present!";

}
3

#include<bits/stdc++.h>

using namespace std;

int kthelement(vector<int> nums, int n, int target)

sort(nums.begin(),nums.end());

return nums[target-1];

int main()

int s;

cout<<"Input size of array:";

cin>>s;

cout<<endl;

vector<int> arr(s);

cout<<"Input elements :";

for(int i=0;i<s;i++)

cin>>arr[i];

cout<<endl;

cout<<"Input k :";

int n;

cin>>n;

cout<<endl;

cout<<kthelement(arr,arr.size(),n)<<endl;

}
5

#include<bits/stdc++.h>

using namespace std;

int searchstring(vector<string> arr, int size, string str)

for(int i=0;i<size;i++)

if(arr[i]==str)

return i;

return -1;

int main()

int s;

cout<<"Input size of string array:";

cin>>s;

cout<<endl;

vector<string> arr(s);

cout<<"Input strings :";

for(int i=0;i<s;i++)

cin>>arr[i];

cout<<endl;

cout<<"Input string you want to search :";


string str;

cin>>str;

cout<<endl;

if(searchstring(arr,arr.size(),str)==-1)

cout<<"The string "<<str<<" is not present"<< endl;

else

cout<<"The string is present at index : "<<searchstring(arr,arr.size(),str)<<endl;

You might also like