0% found this document useful (0 votes)
18 views

DSA LAB Experiments

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

DSA LAB Experiments

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Experiment-1

(a) To Implement Matrix Addition (Subtraction) using Array Data Structure.


Code:
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<vector<int>> a = {{1,2},{3,4}};
vector<vector<int>> b = {{5,6},{7,8}};
vector<vector<int>> res(2, vector<int>(2));
for(int i=0; i<2; i++){
for(int j = 0; j<2; j++){
res[i][j] = a[i][j] + b[i][j];
cout<< res[i][j] << " ";
}
cout<< endl;
}
return 0;
}
Output:
68
10 12
(b) To implement Matrix Multiplication using Array Data Structure.
Code:
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<vector<int>> a = {{1,2,3},{3,4,5}};
vector<vector<int>> b = {{5,6},{7,8},{9,1}};
vector<vector<int>> res(2, vector<int>(2));
for(int i=0; i<2; i++){
for(int j=0; j<2; j++){
for(int k=0; k<3; k++){
res[i][j] += a[i][k]*b[k][j];
}
cout<< res[i][j]<< " ";
}
cout<< endl;
}
return 0;
}
Output:
46 25
88 55
(c) To implement Matrix Transpose using Array Data Structure.
Code:
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<vector<int>> a = {{1,2,3},{3,4,5}};
for(int j=0; j<3; j++){
for(int i=0; i<2; i++){
cout<< a[i][j]<< " ";
}
cout<< endl;
}
return 0;
}
Output:
13
24
35
Experiment-2
(a) To Implement Linear Search using Arrays.
Code:
#include <iostream>
#include <vector>
using namespace std;
bool LinearSearch(vector<int> arr, int target){
for(int i=0; i<arr.size(); i++){
if(arr[i]==target)
return true;
}
return false;
}
int main() {
vector<int> a = {12, 5, 38, 4, 75};
int toFind = 4;
cout<< LinearSearch(a,toFind);
return 0;
}
Output:
1
(b) To implement Binary Search using Arrays.
Code:
#include <iostream>
#include <vector>
#include<algorithm>
using namespace std;
bool BinarySearch(vector<int> arr, int target){
sort(arr.begin(), arr.end());
int l=0, r=arr.size()-1, m;
while(l<=r){
int m = (l+r)/2;
if(arr[m]==target)
return true;
else if(arr[m]>target)
r = m-1;
else
l = m+1;
}
return false;
}
int main() {
vector<int> a = {12, 5, 38, 4, 75};
int toFind = 3;
cout<< BinarySearch(a,toFind);
return 0;
}
Output:
0
Experiment-3
(a) To Implement Bubble Sort using Arrays.
Code:
#include <iostream>
#include <vector>
using namespace std;
void BubbleSort(vector<int> &arr){
int n = arr.size();
for(int i=n-1;i>=1;i--){
for(int j=0;j<i;j++){
if(arr[j+1]<arr[j])
swap(arr[j+1],arr[j]);
}
}
for(int i=0;i<n;i++)
cout<< arr[i] << " ";
}
int main() {
vector<int> a = {8,6,9,5,4};
BubbleSort(a);
return 0;
}
Output:
45689
(b) To implement Selection Sort using Arrays.
Code:
#include <iostream>
#include <vector>
using namespace std;
void SelectionSort(vector<int> &arr){
int n = arr.size();
for(int i=0;i<n;i++){
int minI = i;
for(int j=i;j<n;j++){
if(arr[j]<arr[minI])
minI = j;
}
swap(arr[i],arr[minI]);
}
for(int i=0;i<n;i++)
cout<< arr[i] << " ";
}
int main() {
vector<int> a = {8,6,9,5,4};
SelectionSort(a);
return 0;
}
Output:
45689

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy