DS Unit I
DS Unit I
UNIT I
Basic concepts - Data types, Abstract Data Types, Data structures, Algorithms.
Searching- Linear Search, Binary Search
Sorting- Bubble Sort, Insertion Sort, Selection Sort, Quick sort, Merge sort, Comparison of
Sorting methods.
Data Structures
Data may be organized in many different ways logical or mathematical model of a program
particularly organization of data. This organized data is called “Data Structure”.
Or
The organized collection of data is called a ‘Data Structure’.
Data Types
Primitive Data types are directly supported by the language (ie) any operation is directly
performed in these data items.
o Ex: integer, Character, Real numbers etc.
Non-primitive data types are not defined by the programming language, but are instead
created by the programmer.
1. Linear data structures organize their data elements in a linear fashion, where data
elements are attached one after the other. Linear data structures are very easy to
implement, since the memory of the computer is also organized in a linear fashion.
Some commonly used linear data structures are arrays, linked lists, stacks and
queues.
2. In nonlinear data structures, data elements are not organized in a sequential fashion.
Data structures like multidimensional arrays, trees, graphs, tables and sets are some
examples of widely used nonlinear data structures.
Algorithm
Definition: -
An algorithm is a Step By Step process to solve a problem, where each step indicates an
intermediate task. Algorithm contains finite number of steps that leads to the solution of the
problem.
Categories of Algorithm:
Based on the different types of steps in an Algorithm, it can be divided into three categories,
namely
Sequence
Selection and
Iteration
Sequence:
The steps described in an algorithm are performed successively one by one without
skipping any step.
The sequence of steps defined in an algorithm should be simple and easy to understand.
Each instruction of such an algorithm is executed, because no selection procedure or
conditional branching exists in a sequence algorithm.
Example:
// adding two numbers
Step 1: start
Step 2: read a,b
Step 3: Sum=a+b
Step 4: write Sum
Step 5: stop
Selection:
The sequence type of algorithms are not sufficient to solve the problems, which involves decision
and conditions. In order to solve the problem which involve decision making or option selection,
we go for Selection type of algorithm.
The general format of Selection type of statement is as shown below:
if(condition)
Statement-1;
else
Statement-2;
The above syntax specifies that if the condition is true, statement-1 will be executed otherwise
statement-2 will be executed. In case the operation is unsuccessful.
Then sequence of algorithm should be changed/ corrected in such a way that the system will re-
execute until the operation is successful.
Iteration: Iteration type algorithms are used in solving the problems which involves
repetition of statement.
In this type of algorithms, a particular number of statements are repeated ‘n’ no. of times.
Example:
Step 1 : start
Step 2 : read n
Step 3 : repeat
step 4 : until n>0
(a) r=n mod 10
(b) s=s+r
(c) n=n/10
Step 5 : write s
Step 6 : stop
1.Linear Search
• Linear search, the simplest search algorithm, is mainly used to find the element from
an unordered list.
• It is also known by another name called sequential search algorithm. In linear search,
the list is simply traversed, and each element in the list is matched with the element whose
location needs to be found.
• When the searching process is done, it returns the location of the element, else the
algorithm returns NULL.
for(i=0;i<size;i++)
{
if(search==a[i])
{
flag=1;
break;
}
}
if (flag==1)
printf("%d fount at the position %d ",search,mid+1);
else
printf("element not found");
return 0;
}
Advantages:
It is simplest known technique.
The elements in the list can be in any order.
Disadvantages:
This method is in efficient when large numbers of elements are present in list because time
taken for searching is more.
Complexity of Linear Search: The worst and average case complexity of Linear search is
O(n), where ‘n’ is the total number of elements present in the list.
Binary Search
Binary Search is one of the fastest searching algorithms.
It is used for finding the location of an element in a linear array.
It works on the principle of divide and conquer technique.
Binary Search Algorithm can be applied only on Sorted arrays.(Ascending Order)
So, the elements must be arranged in Either ascending order if the elements are numbers Or
dictionary order if the elements are strings.
To apply binary search on an unsorted array,First, sort the array using some sorting
technique.Then, use binary search algorithm.
start=0;
stop=size-1;
while(start<=stop)
{
mid=(start+stop)/2;
if(search==a[mid])
{
flag=1;
break;
}
else if (search<a[mid])
stop=mid-1;
else
start=mid+1;
}
if(flag==1)
printf("%d fount at the position %d ",search,mid+1);
else
printf("element not found");
return 0;
}
Types of Sorting
1.Bubble Sort
2.Selection Sort
3.Insertion Sort
4.Merge Sort
5.Quick Sort
1. BUBBLE SORT
Bubble sort is a sorting algorithm that compares two adjacent elements and swaps them until
they are in the intended order.
Just like the movement of air bubbles in the water that rise up to the surface, each element of
the array move to the end in each iteration.
Therefore, it is called a bubble sort.
Algorithm :
Step 0 : Initialize n to length of the array
Step 1 : Initialize j to 0 and compare a[j] with a[j+1] (compare adjacent elements starting
from 0th index)
Step 2 : if a[j] > a[j+1] swap a[j] and a[j+1]
Step 3 : repeat steps 1 and 2 until j reached end of the array ( by end of this one element will
be placed at its correct order )
Step 4 : continue from Step 1 n-1 times ( so that all elements will be in proper order)
Example:
printf("Values are");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n-1;i++)
{
for(j=0;j<n-1-i;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
printf("Ascending Order Using Bubble Sort \n");
for(i=0;i<n;i++)
printf("\t %d",a[i]);
return 0;
}
OUTPUT
printf("Enter Values");
for (i = 0; i < n; i++)
scanf("%d", &a[i]);
INSERTION SORT
Insertion sort is a sorting algorithm that places an unsorted element at its suitable place in
each iteration.
Insertion sort is a simple sorting algorithm that works similar to the way you sort playing
cards in your hands
Advantage
o Simple implementation
o Efficient for small data sets
o Adaptive, i.e., it is appropriate for data sets that are already substantially sorted.
Algorithm
Step 1 - If the element is the first element, assume that it is already sorted. Return 1.
Step 2 - Pick the next element, and store it separately in a key.
Step 3 - Now, compare the key with all elements in the sorted array.
Step 4 - If the element in the sorted array is smaller than the current element, then move to
the next element. Else, shift greater elements in the array towards the right.
Step 5 - Insert the value.
Step 6 - Repeat until the array is sorted.
EXAMPLE :
return 0;
}
OUTPUT
Enter the total Number of Elements : 5
Enter the Array Elements : 25
6
97
-1
15
Insertion Sort Result : -1 6 15 25 97
***********
MERGE SORT
Merge Sort is one of the most popular sorting algorithms that is based on the principle
of Divide and Conquer Algorithm.
Here, a problem is divided into multiple sub-problems.
Each sub-problem is solved individually.
Finally, sub-problems are combined to form the final solution.
Algorithm:
step 1: start
step 2: declare array and left, right, mid variable
step 3: perform merge function.
if left > right
return
mid= (left+right)/2
mergesort(array, left, mid)
mergesort(array, mid+1, right)
merge(array, left, mid, right)
step 4: Stop
Follow the steps below the solve the problem:
MergeSort(arr[], l, r)
If r > l
Find the middle point to divide the array into two halves:
middle m = l + (r – l)/2
Call mergeSort for first half:
Call mergeSort(arr, l, m)
Call mergeSort for second half:
Call mergeSort(arr, m + 1, r)
Merge the two halves sorted in steps 2 and 3:
Call merge(arr, l, m, r)
int main()
{
int a[50],i,j,size,temp,low,high,mid,mi,k;
partition(a,0,size-1);
return 0;
}
while(lower<=mid&&mi<=high)
{
if(a[lower]<=a[mi])
{
temp[i]=a[lower];
lower++;
}
else
{
temp[i]=a[mi];
mi++;
}
i++;
}
if(lower>mid)
{
for(k=mi;k<=high;k++)
{
temp[i]=a[k];
i++;
}
}
else
{
for(k=lower;k<=mid;k++)
{
temp[i]=a[k];
i++;
}
}
for(k=low;k<=high;k++)
{
a[k]=temp[k];
}
}
OUTPUT :
EXAMPLE
if(first<last)
{
pivot=first;
i=first;
j=last;
while(i<j)
{
while(a[i]<=a[pivot] && i<last)
i++;
while(a[j]>a[pivot])
j--;
if(i<j)
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
temp=a[pivot];
a[pivot]=a[j];
a[j]=temp;
quicksort(a,first,j-1);
quicksort(a,j+1,last);
}
}
int main()
{
int i, n, a[25];
quicksort(a,0,n-1);
OUTPUT
Enter the total Number of Elements : 5
Enter elements: 12 5 47 11 1
Order of Sorted elements: 1 5 11 12 47
Comparison of Sorting
**************