0% found this document useful (0 votes)
12 views5 pages

Lec-1 DSA 2002

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

Lec-1 DSA 2002

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

Data Structures

Lecture-1

Definition
A data structure is a way of organizing and storing data in a computer so that it can be accessed
and used efficiently. It refers to the logical or mathematical representation of data, as well as
the implementation in a computer program.

How Data Structure varies from Data Type

Data Type Data Structure

The data type is the form of a Data structure is a collection of


variable to which a value can be different kinds of data. That entire
assigned. It defines that the data can be represented using an
particular variable will assign the object and can be used throughout
values of the given data type only. the program.

It can hold value but not data. It can hold multiple types of data
Therefore, it is dataless. within a single object.

The implementation of a data type is Data structure implementation is


known as abstract implementation. known as concrete implementation.

There is no time complexity in the In data structure objects, time


case of data types. complexity plays an important role.

While in the case of data structures,


In the case of data types, the value the data and its value acquire the
of data is not stored because it only space in the computer’s main
represents the type of data that can memory. Also, a data structure can
be stored. hold different kinds and types of data
within one single object.

Data type examples are int, float, Data structure examples are stack,
double, etc. queue, tree, etc.

Saneev Kumar Das | CENTURION UNIVERSITY OF TECHNOLOGY AND MANAGEMENT, BHUBANESWAR


Types

 Linear data structure: Data structure in which data elements are arranged
sequentially or linearly, where each element is attached to its previous and next
adjacent elements, is called a linear data structure.
Examples of linear data structures are array, stack, queue, linked list, etc.
 Static data structure: Static data structure has a fixed memory size. It is
easier to access the elements in a static data structure.
An example of this data structure is an array.
 Dynamic data structure: In the dynamic data structure, the size is not fixed.
It can be randomly updated during the runtime which may be considered
efficient concerning the memory (space) complexity of the code.
Examples of this data structure are queue, stack, etc.
 Non-linear data structure: Data structures where data elements are not placed
sequentially or linearly are called non-linear data structures. In a non-linear data
structure, we can’t traverse all the elements in a single run only.
Examples of non-linear data structures are trees and graphs.

Saneev Kumar Das | CENTURION UNIVERSITY OF TECHNOLOGY AND MANAGEMENT, BHUBANESWAR


Execution Time Calculation

/* Program to demonstrate time taken by function fun() */

#include <stdio.h>

#include <time.h>

// A function that terminates when enter key is pressed

void fun()

printf("fun() starts \n");

printf("Press enter to stop fun \n");

while(1)

if (getchar())

break;

printf("fun() ends \n");

// The main program calls fun() and measures time taken by fun()

int main()

// Calculate the time taken by fun()

clock_t t;

t = clock();

fun();

t = clock() - t;

double time_taken = ((double)t)/CLOCKS_PER_SEC; // in seconds

printf("fun() took %f seconds to execute \n", time_taken);

return 0;

Saneev Kumar Das | CENTURION UNIVERSITY OF TECHNOLOGY AND MANAGEMENT, BHUBANESWAR


Find sum of ‘n’ natural numbers

Program-1
/* Program to demonstrate time taken by function fun() */

#include <stdio.h>

#include <time.h>

// A function that terminates when enter key is pressed

int findSum(int n)

int sum = 0;

for (int i = 0; i<=n; i++)

sum = sum + i;

return sum;

// The main program calls fun() and measures time taken by fun()

int main()

// Calculate the time taken by fun()

clock_t t;

t = clock();

findSum(10000000);

t = clock() - t;

double time_taken = ((double)t)/CLOCKS_PER_SEC; // in seconds

printf("findSum(int n) took %f seconds to execute \n", time_taken);

return 0;

Saneev Kumar Das | CENTURION UNIVERSITY OF TECHNOLOGY AND MANAGEMENT, BHUBANESWAR


Program-2
/* Program to demonstrate time taken by function fun() */

#include <stdio.h>

#include <time.h>

// A function that terminates when enter key is pressed

int findSum(int n)

return n*(n+1)/2;

// The main program calls fun() and measures time taken by fun()

int main()

// Calculate the time taken by fun()

clock_t t;

t = clock();

findSum(10000000);

t = clock() - t;

double time_taken = ((double)t)/CLOCKS_PER_SEC; // in seconds

printf("findSum(int n) took %f seconds to execute \n", time_taken);

return 0;

The difference in execution time shows that Program-2 performs better than Program-1. But, execution time is
dependent on the system specifications. Thus, there comes the concept of Asymptotic notation.

Time and Space Complexity


https://www.simplilearn.com/tutorials/data-structure-tutorial/time-and-space-complexity

https://www.geeksforgeeks.org/understanding-time-complexity-simple-examples/

Saneev Kumar Das | CENTURION UNIVERSITY OF TECHNOLOGY AND MANAGEMENT, BHUBANESWAR

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