Presentation is loading. Please wait.

Presentation is loading. Please wait.

Data Structure and Algorithms

Similar presentations


Presentation on theme: "Data Structure and Algorithms"— Presentation transcript:

1 Data Structure and Algorithms
Lecture 01 Data Structure and Algorithms

2 Outline Basic Terminology – Data and Data Structure
Categories of Data Structure Data Structure Operations Algorithm Complexity

3 Questions ?? What is the Computer Science?
Why we need to study data structures?

4 CSE and Data Structure Organization of data processing and methods of data processing are a subject of Computer Science. A Data Structure is a collection of data organized in some logical pre-defined way. Studying Computer Science you will study different methods of data processing.

5 Data Structure A data structure is an arrangement of data in a computer's memory or even disk storage. The logical and mathematical model of a particular organization of data is called a data structure. A data structure is a particular way of storing and organizing data in a computer so that it can be used efficiently. The choice of a particular data model depends on the two considerations first; It must be rich enough in structure to mirror the actual relationships of the data in the real world. On the other hand, the structure should be simple enough that one can effectively process the data whenever necessary.

6 Categories of Data Structure
The data structure can be classified in to major types:  Linear Data Structure  Non-linear Data Structure

7 Categories of Data Structure
Linear Data Structure: A data structure is said to be linear if its elements form any sequence. There are basically two ways of representing such linear structure in memory. a) One way is to have the linear relationships between the elements represented by means of sequential memory location. These linear structures are called arrays. b) The other way is to have the linear relationship between the elements represented by means of pointers or links. These linear structures are called linked lists. The common examples of linear data structure are  Arrays  Linked lists  Queues  Stacks

8 Categories of Data Structure
Non-linear Data Structure: This structure is mainly used to represent data containing a hierarchical relationship between elements. The common examples of Non-linear data structure are  Graphs  Trees

9 Categories of Data Structure

10 Linear Data Structure Array One Dimensional Array
Multidimensional Array Array Addressing (One Dimensional) Array Addressing (Two Dimensional)

11 Linear Data Structure Array Data Structure
It can hold multiple values of a single type. Elements are referenced by the array name and an ordinal index. Each element is a value Indexing begins at zero. The array forms a contiguous list in memory. The name of the array holds the address of the first array element. We specify the array size at compile time, often with a named constant.

12 Linear Data Structure Array Data Structure
It can hold multiple values of a single type. Elements are referenced by the array name and an ordinal index. Each element is a value Indexing begins at zero. The array forms a contiguous list in memory. The name of the array holds the address of the first array element. We specify the array size at compile time, often with a named constant.

13 Linear Data Structure (Linked lists)
A linked list, or one way list, is a linear collection of data elements, called nodes, where the linear order is given by means of pointers. Dynamically allocate space for each element as needed. Next Data Node In linked list Each node of the list contains the data item & a pointer to the next node Collection structure has a pointer to the list Start Initially NULL Start

14 Linear Data Structure (Linked lists)
Start Data Next node Linked list with 2 nodes

15 Linear Data Structure (Linked lists)
INFO LINK 1 2 3 4 5 6 7 8 A M G N O 5 2 7 4 START START=3, INFO[3]=M LINK[3]=2, INFO[2]=A LINK[2]=5, INFO[5]=N LINK[5]=4, INFO[4]=G LINK[4]=7, INFO[7]=O LINK[7]=0, NULL value, So the list has ended 3

16 Linear Data Structure (Stack)
Stacks are a special form of collection with LIFO semantics Two methods - add item to the top of the stack - remove an item from the top of the stack Like a plate stacker

17 Linear Data Structure (Queues)
Like a stack, a queue is also a list. However, with a queue, insertion is done at one end, while deletion is performed at the other end The insertion end is called rear The deletion end is called front Insert Remove rear front

18 Non-Linear Data Structure
2 primary types: Trees Graphs All trees are graphs, but not all graphs are trees Recursion is useful and is the easiest way to process them.

19 Graphs Graphs can be directed or undirected and cyclic or acyclic
Graphs can have multiple references in and multiple references out (whereas tree node only has one reference in) Graphs can be directed or undirected and cyclic or acyclic

20 Trees Single parent 0 or more children
A node with no children is called a "leaf" The topmost node is called the "root"

21 Data structure operations
The data appearing in our data structure is processed by means of certain operations. In fact, the particular data structure that one chooses for a given situation depends largely on the frequency with which specific operations are performed. The following four operations play a major role: Traversing Accessing each record exactly once so that certain items in the record may be processed. (This accessing or processing is sometimes called 'visiting" the records.) Searching Finding the location of the record with a given key value, or finding the locations of all records, which satisfy one or more conditions. Inserting Adding new records to the structure. Deleting Removing a record from the structure.

22 Data structure operations
The following two operations, which are used in special situations, will also be considered: Sorting: Arranging the records in some logical order Merging: Combining the records in two different sorted files into a single sorted files

23 Algorithms An essential aspect to data structures is algorithms. Data structures are implemented using algorithms. An Algorithm is a finite step – by – step list of well defined instructions for solving a particular problem. It is used to manipulate the data contained in the data structures as in searching and sorting. It states explicitly how the data will be manipulated.

24 Complexity The complexity of an algorithm is a function describing the efficiency of the algorithm in terms of the amount of data the algorithm must process. There are two main complexity measures of the efficiency of an algorithm: Time complexity is a function describing the amount of time an algorithm takes in terms of the amount of input to the algorithm. Space complexity is a function describing the amount of memory (space) an algorithm takes in terms of the amount of input to the algorithm.

25 Data structure operations on Array
Traversing: means to visit all the elements of the array in an operation is called traversing. Insertion: means to put values into an array Deletion / Remove: to delete a value from an array. Sorting: Re-arrangement of values in an array in a specific order (Ascending / Descending) is called sorting. Searching: The process of finding the location of a particular element in an array is called searching. There are two popular searching techniques/mechanisms : Linear search and binary search and will be discussed later.

26 Data structure operations on Array
Traversing in Linear Array: It means processing or visiting each element in the array exactly once; Let ‘A’ is an array stored in the computer’s memory. If we want to display the contents of ‘A’, it has to be traversed i.e. by accessing and processing each element of ‘A’ exactly once.

27 Data structure operations on Array
This program will traverse each element of the array to calculate the sum and then calculate & print the average of the following array of integers. ( 4, 3, 7, -1, 7, 2, 0, 4, 2, 13) # define size 10 // another way // int const size = 10 int main() { int x[10]={4,3,7,-1,7,2,0,4,2,13}, i, sum=0,LB=0, UB = size; float av; for(i=LB,i<UB;i++) sum = sum + x[i]; av = (float)sum/size; printf( “The average of the numbers= %.2f“,av); return 0; }

28 Data structure operations on Array
Sorting in Linear Array: Sorting an array is the ordering the array elements in ascending (increasing from min to max) or descending (decreasing – from max to min) order. Example:  { }  {1, 2, 3, 4, 5,7} ascending order  { }  {7,5, 4, 3, 2, 1} descending order

29 Data structure operations on Array
Bubble Sort: The technique we use is called “Bubble Sort” because the bigger value gradually bubbles their way up to the top of array like air bubble rising in water, while the small values sink to the bottom of array. This technique is to make several passes through the array. On each pass, successive pairs of elements are compared. If a pair is in increasing order (or the values are identical), we leave the values as they are. If a pair is in decreasing order, their values are swapped in the array.

30 Data structure operations on Array
Bubble Sort:

31 Data structure operations on Array
Bubble Sort: The technique we use is called “Bubble Sort” because the bigger value gradually bubbles their way up to the top of array like air bubble rising in water, while the small values sink to the bottom of array. Below is the algorithm of Bubble Sort

32 Data structure operations on Array
Bubble Sort: #include <stdio.h> int const SIZE = 6 void BubbleSort(int [ ], int); int main() { int a[SIZE]= {77,42,35,12,101,6}; int i; cout<< “The elements of the array before sorting\n”; for (i=0; i<= SIZE-1; i++) prinf(“%d ”,a[i]); BubbleSort(a, SIZE); cout<< “\n\nThe elements of the array after sorting\n”; return 0; }

33 Data structure operations on Array
void BubbleSort(int A[ ], int N) { int i, pass, hold; for (pass=1; pass<= N-1; pass++) for (i=0; i<= SIZE-pass; i++) if(A[i] >A[i+1]) hold =A[i]; A[i]=A[i+1]; A[i+1]=hold; } //if condition } //Inner for loop } //Outer for loop }

34 Data structure operations on Array
Home Work Write a program to determine the median of the array given below: (9, 4, 5, 1, 7, 78, 22, 15, 96, 45,25) Note that the median of an array is the middle element of a sorted array.


Download ppt "Data Structure and Algorithms"

Similar presentations


Ads by Google