Unit 1
Unit 1
Introduction to Linear Data Structures :- Definition and importance of Linear Data Structures,
Abstract Data Types (ADT’s) and their Implementation, Time and Space Complexity. Searching
Techniques - Linear search & Binary search. Sorting Techniques - Bubble sort , Selection sort,
Insertion sort.
---------------------------------------------------------------------------------------------------------
Data Structures
A data structure is a particular way of storing and organizing data in
a computer so that it can be used efficiently
A data structure is not only used for organizing the data. It is also
used for processing, retrieving, and storing data.
There are different basic and advanced types of data structures that
are used in almost every program or software system that has been
developed.
Data structures are generally categorized into two classes: primitive and non-
primitive data structures .
Primitive data structures are the fundamental data types which are supported by a
programming language. Some basic data types are integer, real, character, and
boolean. The terms ‘data type, basic data type’, and ‘primitive data type’ are
often used interchangeably.
Non-primitive data structures are those data structures which are created using
primitive data structures. Examples of such data structures include linked lists,
stacks, queues, trees, and graphs
Arrays: An array is a collection of similar data elements. These data elements have
the same data type. The elements of the array are stored in consecutive memory
locations and are referenced by an index (also known as the subscript).
Linked List: linked list is a dynamic data structure in which elements (called nodes)
form a sequential list
Queue: A Queue is a linear data structure in which insertion can be done at rear end
and deletion of elements can be dome at front end.
An abstract data type (ADT) is a data structure that ignores the internal
representation but focus on how it performs it job. (or)
Abstract Data type (ADT) is a type (or class) for objects whose behaviour is defined
by a set of value and a set of operations.
Ex: stacks ADT and queues ADT. the user is concerned only with the type of data and
the operations that can be performed on it
Advantages:
•Modification of a program is simple, For example, if you want to add a new field to
a student’s record to keep track of more information about each student, then it
will be better to replace an array with a linked structure to improve the program’s
efficiency.
•In such a scenario, rewriting every procedure that uses the changed structure is
not desirable. Therefore, a better alternative is to separate the use of a data
structure from the details of its implementation.
ALGORITHM
Algorithm is step by step logical procedure for solving a problem.
1. Input: An Algorithm has take ‘0’ or more number of inputs that can be supplied
as externally.
2. Output: An Algorithm must produce at least one output.
3. Definiteness: Each instruction in the algorithm must be clear.
4. Finiteness: An algorithm must terminate after a finite number of steps .
5. Effectiveness: Each operation should be effective. i.e the operations must be
terminate after finite amount of time.
Efficiency of Algorithm
Time Complexity
Space Complexity
Time Complexity
Frequency count is a count denoting the number of times the statement should be
executed.
Comments – 0
Worst Case: the longest running time performed by an algorithm for the given input for
Best Case: the shortest running time performed by an algorithm for the given input for
solving a problem is called best case. It is denotes by omega ( )
Space Complexity
Where C- Constant, it denotes the space taken for input and output.
Sorting
It is a technique used to arrange a given array or list of unordered elements into an
order (i.e Ascending or Descending)
Bubble sort
Selection sort
Insertion sort
Merge sort
Quick sort
Heap sort
Radix sort
Bucket sort
Insertion Sort: Insertion sort is a simple sorting algorithm that works by iteratively
inserting each element of an unsorted list into its correct position in a sorted
portion of the list. It is like sorting playing cards in your hands
You split the cards into two groups: the sorted cards and the unsorted cards. Then,
you pick a card from the unsorted group and put it in the right place in the sorted
group.
We start with second element of the array as first element in the array is
assumed to be sorted.
Compare second element with the first element and check if the second element
is smaller then swap them.
Move to the third element and compare it with the first two elements and put
at its correct position
Example
Best case: O(n),If the list is already sorted, where n is the number of elements in
the list.
In every pass, we process only those elements that have already not moved to
correct position. After k passes, the largest k elements must have been moved
to the last k positions.
In a pass, we consider remaining elements and compare all adjacent and swap
if larger element is before a smaller element. If we keep doing this, we get
the largest (among the remaining elements) at its correct position.
Example
Note: Time Complexity In all cases worst, best and average is O(n2)
First we find the smallest element and swap it with the first element. This
way we get the smallest element at its correct position.
Then we find the smallest among remaining elements (or second smallest) and
swap it with the second element.
We keep doing this until we get all elements moved to correct position.
Example
void selectionSort(int arr[], int n)
{
for (int i = 0; i < n - 1; i++)
{
// Assume the current position holds the minimum element
int min_idx = i;
//Iterate through the unsorted portion to find the actual minimum
for (int j = i + 1; j < n; j++)
{
if (arr[j] < arr[min_idx])
{
Note: The time complexity of Selection sort is: O(n2) ,as there are two nested loops
Searching
Searching is the process of finding a specific element or item within a
collection of data. This collection of data can take various forms, such as
arrays, lists, trees, or other structured representations.
Best Case: In the best case, the key might be present at the first index. So the best
case complexity is O(1)
Worst Case: In the worst case, the key might be present at the last index i.e.,
opposite to the end from which the search has started in the list. So the worst-case
complexity is O(N) where N is the size of the list.
Binary Search
Binary Search is defined as a searching algorithm used in a sorted array by repeatedly
dividing the search interval in half. The idea of binary search is to use the
information that the array is sorted and reduce the time complexity
Time Complexity:
Best Case: O(1) - When the key is found at the middle element.
Worst Case: O(log N) - When the key is not present, and the search space is
continuously halved.