Posts

Showing posts from October, 2018

Sorting Algorithms Summary

What is in-place sorting? An in-place sorting algorithm uses constant extra space for producing the output (modifies the given array only). It sorts the list only by modifying the order of the elements within the list. For example, Insertion Sort and Selection Sorts are in-place sorting algorithms as they do not use any additional space for sorting the list and a typical implementation of Merge Sort is not in-place, also the implementation for counting sort is not in-place sorting algorithm. What are Internal and External Sortings? When all data that needs to be sorted cannot be placed in-memory at a time, the sorting is called  external sorting . External Sorting is used for massive amount of data. Merge Sort and its variations are typically used for external sorting. Some extrenal storage like hard-disk, CD, etc is used for external storage. When all data is placed in-memory, then sorting is called internal sorting.

Quick Sort vs Merge Sort

Image
Quick sort  is an internal algorithm which is based on divide and conquer strategy. In this: The array of elements is divided into parts repeatedly until it is not possible to divide it further. It is also known as  “partition exchange sort” . It uses a key element (pivot) for partitioning the elements. One left partition contains all those elements that are smaller than the pivot and one right partition contains all those elements which are greater than the key element. Merge sort  is an external algorithm and based on divide and conquer strategy. In this: The elements are split into two sub-arrays (n/2) again and again until only one element is left. Merge sort uses additional storage for sorting the auxiliary array. Merge sort uses three arrays where two are used for storing each half, and the third external one is used to store the final sorted list by merging other two and each array is then sorted recursively. At last, the all sub arrays are merged ...

Time Complexities of all Sorting Algorithms

Following is a quick revision sheet that you may refer at last minute Algorithm Time Complexity    Best Average Worst Selection Sort Ω(n^2) θ(n^2) O(n^2) Bubble Sort Ω(n) θ(n^2) O(n^2) Insertion Sort Ω(n) θ(n^2) O(n^2) Heap Sort Ω(n log(n)) θ(n log(n)) O(n log(n)) Quick Sort Ω(n log(n)) θ(n log(n)) O(n^2) Merge Sort Ω(n log(n)) θ(n log(n)) O(n log(n)) Bucket Sort Ω(n+k) θ(n+k) O(n^2) Radix Sort Ω(nk) θ(nk) O(nk) Cheat Sheet 

Stability in sorting algorithms

Image
A sorting algorithm is said to be stable if two objects with equal keys appear in the same order in sorted output as they appear in the input array to be sorted. Formally stability may be defined as, Let   be an array, and let   be a strict weak ordering on the elements of  . A sorting algorithm is stable if- where   is the sorting permutation ( sorting moves   to position   ) Informally, stability means that equivalent elements retain their relative positions, after sorting. Do we care for simple arrays like array of integers? When equal elements are indistinguishable, such as with integers, or more generally, any data where the entire element is the key, stability is not an issue. Stability is also not an issue if all keys are different. An example where it is useful Consider the following dataset of Student Names and their respective class sections. If we sort this data according to name only, then it is high...

Merge Sort

Image
**  * Merge sort is a sorting technique based on divide and conquer technique. With  * worst-case time complexity being O(n log n), it is one of the most respected  * algorithms.  *  * Merge sort first divides the array into equal halves and then combines them  * in a sorted manner. Compared to insertion sort [O(n*2) worst-case time],  * merge sort is faster. Trading a factor of n for a factor of lg n is a good  * deal. On small inputs, insertion sort may be faster. But for large enough  * inputs, merge sort will always be faster, because its running time grows more  * slowly than insertion sorts.  *  * Stable: If 2 keys are same in input list then in sorted list also their order is same.  *  * O(n) extra space for arrays (as shown)  *  * O(lg(n)) extra space for linked lists  *  * O(nlg(n)) time  *  * Not adaptive  *  * Does not require random access to d...

Exception Handling in Spring

Image
Spring Exception Handling Having a well defined exception handling approach is a huge plus point for any web application framework, that being said Spring MVC framework delivers well when it comes to exception and error handling in our web applications. Spring MVC Framework provides following ways to help us achieving robust exception handling. Controller Based  – We can define exception handler methods in our controller classes. All we need is to annotate these methods with  @ExceptionHandler  annotation. This annotation takes Exception class as argument. So if we have defined one of these for Exception class, then all the exceptions thrown by our request handler method will have handled. These exception handler methods are just like other request handler methods and we can build error response and respond with different error page. We can also send JSON error response, that we will look later on in our example. If there are multiple exception handle...