Introduction to Linear Data Structures
Introduction to Linear Data Structures
Linear Data Structures: A data structure is called linear if all of its elements are
arranged in thelinear order. In linear data structures, the elements are stored in
non-hierarchical way where eachelement has the successors and predecessors
except the first and last element or maintains a linear relationship among its
elements is calleda linear data structure
If a datastructure organizes the datain sequential order, then thatdata
structure is calledLinear Data Structure.
Example:
1. Arrays
2. List (Linked List)
3. Stack
4. Queue
Linked List: Linkedlist is a linear data structure which is used to maintain a list in
the [Link] can be seen as the collection of nodes stored at non-contiguous
memory locations. Each nodeof thelistcontains a pointer to its adjacent node.
Arrays:
Array, in general, refers to an orderly arrangement of data elements. Array is a
type of data structure that stores data elements in adjacent locations. Array is
considered as lineardata structure that stores elements of same data types.
Hence,it is also called as a linear homogenous data structure.
When we declare an array, we can assign initial values to each of its
elements byenclosing the values in braces {}.
int Num [5] = { 26, 7,67,50,66 };
This declaration will createan arrayas shown below:
0 1 2 3 4
1. Linked List:
A linked list is a data structure in which each data element contains a pointeror
link to the next element in the list. Through linked list, insertion anddeletion
of the data element is possible at all places of a linear list. Also in linked list, it is
not necessary to have the data elements stored in consecutive locations. It
allocates space for each data item in its own block of memory. Thus, a linked list is
considered as a chain of data elements or records called nodes. Each node in the
list contains information field and a pointer field. The information field contains the
actual data and the pointer field contains addressof the subsequent nodes in the
list.
Figure represents a linkedlist with 4 nodes. Each node has two parts. The left
part in the node represents the information part which contains an entire
record of data items andthe right part represents the pointer to the next
node. The pointer of the last nodecontainsa null pointer.
: Easier to insert or deletedata elements
: Slow search operation and requires more memoryspace
Applications:
Implementingstacks,queues, binarytrees and graphs ofpredefinedsize.
Implement dynamic memorymanagement functions of operatingsystem.
Polynomial implementation for mathematical operations
Circularlinked listis used to implement OSor application functions that require
roundrobin execution of tasks.
Circular linkedlist is usedin a slideshow where a user wants to go back to the
first slideafter last slide is displayed.
Doublylinked list is used in the implementation of forwardand backward
buttonsin a browser to move backwards and forward in the opened pages of a
website.
Circular queue is usedto maintain the playing sequence of multiple players in a
game.
2. Stacks
A stack is a linear data structure in which insertion and deletion of elements are done
at only one end, which is known as the top of the stack. Stack is called a last-in,
first-out (LIFO) structure because the last element which is added to the stack is the
first element which is deleted from the stack.
In the computer’ s memory, stacks can be implemented using arrays or linked lists.
Figure is a schematic diagram of a stack. Here, element FF is the top of the stack and
element AA is the bottom of the stack. Elements are added to the stack from the top.
Since it follows LIFO pattern, EE cannot be deleted before FF is deleted, and similarly
DDcannotbe deleted beforeEE is deleted and so on.
Applications:
Temporarystorage structure for recursiveoperations
Auxiliarystorage structure for nested operations, function calls,
deferred/postponedfunctions
Managefunction calls
Evaluation of arithmetic expressions in various programminglanguages
Conversion of infix expressions into postfix expressions
Checkingsyntax of expressions in a programmingenvironment
Matchingof parenthesis
String reversal
In all theproblems solutions based on backtracking.
Usedin depth first search in graph andtree traversal.
OperatingSystem functions
UNDO and REDO functions in an editor.
[Link]:
A queue is a first-in, first-out (FIFO) data structure in which the element that is
inserted first is the first one to be taken out. The elements in a queue are added
at one end called the rear and removed from the other end called the front.
Like stacks,queues can be implemented byusingeitherarrays orlinked lists.
Figure shows a queue with 4 elements, where 55 is the front element and 65 is
the rear element. Elements can be added from the rear and deleted from the
front.
Applications:
It is usedin breadth search operation in graphs.
Jobscheduler operations of OS like a print buffer queue, keyboard buffer
queuetostorethe keys pressed byusers
Jobscheduling, CPU scheduling, Disk Scheduling
Priority queues areusedin file downloadingoperations in a browser
Data transfer between peripheral devices and CPU.
Interrupts generatedbytheuser applications for CPU
Calls handled bythe customers in BPO
Applications:
Implementingthe hierarchical structures in computer systems like
directory andfile system.
Implementingthe navigation structure of a website.
Code generation likeHuffman’ s code.
Decision making in gaming applications.
Implementation of priorityqueues for priority-based OS scheduling
functions
Parsingof expressions and statements in programminglanguage
compilers
For storingdata keys for DBMS forindexing
Spanning trees for routingdecisions in computer andcommunications
networks
Hash trees
Path-finding algorithm to im plement in AI, robotics andvideo games
applications
Graphs:
A graph is also a non-linear data structure. In a tree data structure, all data
elements are stored in definite hierarchical structure. In other words, each node
has only one parent node. While in graphs, each data element is called a vertex
and is connected to many other vertexes through connections callededges.
Thus, a graph is considered as a mathematical structure, which is composed ofa
set of vertexes and a set of edges. Figure shows a graph with six nodes A, B, C,
D,E,F and seven edges [A, B], [A,C], [A, D], [B, C],[C, F], [D, F] and [D, E].
Ex2. transpose
Voidtranspose()
{
For(int i=0; i<rows; i++)
For(int j=i+1; j<rows; j++)
Swap(a[i][j],a[j][i]);
}
𝑟𝑜𝑤𝑠 − 1 𝑟𝑜𝑤𝑠
(𝑟𝑜𝑤𝑠 − 𝑖) = 𝑞 = 𝑟𝑜𝑤𝑠(𝑟
𝑟𝑜𝑤𝑠 + 1)/2
𝑖=0 𝑞=1
Searching techniques:
It is a process of finding the location of a given elementin a set of elements. Or
It is designed to check for an element or retrieve an element from any data
structure where it is stored.
Or Searching is the process of fetching a specific element in a collection of
elements
The search is saidto be successful if thegiven element is found,i., the element
does exist in the collection (such as an array); otherwise,it is unsuccessful.
Two prominent search strategies are extensively used to find a specific item on a
list are as follows:
1. Linear or Sequential search
[Link] Interval search: designed for searching in sorted
data-structures, target the center of the search structure and divide
the search space in half.
Linear search: It is the technique for searching a given element/ item in the list
of elements by comparing each and every element in the list sequentially.
Algorithm:
Linear Search ( ) //n is sizeof thearray,Arr is thenameof thearray, and
key is the searchedelement.
Step 1: start
Step 2: read n, Arr,key
Step 3: Set i to 0 // i is theindex of an array which starts from 0
Step 4: if i > n then go to step 9 // n is thenumber of elements in array
Step 5: if Arr[i] == key then go to step 8
Step 6: Set i = i + 1
Step 7: Goto step2
Step 8: Print elementa foundat index i and go to step 10
Step 9: Print element not found
Step 10: Exit
Program:
#include<stdio.h>
voidmain()
{
int n,array[n],key, c;
printf("Enter number of elements in array\n");
scanf("%d",&n);
printf("Enter %d integer(s)\n",n);
for (c = 0; c < n; c++)
scanf("%d",&array[c]);
printf("Enter a number to search\n");
scanf("%d",&key);
for (c = 0; c < n; c++)
{
if (array[c] == key) /* If required element is found */
{
printf("%d is present at location %d.\n",key, c+1);
break;
}
}
if (c == n)
printf("%disn't present in the array.\n", key);
}
The Complexity of linear search : You havethree differentcomplexities faced
while performingLinear Search Algorithm,they are mentionedas follows.
1. Best Case
[Link] Case
[Link] Case
Best case complexity
The element being searched couldbe found in the first position.
In this case, the search ends with a single successful comparison.
Thus,in the best-case scenario,thelinear search algorithm performs O(1)
operations.
Worst case complexity
The element being searched may be at thelastposition in the arrayor not
at all.
In the first case, the search succeeds in ‘n’ comparisons.
In the next case, thesearch fails after ‘n’ comparisons.
Thus,in the worst-casescenario, the linear search algorithmperforms
O(n) operations.
Average casecomplexity:
When the element to be searchedis in the middleof the array,the
average case of the Linear Search Algorithm is O(n).
Next,you will learn about the SpaceComplexity of Linear Search
Algorithm.
Space complexity: The linear search algorithm takes upno extra space;
its space is O(n) for an array of n elements.
Applications: The linearsearch algorithm has the followingapplications:
Linear search can be appliedto both single-dimensional and
multi-dimensional arrays.
Linear search is easyto implement and effective when the array contains
only a few elements.
Linear Search is also efficient when thesearch is performed to fetch a
single search in an unordered-List.
Binarysearch
Binary searches are efficient algorithms based on the concept of
“divide and conquer” , The data structure must be sorted. It improves
the search by recursively dividing the array in half until you either find the
element or the list gets narrowed down to one piece that doesn’ t
match theneeded element.
Let us consideran array arr= {1,5,7, 8, 13, 19, 20, 23,29}. Find the
location of theitem23 in the array.
In 1st step :
1. BEG = 0
[Link] =8
[Link]= 4
4.a[mid] =a[4] = 13 < 23, therefore
In 2nd step:
5. Beg= mid +1 = 5
[Link]= 8
7. mid = 13/2 = 6
8. a[mid] = a[6] = 20 < 23,therefore;
In 3rd step:
[Link]= mid + 1 = 7
[Link]= 8
10. mid = 15/2 = 7
11. a[mid] = a[7]
12. a[7] = 23 = item;
13. therefore,set location = mid;
14. The location of the item will be 7.
Algorithm: Binary search()
step 1: start
step 2: read sorted array, n , key,
step 3: assign first=0,last= n-1
step 4: repeat until first <= last
step 5: compute mid = (first+last)/2 (the mid position in an array)
step 6: if key == array[mid] else goto step6
step 7: write key found go to step 14.
Step 8: If the key < middle element else goto step 10
Step 9: assign last= mid-1
Step 10: go to step4 in left half.
Step 11: assign first=mid+1
Step 12: go to step 4 in right half
Step 13: write key not found
Step 14: stop
Binary searches work under the principle of using the sorted information in the
array to reduce thetime complexityto zero (Log n). Here are the binary search
approach’ s basic steps:
Begin with an interval that covers theentire array
If the search key value is less than the middle-interval item, narrow the
interval to that lower half. Otherwise, narrow the interval to the upper
half.
Keep checking the chosen interval until eit her the value is found or the
interval’ s empty
Time Complexity:
Best Case: O(1)
Average Case: O(log N)
Worst Case: O(log N)
Auxiliary Space: O(1), If the recursive call stack is considered then the auxiliary
space will be O(logN).
Advantages of Binary Search:
Binary search is faster than linear search, especially for large arrays.
More efficient than other searching algorithms with a similar time
complexity, such as interpolation search or exponential search.
Binary search is well-suited for searching large datasets that are stored
in external memory, such as on a hard drive or in the cloud.
Drawbacks of Binary Search:
The array should be sorted.
Binary search requires that the data structure being searched be stored
in contiguous memory locations.
Binary search requires that the elements of the array be comparable,
meaning that they must be able to be ordered.
Applications of Binary search:
Binary search can be used as a building block for more complex algorithms
used in machine learning, such as algorithms for training neural networks or
finding the optimal hyperparameters for a model.
It can be used for searching in computer graphics such as algorithms for
ray tracing or texture mapping.
It can be used for searching a database.
Program:
#include<stdio.h>
voidmain()
{
int i, low, high,mid, n, key,array[100];
printf("Enternumber of elements");
scanf("%d",&n);
printf("Enter%dintegers", n);
for(i = 0; i < n; i++)
scanf("%d",&array[i]);
printf("Entervalue to find");
scanf("%d",&key);
low = 0;
high = n - 1;
mid = (low+high)/2;
while (low<= high) {
if(array[mid]<key)
low = mid+ 1;
elseif (array[mid] == key) {
printf("%dfound atlocation %d", key,mid+1);
break;
}
else
high = mid - 1;
mid = (low + high)/2;
}
if(low>high)
printf("Not found! %disn't present in the list",key);
}
Sequential Search Binary Search
Finds thekey present at firstposition Finds the keypresent at centre
in constant time position in constant time
Sequenceof elements in the container The elements mustbe sorted in the
doesnotaffect. container
Arrays andlinked lists can be usedto It cannotbe implemented directly into
implementthis thelinked list. We need to changethe
basic rules ofthe list to implement
this
Algorithm is iterativein nature Algorithm technique is Divideand
Conquer.
Algorithm is easyto implement, and Algorithm is slightly complex. It takes more
requires less amount of code. amount of codeto implement.
N number of comparisons are Log n number of comparisons are
requiredforworst case. sufficientin worst case.
Time complexityis O(n) Timecomplexityis O(logn)
Sorting:
Sorting is a process in which items arearrangedsystematically
(or)
Sorting is the process of arranging data into meaningful order so that
you can analyze it more effectively
Types of sorting: bubble sort, selection sort, insertion sort, merge sort, Quick sort
etc.
Bubble sort: Bubble Sort is the simplest sorting algorithm that works by
repeatedly swapping the adjacent elements if they are in the wrong order.
It is also known as sinking sort.
traverse from left and compare adjacent elements and the higher
one is placed at right side.
In this way, the largest element is moved to the rightmost end at
first.
This process is then continued to find the second largest and place
it and so on until the data is sorted.
Algorithm:
Step 1: Start
Step 2: Read array,n,temp=0
Step 3: Repeat steps 4 until i<n else go to step 9
Step 4: Repeat steps 5 to 8 until j<n-i-1 else go to step 3
Step 5; If array[j]>array[j+1] else go to step 4
Step 6: Temp=array[j]
Step 7: Array[j]=array[j+1]
Step 8: Array[j+1]=temp
Step 9: write array is sorted.
Step 10: stop
Total no. of passes: n-1, n-2, … .2,1, This is an arithmetic series, and the
equation for the total number of times is (n - 1)*n / 2.
Total no. of comparisons: n*(n-1)/2
Time Complexity:
Worst Case Complexity: If the array elements are in descending order and
we want to make it in ascending order, it is the worst case. The time
complexityforthe worst case is O(n²).
Best Case Complexity: The best case is when all the elements are already
sorted, and no swapping is required. The time complexity in this scenario
is O(n).
Average Case Complexity: This is the case when the elements are
jumbled. The timecomplexity for theaverage case in bubble sort is O(n²).
Space Complexity
Space complexit yfor the standard bubble sort algorithm is O(1) as thereis
one additional variable required to hold the swapped elements
temporarily.
Space complexity for optim ized implementation of the bubble sort
algorithm is O(2). This is because two additional variables are required.
Selection sort
The algorithm repeatedly selects the smallest (or largest) element from the
unsorted portion of the list and swaps it with the first element of the unsorted
part. This process is repeated for the remaining unsorted portion until the entire
list is sorted.
In selection sort, the smallest value among the unsorted elements of the array is
selected in every pass and inserted to its appropriate position into the array. It is
also the simplest algorithm. It is an in-place comparison sorting algorithm. In this
algorithm, the array is divided into two parts, first is sorted part, and another one
is the unsorted part. Initially, the sorted part of the array is empty, and unsorted
part is the given array. Sorted part is placed at the left, while the unsorted part is
placed at theright.
Ex.
In selection sort, the first smallest element is selected from the unsorted array
and placed at the first position. After that second smallest element is selected
and placed in the second position. The process continues until the array is
entirely sorted.
Algorithm:
SELECTIONSORT(arr, n)
Step 1: start
Step 2: read array, n, pos, temp
Step 3: Repeat Steps 4 to 9 until i < n
Step 4: assign pos=i, j=i+1
Step 5: repeat steps 6 to 7 until j<n else goto step 8
Step 6: if array[pos]>array[j] else go to step8
Step 7: pos=j
Step 8: if pos!= i else go to step3
Step 9: SWAP arr[i] with arr[pos] use temp variable, go to step3
Step 10: write sorted array values
Step 11: stop
Program:
#include<stdio.h>
void main() {
int n, arr[n];
int i, j, position,swap;
printf("Enter thesizeof thearray: ");
scanf("%d",&n);
printf("Enter %d elements: ", n);
for(i= 0; i < n; i++)
{
scanf("%d",&arr[i]);
}
for (i= 0; i < (n - 1); i++){
position = i;
for (j = i + 1; j < n; j++) {
if (arr[position] > arr[j])
position = j;
}
if (position != i) {
swap = arr[i];
arr[i] = arr[position];
arr[position] = swap;
}
}
for (i= 0; i < n; i++)
printf("%d\t",arr[i]);
}
Complexity Analysis of Selection sort
Time Complexity: The time complexity of Selection Sort is O(N2) as there are
two nested loops:
One loop to select an element of Array one by one = O(N)
Another loop to compare that element with every other Array element =
O(N)
Therefore overall complexity = O(N) * O(N) = O(N*N) = O(N2)
Auxiliary Space: O(1) as the only extra memory used is for temporary variables
while swapping two values in Array. The selection sort never makes more than
O(N) swaps and can be useful when memory writing is costly.
Advantages:
Simple and easy to understand.
Works well with small datasets.
Disadvantages:
Selection sort has a time complexity of O(n^2) in the worst and average
case.
Does not work well on large datasets.
Does not preserve the relative order of items with equal keys which
means it is not stable.
Time Complexity
Case Time Complexity
Best Case O(n)
Average Case O(n2)
Worst Case O(n2)
o Best Case Complexity - It occurs when there is no sorting required, i.e.
the array is already sorted. The best-case time complexity of insertion
sort is O(n).
o Average Case Complexity -It occurs when the array elements are in
jumbled order that is not properly ascending and not properly
descending. The average case time complexity of insertion sort is O(n2).
o Worst Case Complexity - It occurs when thearrayelements are required
to be sorted in reverse order. That means suppose you have to sort the
array elements in ascending order, but its elements are in descending
order. The worst-casetime complexity of insertion sort is O(n2).
2. Space Complexity
Space Complexity O(1)
Stable YES
o The space complexity of insertion sort is O(1). It is because, in insertion
sort, an extra variable is required for swapping.
Note check space and time complexities of linear, binary, bubble,
insertion,selection.
Insertion Sort Algorithm
Now we havea bigger picture of how this sorting techniqueworks,so we
can derive simple steps by which we can achieve insertion sort.
Step 1 − If it is thefirst element, it is alreadysorted. return 1;
Step 2 − Pick next element
Step 3− Compare with all elements in thesorted sub-list
Step 4 − Shift all the elements in the sorted sub-list that is greater than
thevalue to be sorted
Step 5 − Insert thevalue
Step 6 − Repeat until list is sorted
Pseudocode
Algorithm: Insertion-Sort(A)
for j = 2 to [Link]
key = A[j]
i=j– 1
whilei > 0 and A[i]>key
A[i + 1] = A[i]
i = i -1
A[i + 1] = key
Analysis
Run timeof this algorithm is verymuch dependent on the given input.
If the given numbers are sorted, this algorithm runs in O(n) time. If the
given numbers are in reverse order, the algorithm runs in O(n2) time.
Example
We take an unsortedarrayforour example.
It finds that both 14 and 33 are already in ascending order. For now, 14 is
in sorted sub-list.
And finds that 33 is not in the correct position. It swaps 33 with 27. It
also checks with all the elements of sorted sub-list. Here we see that the
sorted sub-list has only one element 14, and 27 is greater than 14.
Hence,thesortedsub-list remains sorted after swapping.
By now we have 14 and 27 in the sorted sub-list. Next, it compares 33
with 10. Thesevalues are not in a sortedorder.
This process goes on until all the unsorted values arecovered in a sorted
sub-list. Now weshall seesome programming aspects of insertion sort.