Data Structures and Sorting Algorithms: An Introduction, Schemes and Mind Maps of Computer Engineering and Programming

An introduction to data structures and sorting algorithms, covering linear and non-linear data structures, linear search, binary search, interpolation search, bubble sort, selection sort, insertion sort, quick sort, merge sort, and shell sort. It includes algorithms and examples for each topic, making it a useful resource for understanding fundamental concepts in computer science. Suitable for students and professionals alike, offering a clear and concise overview of these essential topics. It also touches on the importance of sorting for optimizing data searching and representation.

Typology: Schemes and Mind Maps

2024/2025

Available from 12/26/2025

shubhangi-birajdar-1
shubhangi-birajdar-1 🇮🇳

1 document

1 / 16

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
UNIT 1 : Introduction to Data Structures & Searching-Sorting
What is Data Structure
A data structure is a storage that is used to store and organize data. It is a way of arranging data on a computer so that it can be
accessed and updated efficiently.
Classification of Data Structure:
Data structure has many different uses in our daily life. There are many different data structures that are used to solve
different mathematical and logical problems. By using data structure, one can organize and process a very large amount of data
in a relatively short period. Let’s look at different data structures that are used in different situations.
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.
Sorting and Searching
Sorting and Searching is one of the most vital topic in DSA. Storing and retrieving information is one of the most common
application of computers now-a-days. According to time the amount of data and information stored and accessed via
computer has turned to huge databases. So many techniques and algorithms have been developed to efficiently maintain and
p
r
o
ce
ss
i
n
f
o
r
m
a
t
i
on
i
n
d
a
t
a
b
a
s
e
s
.
T
h
e
p
r
o
ce
ss
e
s o
f
l
oo
k
i
n
g
u
p
a
p
a
r
t
i
c
u
l
a
r
d
a
t
a
r
e
c
o
r
d
i
n
t
h
e
d
a
t
a
b
a
s
e
i
s
ca
ll
ed
s
ear
c
h
i
n
g
.
T
h
e
p
r
o
ce
ss o
f
o
r
de
r
i
n
g
t
h
e
r
eco
r
d
s
i
n
a
d
a
t
a
b
a
s
e
i
s
ca
ll
ed
S
o
r
t
i
n
g
.
S
o
r
t
i
n
g
a
n
d
s
e
a
r
ch
i
n
g
t
o
ge
t
h
e
r
co
ns
t
i
t
u
t
e
a
m
a
j
o
r
a
r
e
a
o
f
study in computational methods. Both of them are very important field of study in data structure and algorithms. Let us discuss
about both the topics in detail here.
What is searching?
Searching is the process of finding a particular item in a collection of items. A search typically answers whether the item is
present in the collection or not. Searching requires a key field such as name, ID, code which is related to the target item. When
the key field of a target item is found, a pointer to the target item is returned. The pointer may be an address, an index into a
vector or array, or some other indication of where to find the target. If a matching key field isn’t found, the user is informed.
The most common searching algorithms are:
Linear search
Binary search
Interpolation search
Hash table
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download Data Structures and Sorting Algorithms: An Introduction and more Schemes and Mind Maps Computer Engineering and Programming in PDF only on Docsity!

UNIT 1 : Introduction to Data Structures & Searching-Sorting

What is Data Structure A data structure is a storage that is used to store and organize data. It is a way of arranging data on a computer so that it can be accessed and updated efficiently. Classification of Data Structure: Data structure has many different uses in our daily life. There are many different data structures that are used to solve different mathematical and logical problems. By using data structure, one can organize and process a very large amount of data in a relatively short period. Let’s look at different data structures that are used in different situations. 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.

Sorting and Searching

Sorting and Searching is one of the most vital topic in DSA. Storing and retrieving information is one of the most common application of computers now-a-days. According to time the amount of data and information stored and accessed via computer has turned to huge databases. So many techniques and algorithms have been developed to efficiently maintain and process information in databases. The processes of looking up a particular data record in the database is called searching. The process of ordering the records in a database is called Sorting. Sorting and searching together constitute a major area of study in computational methods. Both of them are very important field of study in data structure and algorithms. Let us discuss about both the topics in detail here.

What is searching?

Searching is the process of finding a particular item in a collection of items. A search typically answers whether the item is present in the collection or not. Searching requires a key field such as name, ID, code which is related to the target item. When the key field of a target item is found, a pointer to the target item is returned. The pointer may be an address, an index into a vector or array, or some other indication of where to find the target. If a matching key field isn’t found, the user is informed. The most common searching algorithms are: Linear search Binary search Interpolation search Hash table

Linear Search Algorithm

Linear Search Algorithm is the simplest search algorithm. In this search algorithm a sequential search is made over all the items one by one to search for the targeted item. Each item is checked in sequence until the match is found. If the match is found, particular item is returned otherwise the search continues till the end. Algorithm LinearSearch ( Array A, Value x) Step 1: Set i to 1 Step 2: if i > n then go to step 7 Step 3: if A[i] = x then go to step 6 Step 4: Set i to i + 1 Step 5: Go to Step 2 Step 6: Print Element x Found at index i and go to step 8 Step 7: Print element not found Step 8: Exit Linear Search Example Let us take an example of an array A[ 7 ]={ 5 , 2 , 1 , 6 , 3 , 7 , 8 }. Array A has 7 items. Let us assume we are looking for 7 in the array. Targeted item=7. Here, we have A[ 7 ]={ 5 , 2 , 1 , 6 , 3 , 7 , 8 } X= At first, When i=0 (A[0]=5; X=7) not matched i++ now, i=1 (A[ 1 ]=2; X=7) not matched i++ now, i=2(A[2])=1; X=7)not matched … …. i++ when, i=5(A[5]=7; X=7) Match Found Hence, Element X=7 found at index 5. Linear search is rarely used practically. The time complexity of above algorithm is O(n). int linearSearch(int values[], int target, int n) {

Time complexity of Binary search algorithm is O(logn) or O(n). int binarySearch(int values[], int len, int target) { int max = (len - 1 ); int min = 0; int guess; // index of middle elements int step = 0; // to find out in how many steps we completed the search while(max >= min) { guess = (max + min) / 2; // we made the first guess, incrementing step by 1 step++; if(values[guess] == target) { printf("Number of steps required for search: %d \n", step); return guess; } else if(values[guess] > target) { // target would be in the left half max = (guess - 1 ); } else { // target would be in the right half min = (guess + 1 ); } } // Element not found return - 1; }

Interpolation Search Algorithm

Interpolation Search Algorithm is an improvement of Binary Search. It works on the probing position of the required item. It works properly in sorted and equally distributed data lists. Algorithm Step 1: Start searching data from middle of the list. Step 2: If it is a match, return the index of the item, and exit. Step 3: If it is not a match, probe position. Step 4: Divide the list using probing formula and find the new middle. Step 5: If data is greater than middle, search in higher sub-list. Step 6: If data is smaller than middle, search in lower sub-list. Step 7: Repeat until match. To divide the list into two sub lists, we use mid = Lo + ((Hi – Lo) / (A[Hi] – A[Lo])) * (X – A[Lo])

Where, A = list Lo = Lowest index of the list Hi = Highest index of the list A[n] = Value stored at index n in the list Runtime complexity of interpolation search algorithm is Ο(log (log n)). What is sorting? Sorting is the process of placing elements from a collection in some kind of order. For example, a list of words could be sorted alphabetically or by length. Efficient sorting is important to optimize the use of other algorithms that require sorted lists to work correctly. Importance of sorting To represent data in more readable format. Optimize data searching to high level. The most common sorting algorithms are: Bubble Sort Insertion Sort Selection Sort Quick Sort Merge Sort Shell Sort

Bubble Sort

Bubble sort is the simplest sorting algorithm. It is based on comparison where each adjacent pair of element is compared and swapped if they are not in order. It works by repeatedly stepping through the list to be sorted, comparing two items at a time and swapping them if they are in the wrong order. The pass through the list is repeated until no swaps are needed, which means the list is sorted. This algorithm is not suitable for huge data sets. Average and worst case time complexity of this algorithm are of Ο(n2) where n is the number of items. Algorithm for i=N- 1 to 2 { set swap flag to false for j=1 to i { if list[j- 1 ] > list[j] swap list[j- 1 ] and list[j]

Selection Sort

Selection sort is an in-place comparison sort algorithm. In this algorithm, we repeatedly select the smallest remaining element and move it to the end of a growing sorted list. It is one of the simplest sorting algorithm. Selection sort is known for its simplicity. It has performance advantages over more complicated algorithms in certain situations. This algorithm is not suitable for large data sets as its average and worst case complexities are of Ο(n 2 ) , where n is the number of items. Algorithm Step 1: Set MIN to location 0 Step 2: Search the minimum element in the list Step 3: Swap with value at location MIN Step 4: Increment MIN to point to next element Step 5: Repeat until list is sorted Example, Let us assume an array A[ 10 ]={ 45 , 20 , 40 , 05 , 15 , 25 , 50 , 35 , 30 , 10 }. We have to sort this array using selection sort. In this algorithm we have to find the minimum value in the list first. Then, Swap it with the value in the first position. After that, Start from the second position and repeat the steps above for remainder of the list. // function to look for smallest element in the given subarray int indexOfMinimum(int arr[], int startIndex, int n) { int minValue = arr[startIndex]; int minIndex = startIndex; for(int i = minIndex + 1; i < n; i++) { if(arr[i] < minValue) { minIndex = i; minValue = arr[i]; } }

return minIndex; } void selectionSort(int arr[], int n) { for(int i = 0; i < n; i++) { int index = indexOfMinimum(arr, i, n); swap(arr, i, index); } }

Insertion Sort

Insertion sort is an in-place sorting algorithm based on comparison. It is a simple algorithm where a sorted sub list is maintained by entering on element at a time. An element which is to be inserted in this sorted sub-list has to find its appropriate location and then it has to be inserted there. That is the reason why it is named so. This algorithm is not suitable for large data set. Average and worst case time complexity of the algorithm is Ο(n 2 ) , where n is the number of items. Algorithm Step 1: If it is the first element, it is already sorted. return 1; Step 2: Pick next element Step 3: Compare with all elements in the sorted sub-list Step 4: Shift all the elements in the sorted sub-list that is greater than the value to be sorted Step 5: Insert the value Step 6: Repeat until list is sorted Example, Let us take an example of an array A[ 6 ]={ 20 , 10 , 30 , 15 , 25 , 05 }. We have to sort this array using insertion sort.

Step 4: right points to the high Step 5: while value at left is less than pivot move right Step 6: while value at right is greater than pivot move left Step 7: if both step 5 and step 6 does not match swap left and right Step 8: if left ≥ right, the point where they met is new pivot In practice, quick sort is faster than other sorting algorithms because its inner loop can be efficiently implemented on most architectures, and in most real-world data it is possible to make design choices which minimize the possibility of require time. Example, Let us assume an array A[ 10 ]={ 42 , 37 , 11 , 98 , 36 , 72 , 65 , 10 , 88 , 78 }. We have to sort this array using quick sort.

// to swap two numbers void swap(int* a, int* b) { int t = *a; *a = *b; b = t; } / a[] is the array, p is starting index, that is 0, and r is the last index of array. */ void quicksort(int a[], int p, int r) { if(p < r) {

void mergeSort(int a[], int p, int r) { int q; if(p < r) { q = (p + r) / 2; mergeSort(a, p, q); mergeSort(a, q+ 1 , r); merge(a, p, q, r); } } // function to merge the subarrays void merge(int a[], int p, int q, int r) { int b[5]; //same size of a[] int i, j, k; k = 0; i = p; j = q + 1; while(i <= q && j <= r) { if(a[i] < a[j]) { b[k++] = a[i++]; // same as b[k]=a[i]; k++; i++; } else { b[k++] = a[j++]; } } while(i <= q) { b[k++] = a[i++]; }

while(j <= r) { b[k++] = a[j++]; } for(i=r; i >= p; i--) { a[i] = b[--k]; // copying back the sorted list to a[] } }

Shell Sort

Shell sort is the generalization of insertion sort. It improves insertion sort by comparing elements separated by a gap of several positions. In this algorithm we avoid large shifts. This algorithm uses insertion sort on a widely spread elements, first to sort them and then sorts the less widely spaced elements. This spacing is termed as interval. We calculate the intervals using Knuth’s formula i.e., h=h*+ 1 where, h is interval with initial value 1. Average and worst case complexity of this algorithm are Ο(n) , where n is the number of items. Algorithm Step 1: Initialize the value of h Step 2: Divide the list into smaller sub-list of equal interval h Step 3: Sort these sub-lists using insertion sort Step 4: Repeat until complete list is sorted Example, Let us take an example of an array A[ 1 3]={ 45 , 36 , 75 , 20 , 05 , 90 , 80 , 65 , 30 , 50 , 10 , 75 , 85 }. We have to sort it using Shell sort.

 Step 3 - Insert each number into their respective queue based on the least significant digit.

 Step 4 - Group all the numbers from queue 0 to queue 9 in the order they have inserted into their respective queues.

 Step 5 - Repeat from step 3 based on the next least significant digit.

 Step 6 - Repeat from step 2 until all the numbers are grouped based on the most significant digit.

Example