Category Archives: Snippets

Implementing Greedy Knapsack Algorithm in Java

The Greedy Knapsack Problem/ Fractional Knapsack Problem is a classic example of a greedy algorithm. It differs from the 0/1 Knapsack Problem in that you are allowed to take fractions of an item rather than having to choose to take it or leave it entirely. In this blog post, we’ll walk through a Java implementation of the greedy approach to solving the fractional knapsack problem, explain how the logic works, and break down the sample output.

What is the Greedy Strategy for Knapsack?

In the greedy method, we aim to maximize the total profit by picking items with the highest profit-to-weight ratio. Here’s the approach:

  1. Calculate the profit-to-weight ratio for each item.
  2. Sort the items based on this ratio in descending order.
  3. Select items starting from the highest ratio until the knapsack is full:
    • If the item fits, include it completely.
    • If not, include as much of it as possible.
Continue reading Implementing Greedy Knapsack Algorithm in Java

Implementing Quicksort Algorithm in Java

Sorting is a fundamental operation in computer science, and QuickSort is one of the most popular and efficient sorting algorithms. In this blog post, we’ll look at a Java implementation of QuickSort, explain how it works, break down its steps with a sample output, and even visualize its working.

What is QuickSort?

QuickSort is a highly efficient, comparison-based, divide-and-conquer sorting algorithm. It was developed by Tony Hoare in 1959 and is widely used due to its performance on average-case scenarios.

How QuickSort Works:

  1. Divide: Select a pivot element from the array. Rearrange the array such that all elements less than the pivot go to its left, and all elements greater than the pivot go to its right.
  2. Conquer: Recursively apply the above step to the left and right subarrays.
  3. Combine: Since the array is sorted in-place, there’s no need for a combine step as in merge sort.
Continue reading Implementing Quicksort Algorithm in Java

Implementing Quicksort Algorithm in Java

Quicksort is a divide and conquer algorithm. Quicksort first divides a large array into two smaller sub-arrays: the low elements and the high elements. Quicksort can then recursively sort the sub-arrays.

The steps are:

  1. Pick an element, called a pivot, from the array.
  2. Reorder the array so that all elements with values less than the pivot come before the pivot, while all elements with values greater than the pivot come after it (equal values can go either way). After this partitioning, the pivot is in its final position. This is called the partition operation.
  3. Recursively apply the above steps to the sub-array of elements with smaller values and separately to the sub-array of elements with greater values.

(Via Wikipedia)

Continue reading Implementing Quicksort Algorithm in Java

Implementing Merge Sort Algorithm in Java

A merge sort works as follows:

  • Divide the unsorted list into n sublists, each containing 1 element (a list of 1 element is considered sorted).
  • Repeatedly merge sublists to produce new sorted sublists until there is only 1 sublist remaining. This will be the sorted list.
Merge Sort Example
Merge Sort Example

An example of merge sort. First divide the list into the smallest unit (1 element), then compare each element with the adjacent list to sort and merge the two adjacent lists. Finally all the elements are sorted and merged. (Via Wikipedia)

Continue reading Implementing Merge Sort Algorithm in Java

Implementing MinMax Algorithm in Java

Finding the minimum and maximum elements in a dataset is a common problem in computer science. While you can solve this using simple iteration, using a divide-and-conquer approach is more efficient in certain scenarios, especially in recursive algorithms. In this blog post, we’ll explore a recursive Java implementation of the Min-Max algorithm.

What is the Min-Max Algorithm?

The Min-Max algorithm splits the array into smaller parts, recursively determines the minimum and maximum of each part, and then combines the results. This divide-and-conquer approach reduces the number of comparisons compared to a naive linear method.

Why Use Divide-and-Conquer?

  • It minimizes the number of comparisons.
  • More efficient for large datasets.
  • Demonstrates recursion and good problem-solving strategies.
Continue reading Implementing MinMax Algorithm in Java

Implementing Binary Search in Java

Searching for elements in large datasets is a common task in computer programming, and doing it efficiently can greatly improve performance. One such efficient method is binary search—an algorithm designed specifically for sorted data. In this blog post, we’ll walk through how binary search works conceptually and then explore a working Java program that performs binary search on a user-provided set of numbers.

What is Binary Search?

Binary search is a divide-and-conquer algorithm used to find the position of a target value within a sorted array. Rather than checking each element one by one like a linear search, binary search repeatedly divides the array into two halves and compares the target with the middle element.

How Binary Search Works:

  1. Start with the middle element of the array.
  2. If the middle element is the target, return its index.
  3. If the target is smaller, repeat the search on the left half.
  4. If the target is greater, repeat the search on the right half.
  5. Continue this process until the element is found or the sub-array size becomes zero.

Because it halves the number of elements to be checked each time, binary search has a time complexity of O(log n), making it much faster than linear search for large datasets.

Continue reading Implementing Binary Search in Java