DAA PRACTICAL FILE _
DAA PRACTICAL FILE _
Algorithms
LAB Report
Submitted by Submitted to
1 Quick Sort 1
2 Merge Sort 3
3 Selection Sort 5
4 Bubble Sort 7
5 Insertion Sort 9
6 Linear Search 11
7 Binary Search 13
Experiment – 01
Objective:
Theory:
Quick Sort is a divide-and-conquer sorting algorithm. It selects a pivot
element and partitions the array so that elements smaller than the pivot are moved to
the left, and larger to the right. Then it recursively sorts the subarrays. The average
time complexity is O(n log n), and the worst-case is O(n²).
Implementation
1
public static void main(String[] args) {
int[] arr = { 10, 7, 8, 9, 1, 5 };
Output:
2
Experiment – 02
Objective:
Theory:
Merge Sort is a recursive sorting algorithm that splits the array into halves,
sorts each half, and then merges them. It is stable and has a consistent time complexity of
O(n log n) in all cases.
Implementation:
int i = 0, j = 0, k = l;
while (i < n1 && j < n2)
arr[k++] = (L[i] <= R[j]) ? L[i++] : R[j++];
while (i < n1)
arr[k++] = L[i++];
while (j < n2)
arr[k++] = R[j++];
}
3
public static void main(String[] args) {
int[] arr = { 12, 11, 13, 5, 6, 7 };
Output:
4
Experiment – 03
Objective:
Theory:
Selection Sort is a simple comparison-based sorting algorithm. It works by
repeatedly selecting the smallest (or largest) element from the unsorted portion and
moving it to the sorted portion. It has a time complexity of O(n²) for all cases. Though
inefficient on large lists, it is easy to understand and implement.
Implementation:
public class SelectionSort {
public static void selectionSort(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;
}
}
6
Experiment – 04
Objective:
Theory:
Bubble Sort is one of the simplest sorting algorithms. It works by repeatedly
stepping through the list, comparing adjacent elements, and swapping them if they are in
the wrong order. This process is repeated until the list is sorted. Though inefficient for
large datasets, it's useful for educational purposes. The time complexity is O(n²) in the
worst and average case, and O(n) in the best case when the list is already sorted.
Implementation:
Output:
8
Experiment – 05
Objective:
Theory:
Insertion Sort is a simple sorting algorithm that builds the final sorted array
one item at a time. It is much like sorting playing cards in your hands—by comparing each
new card with the already-sorted ones and placing it in the correct position. It has a time
complexity of O(n²) in the average and worst cases, and O(n) in the best case (when the
array is already sorted). It is efficient for small datasets and mostly sorted arrays.
Implementation:
10
Experiment – 06
Objective:
Theory:
Linear Search is the most basic searching technique. It works by checking each
element in the array one by one until the desired value is found or the end of the list is
reached. It does not require the array to be sorted. The time complexity is O(n) for all
cases.
Implementation:
if (result != -1)
System.out.println("Element found at index: " + result);
else
System.out.println("Element not found.");
}
}
11
Output:
12
Experiment – 07
Objective:
Theory:
Binary Search is an efficient searching algorithm used on sorted arrays. It
works by repeatedly dividing the search interval in half. If the target value is less than the
middle element, the search continues in the left half; otherwise, it continues in the right
half. Its time complexity is O(log n), making it much faster than linear search for large
datasets.
Implementation:
if (arr[mid] == target)
return mid;
else if (arr[mid] < target)
low = mid + 1;
else
high = mid - 1;
}
return -1;
}
Output:
14