Professional Documents
Culture Documents
ADA Prac 1to5
ADA Prac 1to5
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
int n = arr.length;
arr[j] = arr[j+1];
arr[j+1] = temp;
180410107115 TY CE-2/BATCH-C
Solanki Vishva
int n = arr.length;
System.out.println();
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)
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
int n = arr.length;
int min_idx = i;
min_idx = j;
arr[min_idx] = arr[i];
arr[i] = temp;
180410107115 TY CE-2/BATCH-C
Solanki Vishva
int n = arr.length;
System.out.print(arr[i]+" ");
System.out.println();
ob.sort(arr);
System.out.println("Sorted array");
ob.printArray(arr);
180410107115 TY CE-2/BATCH-C
Solanki Vishva
= 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 {
int n = arr.length;
int j = i - 1;
arr[j + 1] = arr[j];
j = j - 1;
arr[j + 1] = key;
180410107115 TY CE-2/BATCH-C
Solanki Vishva
int n = arr.length;
System.out.println();
int arr[] = { 7, 4, 8, 5, 6 };
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)
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 {
int n1 = m - l + 1;
int n2 = r - m;
int i = 0, j = 0;
int k = l;
180410107115 TY CE-2/BATCH-C
Solanki Vishva
arr[k] = L[i];
i++;
else {
arr[k] = R[j];
j++;
k++;
arr[k] = L[i];
i++;
k++;
arr[k] = R[j];
j++;
k++;
180410107115 TY CE-2/BATCH-C
Solanki Vishva
if (l < r) {
int m = (l + r) / 2;
sort(arr, l, m);
sort(arr, m + 1, r);
merge(arr, l, m, r);
int n = arr.length;
System.out.println();
180410107115 TY CE-2/BATCH-C
Solanki Vishva
System.out.println("Given Array");
printArray(arr);
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)
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 i = (low-1);
i++;
arr[i] = arr[j];
arr[j] = temp;
180410107115 TY CE-2/BATCH-C
Solanki Vishva
arr[i+1] = arr[high];
arr[high] = temp;
return i+1;
int n = arr.length;
System.out.print(arr[i]+" ");
System.out.println();
180410107115 TY CE-2/BATCH-C
Solanki Vishva
int arr[] = {10, 70, 80, 40, 20, 50, 30, 90, 60};
int n = arr.length;
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. θ
Best Case: The best case occurs when the partition process always picks the
middle element as pivot. Following is recurrence for best case.
Average 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
2) Linear Search:
180410107115 TY CE-2/BATCH-C
Solanki Vishva
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
PRACTICAL-3
180410107115 TY CE-2/BATCH-C
Solanki Vishva
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
#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