Skip to content

Beginner DSA

codingdud edited this page May 5, 2024 · 7 revisions

Beginner DSA (Data Structures and Algorithms)

These are basic implementations to get you started with each mentioned topic.

1. Arrays

Basic Implementation:

#include <iostream>
#include <vector>

using namespace std;

void printArray(const vector<int>& arr) {
    for (int i = 0; i < arr.size(); ++i) {
        cout << arr[i] << " ";
    }
    cout << endl;
}

int main() {
    // Creating an array of integers
    vector<int> arr = {1, 2, 3, 4, 5};

    // Printing the array
    cout << "Array: ";
    printArray(arr);

    // Accessing elements
    cout << "Element at index 2: " << arr[2] << endl;

    // Modifying elements
    arr[2] = 10;
    cout << "Modified Array: ";
    printArray(arr);

    return 0;
}

2. Basic Mathematics

Basic Implementation:

#include <iostream>

using namespace std;

int main() {
    // Basic arithmetic operations
    int a = 10, b = 5;

    cout << "Addition: " << a + b << endl;
    cout << "Subtraction: " << a - b << endl;
    cout << "Multiplication: " << a * b << endl;
    cout << "Division: " << a / b << endl;

    // Other mathematical operations
    int num = 25;
    cout << "Square root of " << num << ": " << sqrt(num) << endl;
    cout << "Absolute value of -10: " << abs(-10) << endl;

    return 0;
}

3. Searching

Basic Implementation:

#include <iostream>
#include <vector>

using namespace std;

int linearSearch(const vector<int>& arr, int target) {
    for (int i = 0; i < arr.size(); ++i) {
        if (arr[i] == target) {
            return i; // Return the index if found
        }
    }
    return -1; // Return -1 if not found
}

int main() {
    vector<int> arr = {10, 20, 30, 40, 50};
    int target = 30;

    int index = linearSearch(arr, target);

    if (index != -1) {
        cout << "Element found at index: " << index << endl;
    } else {
        cout << "Element not found" << endl;
    }

    return 0;
}

4. Sorting

Basic Implementation:

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

void printArray(const vector<int>& arr) {
    for (int i = 0; i < arr.size(); ++i) {
        cout << arr[i] << " ";
    }
    cout << endl;
}

int main() {
    vector<int> arr = {5, 2, 7, 1, 9};

    // Sorting the array
    sort(arr.begin(), arr.end());

    // Printing the sorted array
    cout << "Sorted Array: ";
    printArray(arr);

    return 0;
}

5. Strings

Basic Implementation:

#include <iostream>
#include <string>

using namespace std;

int main() {
    // Initializing strings
    string str1 = "Hello";
    string str2 = "World";

    // Concatenating strings
    string result = str1 + " " + str2;
    cout << "Concatenated String: " << result << endl;

    // Length of string
    cout << "Length of String: " << result.length() << endl;

    // Accessing characters
    cout << "First character: " << result[0] << endl;
    cout << "Last character: " << result[result.length() - 1] << endl;

    // Substring
    string substring = result.substr(6, 5); // Starting index, length
    cout << "Substring: " << substring << endl;

    return 0;
}
Clone this wiki locally