Browsed by
Category: Algorithm

How to implement Merge Sort in java

How to implement Merge Sort in java

Merge sort is a divide and conquer algorithm which divide original data into smaller sets of data to solve the problem. In merge sort elements in the collection are divided into two part from middle which produce two collections into left and right parts. The resulting collections are again recursively splitted until they are broke into single element in the collection.

After split process, elements in each collection merge into third collection to produce sorted collection. To merge each elements, it picks the object which is smaller and inserts into new collection. For this collection it now selects the next elements and selects the smaller element from both collection by comparing one element from each collection at a time. This process is recursively done for all collections which were created during split process to produce sorted collection.

Read More Read More

How to implement Bubble Sort in Java

How to implement Bubble Sort in Java

Bubble Sort

Bubble sort also termed as sinking sort, is simple sorting algorithm which sort the list of data in ascending order by repeatedly stepping into it. In this algorithm, two adjacent element from list is compared and swapped if preceding element is greater than its next element. This will proceed throughout the list until all the elements are arranged into ascending order.

Read More Read More

How to implement Selection Sort in java

How to implement Selection Sort in java

Selection sort is a sorting algorithm, specifically an in-place comparison sort. It is a combination of searching and sorting. During each pass, the unsorted smallest (or largest) value is moved to its proper position in the array. In the selection sort, the inner loop finds the next smallest (or largest) value and the outer loop places that value into its proper location. The total number of iterations are one less than total number of elements in the array.

Read More Read More

How to implement Insertion Sort in java

How to implement Insertion Sort in java

Insertion sort is a simple sorting algorithm that builds the final sorted array (or list) one item at a time. It is much less efficient on large lists than more advanced algorithms such as quicksort, heapsort, or mergesort. However there are some advantages of insertion sort.

  1. It is very efficient for small data sets.
  2. It does not change the relative order of elements with equal keys.
  3. Requires a constant amount O(1) of additional memory space.

Read More Read More