Skip to content

Data Structures πŸ“ŠπŸ“Š and Algorithms πŸ“ using Java β˜• πŸ‘¨β€πŸ’»

Notifications You must be signed in to change notification settings

kimbo-slicee/java-Data-Structures

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

23 Commits
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

INTRODUCTION TO ➑ DATA STRUCTURES πŸ“Š

Static Badge Maven project

What are Data Structures? πŸ€”

A specialized format for organizing, processing, retrieving and storing data.

Data

Why we need Data Structures

After organizing data, it becomes easy to process it. It is akin to searching for your clothing within an unorganized room, a process that consumes considerable time and energy, ultimately leading to frustration. Therefore, it is advisable to organize your living space for a quick and efficient retrieval of items. An important consideration is that this approach minimizes energy expenditure.

Type of Data Structures

data-Structure Data Structures

Algorithms

An Algorithm is a set of instructions to perform a task or to solve a given problem, for example

Analysis of Algorithms

Analysis of algorithm deals with finding the best algorithm which runs fast and takes in less memory

Time Complexity

It is the duration ⏱ taken by algorithm to run; the input processed by an algorithm helps to determine the time complexity
Note πŸ“:
in java we can use the System. CurrentTimeMille function to know the different of time of execution between tow algorithems that's just to simplify the time complexity, And there are specific mathematics tools to claclculate 🎰 the time complexity

Space Complexity

it is the amount of memory πŸ’Ύ or space taken by algorithm to run the memory required to process the input by an algorithm helps in determining the space complexity

Asymptotic Analysis of an Algorithm

Asymptotic analysis is a mathematical technique

used for understanding the behavior of algorithms as their input increases. It uses asymptotic notations to describe the growth rate or time complexity of an algorithm, which allows us to compare different algorithms and understand how they perform in realistic scenarios.

BigOnAnnotation

Asymptotic Notation

  • Asymptotic notations are mathematical tools to express the time complexity of algorithms for asymptotic analysis.
  • Asymptotic Notation is the mathematical tools used to describe the running time of an algorithm in terms of input size
  • Asymptotic Notation is used to describe the running time of an algorithmβ€”how much time an algorithm takes with a given input, n.
  • Asymptotic Notations help us in determining:
  • 1️⃣.Best Case βˆ’ Minimum time for the execution.🟒
  • 2️⃣.Average Case βˆ’ Average time for the execution.🟠
  • 3️⃣.Worst Case βˆ’ Maximum time for the execution.πŸ”΄ Note πŸ“:
    if there’s no input to the algorithm, Then it is considered to work in a constant time. Other than the "input" all other factors are considered to be constant.

Type of Asymptotic Notation

There are three notations for performing runtime analysis of an algorithm

  • Big (O) Notation
  • Theta (Θ) Notation
  • Omega (Ξ©) Notation

Omega (Ξ©) Notation

image description

  • it is A Formal way to express the lower bound of an algorithm's running time
  • Lower bound means for any given input this notation determines the best amount of time an algorithm can take to complete
  • For example:
    if We say certain algorithm takes 100 secs as the best amount of time, 100 secs will be lower bound of that algorithm. The algorithm can take more than 100 secs but will not take less than 100 secs

Big O(O) Notation

image description The Asymptotic Notation Ο(n) represents the upper bound of an algorithm’s running time. It measures or calculates the worst-case time complexity or the maximum or longest amount of time an algorithm can possibly take to complete. For example, O(log n) represents a binary search algorithm’s upper bound.

Rules of Big O(O) Notation πŸ“

  • it's a Single Processor
  • it performs Sequential Execution of statements
  • Assignment operation takes one unit of Time
  • Return statement takes in one unit of time
  • Arithmetical operation and Logical Operation takes one unit of Time
  • Other Small/single operations take one unit of time
  • Drop lower order terms: if n is huge we Drop lower order terms T=nΒ²+3n+1====>O(nΒ²)
  • Drop constant multipliers T=3nΒ²+6n+1 ====> O(nΒ²)

Theta Asymptotic Notation, ΞΈ

image description The Theta Asymptotic Notation Ο(n) represents the both lower bound and upper bound of an algorithm’s running time. It measures the average case of time complexity. When we use big-Θ notation, we’re saying that we have an asymptotically tight bound on the running time.

Why are asymptotic notations important?

Asymptotic notations provide a standardized way to compare and analyze algorithms’ efficiency, making it easier to assess their scalability and performance characteristics. They help us focus on the core trends in algorithm behavior as input sizes become large, which is essential for designing robust and efficient software.

Calculating Time Complexity of Constant Algorithm

If an algorithm's time complexity is constant, it means that it will always run in the same amount of time; no matter the input size.For example, if we want to get the first item of an array,it doesn't matter how big the input size is.

Time Complexity of a constant  Example
in the First line we have four operations, so we have four Unite of Time and in the second line we have to operation, so it'd two unite of times T=4+2=6 ==> T=C(Constant)

In Complexity fo Constant Algorithm wherever the input size the Time complexity always still the O(1)
        β”Œβ€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β” 
        β”‚ Line number  | operations  | unite of Time β”‚   
        β”‚------------- | ----------- | ------------- β”‚ 
        β”‚      2       |   1+1+1+1   |      4        β”‚ 
        β”‚ ------------ | ----------- | ------------- β”‚
        β”‚      3       |     1+1     |      2        β”‚  
        β”‚ ------------ | ----------- | ------------- β”‚
        β””β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β”˜
        T=4+2=6
        T~(Constant)
        O(1)

Constant Time Complexity image

Calculating time Complexity of Linear Algorithm

An algorithm is said to have a linear time complexity when the running time increases linearly with the length of the input. When the function involves checking all the values in input data, with this order O(n). The above code shows that based on the length of the array (n), the run time will get linearly increased.
img.png

 β”Œβ€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β”
 β”‚ Line number  | operations  | unite of Time β”‚   
 β”‚------------- | ----------- | ------------- β”‚
 β”‚      29      |      1      |      1        β”‚
 β”‚ ------------ | ----------- | ------------- β”‚
 β”‚      30      |  1+3n+3+3n  |     6n+4      β”‚  
 β”‚ ------------ | ----------- | ------------- β”‚
 β”‚      31      | n(1+1+1+1)  |      4n       β”‚
 β”‚ ------------ | ----------- | ------------- β”‚
 β”‚      33      |     1+1     |      2        β”‚     
 β”‚ ------------ | ----------- | ------------- β”‚
 β””β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β”˜
  • T=1+6n+4n+2 = 10n+7 in big O roles we have to ignore the constant 10 and lower Value 7 so O(n)

TimeComplexityOfLinear-diagram

Note πŸ“:
time is proportional with the input size

Calculating time Complexity of Polynomial Algorithm

Understanding Polynomial Time Complexity: Polynomial-time complexity is denoted by the "O(n^k)" notation, where "n" represents the input size and "k" represents the degree of the polynomial. In simpler terms, a polynomial time algorithm's runtime is directly proportional to some power of the input size
img

Line number operations unite of Time
Header Title Here's this
Paragraph Text And more
                                                             T=6n+4+6nΒ²+4n+3n+n+1=9nΒ²+11+5=O(nΒ²)

Big O polynomial Alogorithem
Note πŸ“; that's why we shouldn't 🚨 have to use nested loop a lot in our program

What is an Array? πŸ€”

An array is a collection of items of the same data type stored at contiguous memory locations, And the size of the array is immutable (Fixed) cannot be modified. Array

About

Data Structures πŸ“ŠπŸ“Š and Algorithms πŸ“ using Java β˜• πŸ‘¨β€πŸ’»

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages