How many sorting techniques are there in java




















Sorting is the operation performed to arrange the records of a table or list in some order according to some specific ordering criterion.

Sorting is performed according to some key value of each record. The records are either sorted either numerically or alphanumerically. The records are then arranged in ascending or descending order depending on the numerical value of the key. Here is an example, where the sorting of a lists of marks obtained by a student in any particular subject of a class. Internal Sorting : If all the data that is to be sorted can be adjusted at a time in the main memory, the internal sorting method is being performed.

External Sorting : When the data that is to be sorted cannot be accommodated in the memory at the same time and some has to be kept in auxiliary memory such as hard disk, floppy disk, magnetic tapes etc, then external sorting methods are performed.

Comb Sort is the advanced form of Bubble Sort. Bubble Sort compares all the adjacent values while comb sort removes all the turtle values or small values near the end of the list. It is a sorting technique based on the keys i. Counting sort calculates the number of occurrence of objects and stores its key values. New array is formed by adding previous key elements and assigning to objects. In the heap sort, Min heap or max heap is maintained from the array elements deending upon the choice and the elements are sorted by deleting the root element of the heap.

As the name suggests, insertion sort inserts each element of the array to its proper place. It is a very simple sort method which is used to arrange the deck of cards while playing bridge. Merge sort follows divide and conquer approach in which, the list is first divided into the sets of equal elements and then each half of the list is sorted by using merge sort. The sorted list is combined again to form an elementary sorted array.

Quick sort is the most optimized sort algorithms which performs sorting in O n log n comparisons. Like Merge sort, quick sort also work by using divide and conquer approach. In Radix sort, the sorting is done as we do sort the names according to their alphabetical order. It is the lenear sorting algorithm used for Inegers. Selection sort finds the smallest element in the array and place it on the first place on the list, then it finds the second smallest element in the array and place it on the second place.

This process continues until all the elements are moved to their correct ordering. In the following example, we have initialized an array of integer type and sort the array in ascending order.

In the following example, we have defined a method named sortArray that contains the logic to sort an array in natural order. The descending order arranges the elements in the highest to lowest order.

Java Collections class provides the reverseOrder method to sort the array in reverse-lexicographic order. It is a static method, so we can invoke it directly by using the class name. It does not parse any parameter. It returns a comparator that imposes the reverse of the natural ordering ascending order. It means that the array sorts elements in the ascending order by using the sort method, after that the reverseOrder method reverses the natural ordering, and we get the sorted array in descending order.

Suppose, a[] is an array to be sort in the descending order. We will use the reverseOrder method in the following way:.

In the following program, a point to be noticed that we have defined an array as Integer. Because the reverseOrder method does not work for the primitive data type. In the following example, we have initialized an integer array and perform sorting in descending order. An array derived from the array is known as subarray. Suppose, a[] is an array having the elements [12, 90, 34, 2, 45, 3, 22, 18, 5, 78] and we want to sort array elements from 34 to It will sort the subarray [34, 2, 45, 3, 22, 18] and keep the other elements as it is.

To sort the subarray, the Arrays class provides the static method named sort. It sorts the specified range of the array into ascending order. We can also sort the array of type long, double, float, char, byte, etc. If formIndex is equal to the toIndex, the range to be sorted is empty. JavaTpoint offers too many high quality services. Mail us on [email protected] , to get more information about given services.

Please mail your requirement at [email protected] Duration: 1 week to 2 week. The core function works pretty much as laid out in the explanation. We're just passing indexes left and right which are indexes of the left-most and right-most element of the subarray we want to sort. Initially, these should be 0 and array. The base of our recursion ensures we exit when we've finished, or when right and left meet each other. We find a midpoint mid , and sort subarrays left and right of it recursively, ultimately merging our solutions.

If you remember our tree graphic, you might wonder why don't we create two new smaller arrays and pass them on instead. This is because on really long arrays, this would cause huge memory consumption for something that's essentially unnecessary. Merge Sort already doesn't work in-place because of the merge step, and this would only serve to worsen its memory efficiency.

The logic of our tree of recursion otherwise stays the same, though, we just have to follow the indexes we're using:. To merge the sorted subarrays into one, we'll need to calculate the length of each and make temporary arrays to copy them into, so we could freely change our main array. After copying, we go through the resulting array and assign it the current minimum. Because our subarrays are sorted, we just chose the lesser of the two elements that haven't been chosen so far, and move the iterator for that subarray forward:.

If we want to derive the complexity of recursive algorithms, we're going to have to get a little bit mathy. The Master Theorem is used to figure out time complexity of recursive algorithms. For non-recursive algorithms, we could usually write the precise time complexity as some sort of an equation, and then we use Big-O Notation to sort them into classes of similarly-behaving algorithms.

The equation itself is recursive! In this equation, a tells us how many times we call the recursion, and b tells us into how many parts our problem is divided. In this case that may seem like an unimportant distinction because they're equal for mergesort, but for some problems they may not be. The rest of the equation is complexity of merging all of those solutions into one at the end.

The Master Theorem solves this equation for us:. If T n is runtime of the algorithm when sorting an array of the length n , Merge Sort would run twice for arrays that are half the length of the original array. This means the equation for Merge Sort would look as follows:.

Check out our hands-on, practical guide to learning Git, with best-practices, industry-accepted standards, and included cheat sheet. Stop Googling Git commands and actually learn it! That means our complexity is O nlog n. This is an extremely good time complexity for a sorting algorithm, since it has been proven that an array can't be sorted any faster than O nlog n.

While the version we've showcased is memory-consuming, there are more complex versions of Merge Sort that take up only O 1 space. In addition, the algorithm is extremely easy to parallelize, since recursive calls from one node can be run completely independently from separate branches. While we won't be getting into how and why, as it's beyond the scope of this article, it's worth to keep in mind the pros of using this particular algorithm.

To properly understand why Heapsort works, you must first understand the structure it's based on - the heap. We'll be talking in terms of a binary heap specifically, but you can generalize most of this to other heap structures as well. A heap is a tree that satisfies the heap property, which is that for each node, all of its children are in a given relation to it.



0コメント

  • 1000 / 1000