/Data-Structures-Algorithm-DSA-

Data structures are formats for organizing and storing data, like arrays, linked lists, trees, and graphs. Algorithms are systematic instructions for performing tasks, such as sorting and searching. These structures and algorithms are integral to computing, enhancing data manipulation.

Primary LanguageJavaScript

Minimum & Maximum (DSA)

function min5Log(n){
    for(let i = 0; i <= Math.min(5, n); i++){
        console.log(i)
    }
}
// min5Log(90000000000000)

// maximum
function max5Log(n){
    for(let i = 0; i <= Math.max(10, n); i++){
        console.log(i)
    }
}

max5Log(-1)

Visualization (min&max)

Visualize code

Sum Of Array (DSA)

// space complexity o(1), o(n)

function sumOfArray(arr){
    total = 0
    for(let i = 0; i < arr.length; i++){
        total = arr[i]
    }
    return total
}

// console.log(sumOfArray([25, 25, 50]))



// another example
function sumOfArray(arr){
    newArr = []
    for(let i = 0; i < arr.length; i++){
        newArr.push(arr[i] *2)
    }
    return newArr
}

console.log(sumOfArray([25, 25, 50]))

Visualization (sumOfArray)

Visualize code -(sumOfArray)

character Count (DSA)

function charCount(str){
    let charList = {}

    for (let i = 0; i < str.length; i++){
       let char = str[i].toLowerCase()

       if(charList[char] > 0){
        charList[char]++
       }else{
        charList[char] = 1
       }
    }
    return charList
}
console.table(charCount("Anower hossain"))

Visualization (charCount)

Visualize code -(charCount)

Binary Search (DSA)

function binarySearch(arr, value){
    let start = 0
    let end = arr.length -1


    while(start <= end){
        let middle = Math.round((start + end) / 2) // use floor 

        if(arr[middle] === value) return middle

        if(value > arr[middle]){
            start = middle + 1
        }else if(value < arr[middle]){
            end = middle -1
        }


    }
    return -1
}
console.log(binarySearch([2,3,4,7,34,66], 7))

Visualization (binarySearch)

Visualize code -(binarySearch)

Recursion (DSA)

function countDown(n){
    // normal method
    // for(let i = n; i > 0; i--){
    //     console.log(i)
    // }
    // Recursin methods
    console.log(n)
    n-- 
    if(n > 0) {
        countDown(n)
    }
}
// countDown(10)


//Another example
function sumOfNumber(n){
    let sum = 0
    for(let i = n; i >= 0; i--){
        sum += i
    }
    return sum
}

// console.log(sumOfNumber(10))

//Recursion 
function sumOfNumberRe(n){
    if(n < 0)return 0
    

    return n + sumOfNumberRe(n - 1)
}

console.log(sumOfNumberRe(10))

/*
Operation
============
10 + 9
19 + 8
27 + 7
34 + 6
40 + 5
45 + 4
49 + 3
52 + 2
54 + 1
55 + 0

finel result = 55
*/

Visualization (recursion)

Visualize code -(recursion)

Proble Solving(DSA)

let temperature = [-2, 4, 45, "error", -6, -8, 43]

function getHigherAndLower(arr){
    // make a higher variable
    let higher = arr[0]
    
    // make a lower variable
    let lower = arr[0]

    // iterate the whole Array
    for(let i = 0; i < arr.length; i++){
    
    // let's check is there has any error
    if(typeof arr[i] !== "number") continue 
    console.log(typeof arr[i])


    // if higher variable has smaller number than our current number than value will be our current element
    if(higher < arr[i]){
        higher = arr[i]
    }
    
    // if lower variable has bigger number than our current number than value will be our current element
    if(lower > arr[i]){
        lower = arr[i]
    }
}
    
    
    // return our higher variable and lower variable
    return higher - lower

}

// getHigherAndLower([3, 5, -9])
// getHigherAndLower(temperature)


console.log(getHigherAndLower(temperature))

Visualization (problesolving)

Visualize code -(problesolving)

Analyze BigO (DSA)

function uniqueName(arr){

    let uniqueName = []
    for(let i = 0; i < arr.length; i++){
        let ele = arr[i]
        if(!uniqueName.includes(ele)){
            uniqueName.push(ele)
        }
    }
    return uniqueName
}


let nameArray = ['anower', 'shanto', 'rohan', 'rohan']
console.log(uniqueName(nameArray)) 

Visualization (analyzeBigO)

Visualize code -(analyzeBigO)

Bubble Sort(DSA)

// bubble Sort implementation

function bubbleSort(arr){
    let isSwap
    for(let i = 0; i < arr.length; i++){
        isSwap = false
        for(let j = 0; j < arr.length -i -1; j++){
        console.log("Swap = ", arr, arr[j], arr[j+1])
        if(arr[j] > arr[j+1]){
            let temp = arr[j]
            arr[j] = arr[j+1]
            arr[j+1] = temp
            isSwap = true
           }
        }
        if(!isSwap) break
    }
    return arr
}

console.log(bubbleSort([2, 53, 23, 1, -3]))

Visualization (bubbleSort)

Visualize code -(bubbleSort)

Linear Search (DSA)

Visualization (linearSearch)

Visualize code -(linearSearch)

Helper Function(DSA)

// statement 
// find the odd number and push empty array

function findOddNumber(arr){

    let result = []
    function helper(input){
        if(input.length === 0) return

        if(input[0] % 2 !== 0){
            result.push(input[0])
        }

        helper(input.slice(1))
    }

    helper(arr)

    return result //if someone say find length just add (result.length)
}

console.log(findOddNumber([2,3,56,8, 3, 6, 8, 5]))

Visualization (helperFunction)

Visualize code -(helperFunction)