0% found this document useful (0 votes)
24 views54 pages

DS Unit-1

The document discusses linear and non-linear data structures. It defines data structures as a way to organize and store data in a computer so it can be used efficiently. Linear data structures like stacks, queues, and lists allow sequential access to elements, while non-linear structures like trees and graphs do not require sequential access. Common operations on data structures include searching, with linear search examining each element sequentially and binary search using divide-and-conquer to eliminate half the remaining elements at each step.

Uploaded by

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

DS Unit-1

The document discusses linear and non-linear data structures. It defines data structures as a way to organize and store data in a computer so it can be used efficiently. Linear data structures like stacks, queues, and lists allow sequential access to elements, while non-linear structures like trees and graphs do not require sequential access. Common operations on data structures include searching, with linear search examining each element sequentially and binary search using divide-and-conquer to eliminate half the remaining elements at each step.

Uploaded by

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

Introduction to Linear & Non-linear

Data Structure 1 of 3
The data structure can be defined as the collection of elements and all the possible
operations which are required for those set of elements. In other words data structure will
tell us specific set of elements and corresponding set of operations.

A data structure can be defined as a way of organizing and storing data in a computer so
that it can used efficiently.

Mathematically, a data structure D is a triplet, that is, D=(d, f, a) where

D= data structure
d= a set of variables (data objects)
f= a set of functions
a= set of rules to implement the functions.

Dr.L.Lakshmi 1
Introduction to Linear & Non-linear
Data Structure 2 of 3
Data Structures:
A data structure is an arrangement of data in a computer's memory or even disk
storage.

It classified into two types


1. Linear Data Structures
2. Non Linear Data Structures

1. Linear Data Structures:


Linear data structures are those data structures in which data elements are accessed
(read and written) in sequential fashion ( one by one)

Eg: Stacks , Queues, Lists, Arrays

2. Non Linear Data Structures:


Non Linear Data Structures are those in which data elements are not accessed in
sequential fashion.

Eg: trees, graphs


Dr.L.Lakshmi 2
Introduction to Linear & Non-linear
Data Structure 3 of 3
Types of Data Structure

Data Structure

Primitive Data Non Primitive


Structures Data Structures

Linear Data Non Linear Data


Eg: int, char, float
Structures Structures

Eg: array, linked


Eg: trees, graphs
list, stack, queue

Dr.L.Lakshmi 3
Difference between array and linked lists

Dr.L.Lakshmi 4
Difference between linear and non linear data
structures
Linear DS Non Linear DS
data elements are organized a data element can be attached
sequentially and therefore they to several other data elements to
are easy to implement in the represent specific relationships
computer’s memory. that exist among them. Due to
this nonlinear structure, they
might be difficult to be
implemented in computer’s
linear memory compared to
implementing linear data
structures.

every item is related to its every item is attached with many


previous and next time. other items.
Dr.L.Lakshmi 5
Difference between linear and non linear data
structures
Linear DS Non Linear DS
data is arranged in linear data is not arranged in sequence.
sequence.
data items can be traversed in a data cannot be traversed in a
single run. single run.
eg. array, stcks, linked list, queue. eg. tree, graph
implementation is easy implementation is difficult.
It contains data item and a single It contains a data item and two
link pointing to next element links left link pointing to previous
element and right link pointing to
next element

Dr.L.Lakshmi 6
Difference between linear and non linear data
structures
Linear DS Non Linear DS
Syntax Syntax
Struct node Struct node
{ {
int data; int data;
struct node*link; struct node*left;
}; struct node*right;
};

Dr.L.Lakshmi 7
Difference between linear and non linear data
structures
Linear DS Non Linear DS
Syntax Syntax
Struct node Struct node
{ {
int data; int data;
struct node*link; struct node*left;
}; struct node*right;
};

Dr.L.Lakshmi 8
Searching
• Another common operation in computer science is searching, which is the process used to find
the location of a target among a list of objects.

• In the case of an array searching means that given a value , we want to find the location of the
first element in the array that contains that value .

• There are two basic searches for arrays:


1. The sequential search
2.The binary search

• The sequential search can be used to locate an item in any array.

• The binary search , on other hand, requires the list to be sorted.


Linear/ Sequential search (1 of 9)

• Sequential search is used whenever the list is not ordered.

• Generally we use the technique only for small lists or lists that are not

searched often.

• In the sequential search , we start searching for the target from the

beginning of the list, and we continue until the we find the target or

until we are sure that it is not in the list.

• This gives us two possibilities:

either we find it or

we reach the end of the list


Linear search Example (2 of 9)

Locating data in unordered list


Linear search Example (3 of 9)

Locating data in unordered list


Linear search Example (4 of 9)

Unsuccessful search in unordered list


Linear search Example (5 of 9)

Unsuccessful search in unordered list


Linear search Example (6 of 9)

Unsuccessful search in unordered list


Linear search Algorithm (7 of 9)
1.start
2.read n
3.read elements of list (a[])
4.read element to be searched(item)
5.initialize found=0
6. for(i=0;i<n;i++)
7. {
If(a[i]==item)
{
found=1
Break
}
}
8.if (found==1)
then print element a[i] is found i th location
else
print element not found
9.stop
Linear search Flowchart (8 of 9)
Linear search (9 of 9)
Advantages :

1.Easy to understand
2.The list need not be in sorted order
3.If item is at beginning not many comparisons are needed

Disadvantages:

1.Slow if list is large


2.when item at end of list which takes more time to execute
3.which needs more time and space complexity.

Complexity:
Best case: O(1)
Average case: O(n)
Worst case: O(n)

C program u7_ex6.c
Binary search (1 of 5)
• Sequential search algorithm is very slow if list contains more number of
elements.
• If the array is not sorted ,linear search is the only solution.
• If the list is sorted , we can use a more efficient algorithm called
the binary search.
• We should use a binary search whenever the list starts to become large.
• The binary search starts by testing the data in the element at the middle of the list.
• This determines if the target is in first half or second half of the list.
• If it is in first half , we do not need to check the second half.
• If it is in second half , we do not need to check the first half.
• In other words ,either way we eliminate half the list from further
consideration.
• We repeat this process until we find the target or satisfy ourselves
that it is not in the list.
• To find the middle of the list we three variables,
one to identify the beginning of the list(first)
one to identify the beginning of the list(mid)
one to identify the beginning of the list(last)
mid=( first + last )/2
Binary search Example (2 of 5)

Binary search example if target is present in the list


Binary search Example (3 of 5)

Binary search example if target is not present in the list


Binary search Example (4 of 5)

Binary search example if target is not present in the list


Binary search Algorithm (5 of 5)
Iterative Algorithm
int binarysearch (int a[], int target, int n)
// pre: list is sorted in ascending order
//post: ITERATIVE binary search will return the index of the target element, else -1
{
int mid;
int first = 0;
int last = n-1
while ( first <= last )
{
mid = (first + last) / 2;
if ( a[mid] == target )
return mid;
if ( a[mid] > target )
last = mid - 1; Complexity of quick sort:
else Best case: O(logn)
first = mid + 1; Average case: O(logn)
}
Worst case: O(log n)
return -1;
}

C program u7_ex7.c
Linear Vs Binary Search
Analysis Linear Search Binary Search

Best Case O(1) O(1)

Average Case O(n) O(log n)

Worst Case O(n) O(log n)

Unsuccessful Search O(n) O(log n)

List Unordered List Ordered List

Usage Small list Large List

Time & Space More Less

Search Starts at beginning Starts at middle


Sorting
• Sorting is the process through which data are arranged according to their values.

• Three sorting algorithms:

• Selection Sort
• Bubble Sort
• Insertion Sort
Selection Sort (1 of 4)

• The list is divide into two sub lists as sorted and unsorted.

• The sub lists are divided by an imaginary wall.

• Find the smallest element from the unsorted sub list and swap it with the element
at the beginning of unsorted sub list.

• After each selection and swapping, the wall between the two sub lists moves one
element ahead, increasing the number of sorted elements and decreasing the
number of unsorted sub lists.

• Move one element from the unsorted sub list to the sorted sub list completes a
sort pass.

• Thus a list of n elements need n-1 passes to completely rearrange the data.
Selection Sort (2 of 4)

Example
Selection Sort (3 of 4)

Flowchart
Selection Sort (4 of 4)

Code:

selection_sort ( int A[ ] , int n )


{
int i , j , small , temp ;
for ( i = 0 ; i < n ; i++ )
{
small = i ;
for ( j = i + 1 ; j <= n ; j ++ )
{
if ( A [ j ] < A [ small ] )
small = j ;
}
temp = A [ i ] ;
A [ i ] = A [ small ] ;
Complexity of Selection Sort
A [ small ] = temp ;
Best Case : O ( n2 )
} Average Case : O ( n2 )
} Worst Case : O ( n2 )
C program u7_ex1.c
Bubble Sort (1 of 4)

• The list is divide into two sub lists as sorted and unsorted.
• The smaller element is bubbled from the unsorted sub list and moved to
the sorted sub list.
• After moving the smallest element to the sorted list, the wall moves one
element ahead, increasing the number of sorted elements and decreasing
the number of unsorted ones.
• Move one element from the unsorted sub list to the sorted sub list
completes one sort pass.
• Thus a list of n elements, the bubble sort requires up to n-1 passes to sort
the data.
Bubble Sort (2 of 4)

Example:
Bubble Sort (3 of 4)
Flowchart:
Bubble Sort (4 of 4)
Complexity of Bubble_Sort
The complexity of sorting algorithm is
Code:
depends upon the number of comparisons that
are made.
bubblesort ( int A[ ] , int n )
Total comparisons in Bubble sort is
{ n ( n – 1) / 2 ≈ n 2 – n
int i , j, temp ; Complexity = O ( n 2 )
for ( i = 0 ; i < n ; i++ )
{
for ( j = 0; j <n-i-1 ; j ++ )
{
if ( A [ j ] < A [ j+1 ] )
{
temp = A [ j ] ;
A [ j ] = A [ j +1 ] ;
A [j +1] = temp ;
}
}
}
Insertion Sort (1 of 4)

• Insertion sort is one of the most common sorting techniques used by card players.

• The list is divide into two sub lists as sorted and unsorted.

• In each pass, the first element of the unsorted sub list is picked up and

transformed into the sorted sub list by inserting at the appropriate place.

• A list of n elements, the insertion sort requires at most n-1 passes to sort the data.
Insertion Sort (2 of 4)

Example:
Insertion Sort (3 of 4)

Flowchart
Insertion Sort (4 of 4)

Code: Complexity of Insertion Sort


insertion_sort ( int A[ ] , int n ) Best Case : O ( n )
{ Average Case : O ( n2 )
int i , j, temp ; Worst Case : O ( n2 )
for ( i = 1 ; i <= n ; i++ )
{
flag = false;
temp = A[i];
for ( j = i-1; j > 0 && !flag )

if ( A temp < A [ j ] )
{
A[j+1] = A[j];
j - -;
}//if
else
flag = true;
A[j+1] = temp;
}
}
C program u7_ex3.c
Divide and Conquer sorting methods (1 of 4)

• We have described 3 sorting techniques –


selection ,bubble , insertion sorts,
which are useful for sorting very small lists.

• To sort large lists we use quick sort and merge sort.

• Divide and conquer is an important algorithm design paradigm in solving all kinds
computational problems.

• Quick sort ,merge sort and binary search are based on divide and conquer strategy.

• Divide and conquer is essentially a special case of recursion in which a given


problem is divided into two or more sub problems of exactly the same type and
solution to the problem is expressed in terms of solutions to the sub problems.

Dr.L.Lakhmi
38
Divide and Conquer sorting methods (2 of 4)

• Example: Cutting a circle out of a sheet of paper is a tedious task which takes a lot of

time .

• But there is a way by which we can reduce the time and effort required and cut perfect

circles every time ----divide and conquer.

• Divide and conquer strategy helps in simplification of complex problems

(a) (b) (c) (d)


Dr.L.Lakhmi
39
Divide and Conquer sorting methods (3 of 4)
• Divide and conquer algorithms are quite efficient as the average number of steps in this methods
is less as compared to other procedures.

• Divide and conquer algorithms are more efficient with regard to use of memory caches
Internal and external sorts:
Sorting algorithms are generally classified into 2 types:
1.Internal sorting
2.External sorting
• Internal sort uses only primary memory during entire sorting process.

• All data items are held in the main memory and no secondary memory is required.

• Selection, bubble and insertion sorts are internal sorting techniques.

• Internal sorting procedures can only process relatively small lists due to memory constraints

Dr.L.Lakhmi 40
Divide and Conquer sorting methods (4 of 4)
Sorts

Internal Sorts
· Natural
· Balanced
Insertion Selection Exchange
· Polyphase
· Insertion • selection · Bubble

· shell •heap · Quick

•Sorting large chunks of data requires the use of external or secondary memory.

•An external sort uses a secondary memory such as hard disk, to store data that does not fit into the
main memory

•All external sorts are based on process of merging, different portions of data are sorted separately and
then merged together.

•Depending on the complexity of the list to be sorted, different types of merging procedures are used
in external sorting.
Dr.L.Lakhmi 41
Quick sort (1 of 8)
• Quick sort algorithm was developed by C.A.R Hoare in 1962.
• Like bubble sort quick sort is an exchange sort ,i.e. , items swap positions till the entire array is
sorted.
• The quick sort is more efficient because it requires fewer exchanges to correctly position an
element.
• The basic principle underlying the quick sort algorithm is to pick one element in the array and
rearrange the remaining elements around it.
• The chosen element is called the pivot(usually first element of list).
• Once the pivot is chosen , all the elements lesser than the pivot are moved to the left of the pivot,
and all elements equal to or greater than the pivot are moved to the right of the pivot.
• The pivot acts as a partition dividing the original lists into two sub lists, and after the partitioning,
the pivot is in its correct position in the sorted list.
• This procedure of choosing the pivot and partitioning the list is recursively applied to the sub lists
till the subsequent sub lists consists of only one element and entire list is sorted.

Dr.L.Lakhmi
42
Quick sort (2 of 8)

Dr.L.Lakhmi 43
Quick sort Algorithm (3 of 8)
1.Start

2.Read the no of elements to sort(n).

3.Read elements to be sorted in array a


a[0]………a[n-1]

4.Call quick sort function


quicksort(a,0,n-1)

5.Print the sorted array a[0]…..a[n-1]

6.Stop

Dr.L.Lakhmi 44
Quick sort (4 of 8)
Algorithm quicksort(int a[10],int first,int last)
{
i=first; j=last; pivot=first;
if(first<last)
{
while(i<j)
{
while(x[i]<=x[pivot]&&i<last)
Complexity of quick sort: i++;
while(x[j]>x[pivot])
Best case: O(n logn) j--;
if(i<j)
{
Average case:O(n logn) t=x[i]; x[i]=x[j]; x[j]=t;
}
2
Worst case: O(n ) }
t=x[pivot]; x[pivot]=x[j]; x[j]=t;
}
qsort(x,first,j-1);
qsort(x,j+1,last);
Dr.L.Lakhmi 45
}
Quick sort example (5 of 8)

Dr.L.Lakhmi 46
Quick sort (6 of 8)

Dr.L.Lakhmi 47
Quick sort (7 of 8)

Dr.L.Lakhmi 48
Quick sort (8 of 8)

Dr.L.Lakhmi 49
C program u7_ex4.c
Merge sort (1 of 6)
• Merge sort is a divide and conquer external sorting algorithm.

• It was invented by John von Neumann in 1942.

• The basic procedure of merge sort is to divide the list to be sorted into two smaller
sub lists (roughly equal in size) and recursively repeat this procedure till only one
element is left in the sub list.

• After this the various sorted sub lists are merged back to from the parent list

• The merging process also goes on recursively till the sorted original list is arrived
at.

Complexity of quick sort:


Best case: O(n logn)
Average case: O(n logn)
Worst case: O(n log n)
Dr.L.Lakhmi 50
Merge sort Example (2 of 6)
Merge sort Algorithm (4 of 6)

1.Start

2.Read the no of elements to sort(n).

3.Read elements to be sorted in array a


a[0]………a[n-1]

4.Call merge sort function


mergesort(a,0,n-1)

5.Print the sorted array a[0]…..a[n-1]

6.Stop

Dr.L.Lakhmi 52
Merge sort Algorithm (5 of 6)

mergesort (a, low, high)


if left >= right return
else
mid ← b(low+high)/2
mergesort(A, low, mid)
mergesort(A, mid+1, high)
merge(a, low, mid, high)

Dr.L.Lakhmi 53
Merge sort Algorithm (6 of 6)
merge(A, low, mid, high)
1. n1 ← mid – low + 1
2. n2 ← high – mid
3. create array L[n1], R[n2]
4. for i ← 0 to n1-1 do L[i] ← A[low +i]
5. for j ← 0 to n2-1 do R[j] ← A[mid+j]
6. k←i←j←0
7. while i < n1 & j < n2
8. if L[i] < R[j]
9. A[k++] ← L[i++]
10. else
11. A[k++] ← R[j++]
12. while i < n1
13. A[k++] ← L[i++]
14. while j < n2
15. A[k++] ← R[j++]

Dr.L.Lakhmi
C program u7_ex5.c 54

You might also like