Professional Documents
Culture Documents
of
Session 2023-24
INDEX
Date of Date of
S.no. Name of the experiment Remarks
conduction submission
1 Program to implement Insertion sort 17/aug/23 24/aug/23
2 Program to implement Selection sort 17/aug/23 24/aug/23
3 Program to implement bubble sorting 18/aug/23 24/aug/23
4 Program to implement Count sort 24/aug/23 24/aug/23
5 Program to implement Quick sort 7/sep/23 24/aug/23
6 Program to implement Merge sort 14/sept/23 24/aug/23
7 Program to implement Binary search 20/sep/23 24/aug/23
8 Program to implement Heap sort 21/sep/23 24/aug/23
Write a JAVA program to implement
9 19/oct/23 24/aug/23
Dijkstra’s Algorithm.
Write a program to implement 0-1
10 26/oct 24/aug/23
Knapsack problem.
Algorithm:
Insertion Sort(arr, size)
consider 0th element as sorted part
for each element from i=2 to n-1
tmp = arr[i]
for j=i-1 to 0
If a[j]>tmp
Then right shift it by one position
put tmp at current j
Program:
public class InsertionSortExample {
public static void insertionSort(int array[]) {
int n = array.length;
for (int j = 1; j < n; j++) {
int key = array[j];
int i = j-1;
while ( (i > -1) && ( array [i] > key ) ) {
array [i+1] = array [i];
i--;
}
array[i+1] = key;
}
}
Output:
Complexity:
Algorithm-
for i = 1 to size - 1
minimum = j;
end if
end for
if indexofMinimum != i then //swap the minimum element with the current element
end if
end for
end function
import java.util.Scanner;
int n = scanner.nextInt();
arr[i] = scanner.nextInt();
selectionSort(arr);
System.out.println("Sorted array:");
scanner.close();
int n = arr.length;
int minIndex = i;
{ minIndex = j;
arr[minIndex] = temp;
Output:
Complexity:
The complexity of Selection sort is O(n^2)
begin BubbleSort(arr)
for all array elements
if arr[i] > arr[i+1]
swap(arr[i], arr[i+1])
end if
end for
return arr
end BubbleSort
Program:-
import java.util.Scanner;
int n = scanner.nextInt();
bubbleSort(arr);
System.out.println("Sorted array:");
scanner.close();
{ int n = arr.length;
boolean swapped;
{ swapped = false;
arr[j + 1] = temp;
swapped = true;
if (!swapped)
{ break;
Complexity:
algorithm has a worst-case time complexity of O(n2). The bubble sort has a space
complexity of O(1).
for i = 0 to n
find the count of every unique element and
store that count at ith position in the count array
for j = 1 to max
Now, find the cumulative sum and store it in count array
for i = n to 1
Restore the array elements
Decrease the count of every restored element by 1
end countingSort
Program:
class CountingSort {
/* This loop will find the index of each element of the original array in
Output:
Time Complexity:
Algorithm-
Partition Algorithm:
import java.util.*;
class QuickSort {
int i = start-1;
for(int j=start;j<=end;j++){
if(arr[j]<=arr[pivot]){
i++;
arr[i] = arr[j];
arr[j] = temp;
return i;
if(start<end){
quickSort(arr,start,pivot-1);
quickSort(arr,pivot+1,end);
int n=sc.nextInt();
System.out.println("Enter elements:");
for(int i=0;i<n;i++){
arr[i]=sc.nextInt();
quickSort(arr,0,n-1);
for(int i=0;i<n;i++){
System.out.print(arr[i]+" ");
System.out.println();
Output:
Algorithm-
END MERGE_SORT
Program:
import java.util.*;
class MergeSort {
for(int i=0;i<=middle-left;i++){
leftTempArray[i] = arr[left+i];
for(int i=0;i<right-middle;i++){
rightTempArray[i] = arr[middle+1+i];
rightTempArray[right-middle] = Integer.MAX_VALUE;
int i=0,j=0;
for(int k=left;k<=right;k++){
if(leftTempArray[i]<rightTempArray[j]){
arr[k] = leftTempArray[i];
i++;
}else{
arr[k] = rightTempArray[j];
j++;
if(right>left){
int m= (left+right)/2;
mergeSort(arr,left,m);
mergeSort(arr,m+1,right);
merge(arr,left,m,right);
int n=sc.nextInt();
System.out.println("Enter elements:");
for(int i=0;i<n;i++){
arr[i]=sc.nextInt();
mergeSort(arr,0,n-1);
for(int i=0;i<n;i++){
System.out.print(arr[i]+" ");
System.out.println();
Output:
Algorithm:
Program:
class BinarySearch {
static int binarySearch(int a[], int beg, int end, int val)
{
int mid;
if(end >= beg)
{
Time Complexity:
The time complexity of binary search is O(logn)
Algorithm-
HeapSort(arr)
BuildMaxHeap(arr)
for i = length(arr) to 2
swap arr[1] with arr[i]
heap_size[arr] = heap_size[arr] ? 1
MaxHeapify(arr,1)
End
BuildMaxHeap(arr)
BuildMaxHeap(arr)
heap_size(arr) = length(arr)
for i = length(arr)/2 to 1
MaxHeapify(arr,i)
End
L = left(i)
R = right(i)
if L ? heap_size[arr] and arr[L] > arr[i]
largest = L
else
largest = i
if R ? heap_size[arr] and arr[R] > arr[largest]
largest = R
if largest != i
End
Program-
class HeapSort
{
static void heapify(int a[], int n, int i)
{
int largest = i; // Initialize largest as root
int left = 2 * i + 1; // left child
int right = 2 * i + 2; // right child
// If left child is larger than root
if (left < n && a[left] > a[largest])
largest = left;
// If right child is larger than root
if (right < n && a[right] > a[largest])
largest = right;
// If root is not largest
if (largest != i) {
// swap a[i] with a[largest]
int temp = a[i];
a[i] = a[largest];
a[largest] = temp;
heapify(a, n, largest);
}
}
/*Function to implement the heap sort*/
static void heapSort(int a[], int n)
{
for (int i = n / 2 - 1; i >= 0; i--)
heapify(a, n, i);
heapify(a, i, 0);
}
}
/* function to print the array elements */
static void printArr(int a[], int n)
{
for (int i = 0; i < n; ++i)
System.out.print(a[i] + " ");
}
public static void main(String args[])
{
int a[] = {45, 7, 20, 40, 25, 23, -2};
int n = a.length; System.out.print("Before sorting array elements are - \n");
printArr(a, n);
heapSort(a, n);
System.out.print("\nAfter sorting array elements are - \n");
printArr(a, n);
}
}
Output:
Time complexity: The best-case time complexity of heap sort is O(n logn).
Algorithm-
STEP: Create a visited boolean array of size = vertices, that initially contain false at each index
describing that no vertex has been visited yet.
STEP: Create an integer array called distance that contains the distance of that node from the
source node.
STEP: Mark distance[source] = 0 and the distance of other vertices to be infinity. Also, mark
visited [source] = true.
STEP: Start from the source node and explore all the neighbors of the source node that have
been marked as non-visited.
STEP: If the weight of edge + distance [currentVertex] is less than distance[vertex], then update
the distance[vertex] = weight of Egde + distance[currentVertex].
STEP: Repeat step 5 for all the unvisited neighbors of the current vertex.
STEP: Now we take the index having a minimum value from the distance array whose visited is
marked as false as the next current vertex.
Program:
distance[i] = Integer.MAX_VALUE; }
distance[source] = 0;
visitedVertex[u] = true;
minDistance = distance[i];
minDistanceVertex = i; } }
return minDistanceVertex; }
public static void main(String[] args) {
int graph[][] = new int[][] {
{ 0, 0, 1, 2, 0, 0, 0 }, { 0, 0, 2, 0, 0, 3, 0 },
{ 1, 2, 0, 1, 3, 0, 0 }, { 2, 0, 1, 0, 0, 0, 1 },
{ 0, 0, 3, 0, 0, 2, 0 }, { 0, 3, 0, 0, 2, 0, 1 },
{ 0, 0, 0, 1, 0, 1, 0 } };
Dijkstra T = new Dijkstra();
T.dijkstra(graph, 0); } }
Output:
Algorithm-
Dynamic-0-1-knapsack (v, w, n, W)
for w = 0 to W do
c[0, w] = 0
for i = 1 to n do
c[i, 0] = 0
for w = 1 to W do
if wi ≤ w then
if vi + c[i-1, w-wi] then
c[i, w] = vi + c[i-1, w-wi]
else c[i, w] = c[i-1, w]
else
c[i, w] = c[i-1, w]
Program:
class Knapsack {
if (n == 0 || W == 0) return 0;
if (wt[n - 1] > W)
int W = 50;
int n = profit.length;
} }
Output:
Time complexity:
The time complexity for the 0/1 Knapsack problem solved using DP is O(N*W) where N
denotes the number of items available and W denotes the capacity of the knapsack.