Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 35

Solanki Vishva

ADA
Practical-1

1)Bubble Sort:-
Defination : Sometimes reffered to as sinking sort, it is simplest sorting algorithm
that repetedly steps through the list, compare adjacent element and swap the if they
are in the wrong order.The pass through the list until the list is repeted until the list
is sorted.

class BubbleSort

void bubbleSort(int arr[])

int n = arr.length;

for (int i = 0; i < n-1; i++)

for (int j = 0; j < n-i-1; j++)

if (arr[j] > arr[j+1])

// swap arr[j+1] and arr[i]

int temp = arr[j];

arr[j] = arr[j+1];

arr[j+1] = temp;

180410107115 TY CE-2/BATCH-C
Solanki Vishva

void printArray(int arr[])

int n = arr.length;

for (int i=0; i<n; ++i)

System.out.print(arr[i] + " ");

System.out.println();

public static void main(String args[])

BubbleSort ob = new BubbleSort();

int arr[] = {77, 55, 33, 44, 22, 11, 66};

ob.bubbleSort(arr);

System.out.println("Sorted array");

ob.printArray(arr);

180410107115 TY CE-2/BATCH-C
Solanki Vishva

Time complexity :
T(n) = (n-1)+(n-2)+(n-3)+......+1
= n(n-1)/2
= n2/2-n/2
= n2
= o(n2)

Worst case Time Complexity = o(n2)


Best case Time Complexity = o(n)
Average case Time Complexity = o(n2)

180410107115 TY CE-2/BATCH-C
Solanki Vishva

2)Selection Sort:-
Defination : This sorting algorithm is an in-place comparison based algorithm in
which the list is divided into two parts, sorted part is left end and unsorted part is
right end. Initially, the sorted part is empty and unsorted part is the entire list.

class SelectionSort

void sort(int arr[])

int n = arr.length;

for (int i = 0; i < n-1; i++)

int min_idx = i;

for (int j = i+1; j < n; j++)

if (arr[j] < arr[min_idx])

min_idx = j;

int temp = arr[min_idx];

arr[min_idx] = arr[i];

arr[i] = temp;

void printArray(int arr[])

180410107115 TY CE-2/BATCH-C
Solanki Vishva

int n = arr.length;

for (int i=0; i<n; ++i)

System.out.print(arr[i]+" ");

System.out.println();

public static void main(String args[])

SelectionSort ob = new SelectionSort();

int arr[] = {50,20,30,40,10};

ob.sort(arr);

System.out.println("Sorted array");

ob.printArray(arr);

180410107115 TY CE-2/BATCH-C
Solanki Vishva

Time complexity = Comperision + Movement

J=2 = 1+1 = 2 2(1)


J=3 = 2+2 = 4 2(2)
J=4 = 3+3 = 6 2(3)
: : : :
: : : :
J=n = (n-1)+(n-1)=2n-2 2(n-1)

= 2(1)+2(2)+......+2(n-1)
= n(n-1)
= n2-n
= o(n2)

180410107115 TY CE-2/BATCH-C
Solanki Vishva

3)Insertion Sort:-
Defination : Insertion sort is a simple sorting algorithm that builds the final sorted
array one item at a time.It is much less efficient on large lists than more advanced
algorithm such as quicksort,heapsort, or merge sort.

class InsertionSort {

void sort(int arr[])

int n = arr.length;

for (int i = 1; i < n; ++i) {

int key = arr[i];

int j = i - 1;

while (j >= 0 && arr[j] > key) {

arr[j + 1] = arr[j];

j = j - 1;

arr[j + 1] = key;

static void printArray(int arr[])

180410107115 TY CE-2/BATCH-C
Solanki Vishva

int n = arr.length;

for (int i = 0; i < n; ++i)

System.out.print(arr[i] + " ");

System.out.println();

public static void main(String args[])

int arr[] = { 7, 4, 8, 5, 6 };

InsertionSort ob = new InsertionSort();

ob.sort(arr);

System.out.println("Sorted array");

printArray(arr);

180410107115 TY CE-2/BATCH-C
Solanki Vishva

Time complexity :
T(n) = (n-1)+(n-2)+(n-3)+......+1
= n(n-1)/2
= n2/2-n/2
= n2
= o(n2)

Worst case Time Complexity = o(n2)


Best case Time Complexity = o(n)
Average case Time Complexity = o(n2)

180410107115 TY CE-2/BATCH-C
Solanki Vishva

4)Merge Sort:-
Defination : In computer science merge sort is an efficient, general-purpose,
comparison –based sorting algorithm. Most implementation produce a stable sort,
that the order of equal elements is the same in the inpute and output.

class MergeSort {

void merge(int arr[], int l, int m, int r)

int n1 = m - l + 1;

int n2 = r - m;

int L[] = new int[n1];

int R[] = new int[n2];

for (int i = 0; i < n1; ++i)

L[i] = arr[l + i];

for (int j = 0; j < n2; ++j)

R[j] = arr[m + 1 + j];

int i = 0, j = 0;

int k = l;

while (i < n1 && j < n2) {

180410107115 TY CE-2/BATCH-C
Solanki Vishva

if (L[i] <= R[j]) {

arr[k] = L[i];

i++;

else {

arr[k] = R[j];

j++;

k++;

while (i < n1) {

arr[k] = L[i];

i++;

k++;

while (j < n2) {

arr[k] = R[j];

j++;

k++;

180410107115 TY CE-2/BATCH-C
Solanki Vishva

void sort(int arr[], int l, int r)

if (l < r) {

int m = (l + r) / 2;

sort(arr, l, m);

sort(arr, m + 1, r);

merge(arr, l, m, r);

static void printArray(int arr[])

int n = arr.length;

for (int i = 0; i < n; ++i)

System.out.print(arr[i] + " ");

System.out.println();

public static void main(String args[])

int arr[] = { 12, 11, 13, 5, 9, 6, 7 ,10 ,8};

180410107115 TY CE-2/BATCH-C
Solanki Vishva

System.out.println("Given Array");

printArray(arr);

MergeSort ob = new MergeSort();

ob.sort(arr, 0, arr.length - 1);

System.out.println("\nSorted array");

printArray(arr);

180410107115 TY CE-2/BATCH-C
Solanki Vishva

Time complexity :
Height Time
per level
N o(n)

o(nlogn) n/2 n/2 o(n)

n/4 n/4 n/4 n/4 o(n)


:
:

O(nlogn)
s

5)Quick Sort:-

180410107115 TY CE-2/BATCH-C
Solanki Vishva

Definition: Quicksort is a divide and conquer algorithm. The steps are: 1) Pick
an element from the array, this element is called as pivot element. 2) Divide the
unsorted array of elements in two arrays with values less than the pivot come in
the first sub array, while all elements with values greater than the pivot come in
the second sub-array (equal values can go either way). This step is called the
partition operation. 3) Recursively repeat the step 2(until the sub-arrays are
sorted) to the sub-array of elements with smaller values and separately to the
sub-array of elements with greater values. The same logic we have
implemented in the following C program.

class QuickSort

int partition(int arr[], int low, int high)

int pivot = arr[high];

int i = (low-1);

for (int j=low; j<high; j++)

if (arr[j] < pivot)

i++;

int temp = arr[i];

arr[i] = arr[j];

arr[j] = temp;

180410107115 TY CE-2/BATCH-C
Solanki Vishva

int temp = arr[i+1];

arr[i+1] = arr[high];

arr[high] = temp;

return i+1;

void sort(int arr[], int low, int high)

if (low < high)

int pi = partition(arr, low, high)

sort(arr, low, pi-1);

sort(arr, pi+1, high);

static void printArray(int arr[])

int n = arr.length;

for (int i=0; i<n; ++i)

System.out.print(arr[i]+" ");

System.out.println();

180410107115 TY CE-2/BATCH-C
Solanki Vishva

public static void main(String args[])

int arr[] = {10, 70, 80, 40, 20, 50, 30, 90, 60};

int n = arr.length;

QuickSort ob = new QuickSort();

ob.sort(arr, 0, n-1);

System.out.println("sorted array");

printArray(arr);

180410107115 TY CE-2/BATCH-C
Solanki Vishva

Analysis:

Worst case: The worst case occurs when the partition process always picks
greatest or smallest element as pivot. If we consider above partition strategy where
last element is always picked as pivot, the worst case would occur when the array
is already sorted in increasing or decreasing order. Following is recurrence for
worst case. θ 

T(n) = T(n) + T(n-1) + θ(n)


Which is Equivalent to
T(n) = T(n-1) + θ(n)

The solution of above recurrence is θ(n2).

Best Case: The best case occurs when the partition process always picks the
middle element as pivot. Following is recurrence for best case.

T(n) = 2T(n/2) + θ(n)

The solution of above recurrence is θ(nLogn).

Average case:

To do average case analysis, we need to consider all possible permutation of array


and calculate time taken by every permutation which doesn’t look easy.
We can get an idea of average case by considering the case when partition puts
O(n/9) elements in one set and O(9n/10) elements in other set. Following is
recurrence for this case.

180410107115 TY CE-2/BATCH-C
Solanki Vishva

PRACTICAL-2
> Implementation and Time analysis of linear and binary
search algorithm.
 
Search a sorted array by repeatedly dividing the search interval in half. Begin with
an interval covering the whole array. If the value of the search key is less than the
item in the middle of the interval, narrow the interval to the lower half. Otherwise
narrow it to the upper half. Repeatedly check until the value is found or the interval
is empty.

1) Binary Search:

180410107115 TY CE-2/BATCH-C
Solanki Vishva

A binary search algorithm is used to find the position of a specific value contained
in a sorted array. Working with the principle of divide and conquer.

#include <stdio.h>
int main()
{
printf("Name: Vishva Solanki\n");
printf("Enrollment No.: 180410107115\n\n");
int first, last, mid, n, search, array[50];
printf("How many elements do you want in Array? ");
scanf("%d",&n);
printf("Enter array elements:\n");
for(int i=0;i<n;i++)
scanf("%d",&array[i]);
printf("Enter Element to Search: ");
scanf("%d",&search);
first=0;
last=n-1;
mid=(first+last)/2;
while(first<=last)
{
if(array[mid]<search)
first=mid+1;
else if(array[mid]==search)
{
printf("Element %d is present at Index %d in Array",search,mid+1);
break;
}
else
last=mid-1;
mid=(first+last)/2;
}
if(first>last)
printf("Not found! %d is not present in the Array",search);
return 0;
}

180410107115 TY CE-2/BATCH-C
Solanki Vishva

Output:-

180410107115 TY CE-2/BATCH-C
Solanki Vishva

Time Complexity of Binary Search Algorithm is O(log2n).


Here, n is the number of elements in the sorted linear array.

2) Linear Search:

180410107115 TY CE-2/BATCH-C
Solanki Vishva

A linear search, also known as a sequential search, is a method of finding an


element within a list. It checks each element of the list sequentially until a match is
found or the whole list has been searched.

A simple approach is to do linear search, i.e Start from the leftmost element of arr[]
and one by one compare x with each element of arr[].
If x matches with an element, return the index.If x doesn’t match with any of
elements, return -1.

#include<stdio.h>
int main()
{
printf("Name: Vishva Solanki\n");
printf("Enrollment No.: 180410107115\n\n");
int arr[20],search,n,i;
printf("How many elements do you want in Array? ");
scanf("%d",&n);
printf("Enter array elements:\n");
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
printf("Enter Element to Search: ");
scanf("%d",&search);
for(i=0;i<n;i++)
if(arr[i]==search)
break;
if(i<n)
printf("Element %d Found at Index %d in Array",search,i);
else
printf("Element %d not Found in Array",search);
return 0;
}

Output:

180410107115 TY CE-2/BATCH-C
Solanki Vishva

Time Complexity of Linear Search Algorithm is O(n).


Here, n is the number of elements in the linear array.

PRACTICAL-3

180410107115 TY CE-2/BATCH-C
Solanki Vishva

> Implementation of max-heap sort algorithm.

Heap sort is a comparison based sorting technique based on Binary Heap data
structure. It is similar to selection sort where we first find the maximum element
and place the maximum element at the end. We repeat the same process for
remaining element.

#include<stdio.h>
void heapify(int arr[],int n,inti)
{
int largest=i;
int l = 2*i + 1;
int r = 2*i + 2;
if (l < n &&arr[l] >arr[largest])
largest = l;
if (r < n &&arr[r] >arr[largest])
largest = r;
int temp;
if (largest != i)
{
temp=arr[i];
arr[i]=arr[largest];
arr[largest]=temp;
heapify(arr, n, largest);
}
}
void heapSort(int arr[], int n)
{
int i;
for (i = n / 2 - 1; i>= 0; i--)
{
heapify(arr, n, i);

180410107115 TY CE-2/BATCH-C
Solanki Vishva

}
int temp;
for (i=n-1; i>=0; i--)
{
temp=arr[0];
arr[0]=arr[i];
arr[i]=temp;
heapify(arr, i, 0);
}
}
void printArray(int arr[], int n)
{
int i;
for (i = 0; i< n; i++)
{
printf("%d ",arr[i]);
}
}
int main()
{
printf("Name: Solanki Vishva\n");
printf("Enrollment No.: 180410107115\n\n");
int arr[]= {5,9,0,14,2};
int n=5;
printf("Entered Array: ");
printArray(arr, n);
heapSort(arr, n);
printf("\nSorted Array: ");
printArray(arr, n);
}

180410107115 TY CE-2/BATCH-C
Solanki Vishva

Output:

Time Complexity:
Best case=Average case=Worst case=O(nlogn)

PRACTICAL-4

180410107115 TY CE-2/BATCH-C
Solanki Vishva

> Implementation and Time analysis of factorial program using iterative and
recursive method.

Iterative Method:
To calculate the factorial in a Loop, It seems like all we would have to do is start
from x and then multiply by all integer values below x, and just hold that value
until we done iterative.

Below is the source code for c program to find factorial by recursion and iteration
method which is successfully compiled and run on windows system to produce
desired output as shown below.

Recursive Method:
We will use recursive user defines function to perform the task. Here we have a
function fact() that call itself in a recursive manner to find out the factorial of input
number.

1) Recursive Solution:

#include <stdio.h>
unsigned int factorial(unsigned int n)
{
if(n==0)
return 1;
return n*factorial(n-1);
}
int main()
{
printf("Name: Solanki Vishva\n");
printf("Enrollment No.: 180410107115\n\n");
int num;
printf("Enter Integer value: ");
scanf("%d",&num);
printf("Factorial of %d is %d (Using Recursive Method)", num,
factorial(num));

180410107115 TY CE-2/BATCH-C
Solanki Vishva

return 0;
}

Output:

180410107115 TY CE-2/BATCH-C
Solanki Vishva

2) Iterative Solution:
Using for Loop:

180410107115 TY CE-2/BATCH-C
Solanki Vishva

#include <stdio.h>
unsigned int factorial(unsigned int n)
{
int res=1, i;
for(i=2; i<=n; i++)
res*=i;
return res;
}
int main()
{
printf("Name: Solanki Vishva\n");
printf("Enrollment No.: 180410107115\n\n");
int num;
printf("Enter Integer value: ");
scanf("%d",&num);
printf("Factorial of %d is %d (Iterative Method- Using For Loop)", num,
factorial(num));
return 0;
}

Output:

180410107115 TY CE-2/BATCH-C
Solanki Vishva

PRACTICAL-5

180410107115 TY CE-2/BATCH-C
Solanki Vishva

>Implementation of a knapsack problem using dynamic programming.

KNAPSACK PROBLEM is a very helpful problem in combinatoricks. In the


supermarket there are n packages (n ≤ 100) the package i has weight W[i] ≤ 100
and value V[i] ≤ 100. A thief breaks into the supermarket; the thief cannot carry
weight exceeding M (M ≤ 100). The problem to be solved here is: which packages
the thief will take away to get the highest value?

#include<stdio.h>
int max(int a, int b)
{
return (a>b)?a:b;
}
int knapSack(int W, int wt[], int val[], int n)
{
int i, w;
int K[n+1][W+1];
for(i=0; i<=n; i++)
{
for(w=0; w<=W; w++)
{
if(i==0 || w==0)
K[i][w]=0;
else if(wt[i-1]<=w)
K[i][w]=max(val[i-1]+K[i-1][w-wt[i-1]],K[i-1][w]);
else
K[i][w]=K[i-1][w];
}
}
return K[n][W];
}

180410107115 TY CE-2/BATCH-C
Solanki Vishva

int main()
{
printf("Name: Solanki Vishva\n");
printf("Enrollment No.: 180410107115\n\n");
int i, n, val[20], wt[20], W;
printf("Enter number of Items: ");
scanf("%d", &n);
printf("Enter value and weight of Items:\n");
for(i=0; i<n; ++i)
{
scanf("%d\t%d",&val[i],&wt[i]);
}
printf("Enter size of knapsack: ");
scanf("%d", &W);
printf("Solution: %d", knapSack(W, wt, val, n));
return 0;
}

Output:

180410107115 TY CE-2/BATCH-C
Solanki Vishva

TimeComplexity: O(N*W).
where ‘N’ is the number of weight element and ‘W’ is capacity. As for every
weight element we traverse through all weight capacities 1<=w<=W.

180410107115 TY CE-2/BATCH-C

You might also like