Prim’s algorithm is a greedy algorithm that finds a minimum spanning tree for a connected weighted undirected graph.

Continue reading Constructing The Minimum Spanning Tree for a Graph using Prim’s Algorithm# Tag Archives: Java

# Implementing Greedy Knapsack Algorithm in Java: Alternate Way

# Implementing Greedy Knapsack Algorithm in Java

According to Wikipedia,

Continue reading Implementing Greedy Knapsack Algorithm in JavaThe knapsack problem or rucksack problem is a problem in combinatorial optimization: Given a set of items, each with a mass and a value, determine the number of each item to include in a collection so that the total weight is less than or equal to a given limit and the total value is as large as possible.

# Implementing Quicksort Algorithm in Java

Quicksort is a divide and conquer algorithm. Here is an another way to implement this algorithm in Java.

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:

- Pick an element, called a pivot, from the array.
- 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.
- 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.

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