dsa codes
dsa codes
dsa codes
int i = 0, j = 0, k = left;
while (i < n1 && j < n2) {
if (a[i] <= b[j]) {
arr[k++] = a[i++];
} else {
arr[k++] = b[j++];
}
}
int main() {
int n;
cin >> n;
int arr[n];
cout << "Enter the elements:\n";
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
merge_sort(arr, 0, n - 1);
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
cout << endl;
return 0;
}
// BUBBLE SORT
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
int arr[n];
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
bubble_sort(arr, n);
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
cout << endl;
return 0;
}
// INSERTION SORT
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter the number of elements: ";
cin >> n;
int arr[n];
insertion_sort(arr, n);
cout << "Sorted array:\n";
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
cout << endl;
return 0;
}
// INSERTION SORT
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter the number of elements: ";
cin >> n;
int arr[n];
selection_sort(arr, n);
return 0;
}
// LINEAR SEARCH
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter the number of elements: ";
cin >> n;
int arr[n];
int key;
cout << "Enter the element to search for: ";
cin >> key;
return 0;
}
// BINARY SEARCH
#include <iostream>
using namespace std;
if (arr[mid] == key) {
return mid; // Return the index if the key is found
} else if (arr[mid] < key) {
left = mid + 1; // Search in the right half
} else {
right = mid - 1; // Search in the left half
}
}
return -1; // Return -1 if the key is not found
}
int main() {
int n;
cout << "Enter the number of elements: ";
cin >> n;
int arr[n];
return 0;
}
// LINKED LIST
#include <iostream>
using namespace std;
class Node{
public:
int data;
Node *next;
};
void insert_front(Node **head,int data ){
Node *ptr = new Node();
ptr->data = data;
ptr->next = *head;
*head = ptr;
}
void delete_front(Node*& head){
if(head==nullptr){
cout<<"linked list is empty"<<endl;
return;
}
Node *temp = head;
head = head->next;
delete temp;
}
void display(Node *node){
while(node!=nullptr){
cout<<node->data<<endl;
node = node->next;
}
cout<<endl;
}
int main(){
Node *head = nullptr;
insert_front(&head,10);
insert_front(&head,20);
display(head);
delete_front(head);
display(head);
return 0;
}
include <iostream>
using namespace std;
class Node{
public:
int data;
Node *next;
};
void insert_front(Node **head,int data){
Node *nextptr = new Node();
nextptr->data = data;
nextptr->next = *head;
*head = nextptr;
}
void display(Node *node){
while(node!=nullptr){
cout<<node->data<<endl;
node = node->next;
}
}
int main(){
Node *head = nullptr;
insert_front(&head,10);
insert_front(&head,20);
display(head);
return 0;
}
#include <iostream>
using namespace std;
class Node{
public:
int data;
Node *next;
};
void insert_last(Node **head,int data){
Node *nextptr = new Node();
nextptr->data = data;
nextptr->next = nullptr;
if(*head==nullptr){
*head = nextptr;
}else{
Node *temp = *head;
while(temp->next!=nullptr){
temp=temp->next;
}
temp->next = nextptr;
}
cout<<nextptr->data<<endl;
}
int main(){
Node *head = nullptr;
insert_last(&head,10);
insert_last(&head,20);
return 0;
}