All of them come from the. Collections class, and all take the form of static methods whose first argument is the collection on which the operation is to be performed. The great majority of the algorithms provided by the Java platform operate on. List instances, but a few of them operate on arbitrary.
Collection instances. This section briefly describes the following algorithms: Sorting. See Program 8.4 (or Java system sort) 16. Program: Write a program for Insertion Sort in java. Write a program to sort a map by value. Write a program to find common elements between two arrays. How to swap two numbers without using temporary variable? The sort algorithm reorders a List so that its elements are in ascending order according to an ordering relationship. Two forms of the operation are provided. The simple form takes a List and sorts it according to its elements' natural ordering. If you're unfamiliar with the concept of natural ordering, read the. Object Ordering section. The sort operation uses a slightly optimized merge sort algorithm that is fast and stable: Fast: It is guaranteed to run in n log(n) time and runs substantially faster on nearly sorted lists. Empirical tests showed it to be as fast as a highly optimized quicksort. A quicksort is generally considered to be faster than a merge sort but isn't stable and doesn't guarantee n log(n) performance. Stable: It doesn't reorder equal elements. This is important if you sort the same list repeatedly on different attributes. If a user of a mail program sorts the inbox by mailing date and then sorts it by sender, the user naturally expects that the now- contiguous list of messages from a given sender will (still) be sorted by mailing date. This is guaranteed only if the second sort was stable. The following. trivial program prints out its arguments in lexicographic (alphabetical) order. Suppose you want to print out the anagram groups from our earlier example in reverse order of size — largest anagram group first. The example that follows shows you how to achieve this with the help of the second form of the sort method. Recall that the anagram groups are stored as values in a Map, in the form of List instances. The revised printing code iterates through the Map's values view, putting every List that passes the minimum- size test into a List of Lists. Then the code sorts this List, using a Comparator that expects List instances, and implements reverse size- ordering. Finally, the code iterates through the sorted List, printing its elements (the anagram groups). The following code replaces the printing code at the end of the main method in the Anagrams example. That is, this algorithm reorders the List based on input from a source of randomness such that all possible permutations occur with equal likelihood, assuming a fair source of randomness. This algorithm is useful in implementing games of chance. For example, it could be used to shuffle a List of Card objects representing a deck. Also, it's useful for generating test cases. This operation has two forms: one takes a List and uses a default source of randomness, and the other requires the caller to provide a. Random object to use as a source of randomness. The code for this algorithm is used as an example in the. List section. This operation is useful for reinitializing a List. List and a source List, and copies the elements of the source into the destination, overwriting its contents. The destination List must be at least as long as the source. If it is longer, the remaining elements in the destination List are unaffected. List. add. All — adds all the specified elements to a Collection. The elements to be added may be specified individually or as an array. Searching. The binary. Search algorithm searches for a specified element in a sorted List. This algorithm has two forms. The first takes a List and an element to search for (the . This form assumes that the List is sorted in ascending order according to the natural ordering of its elements. The second form takes a Comparator in addition to the List and the search key, and assumes that the List is sorted into ascending order according to the specified Comparator. The sort algorithm can be used to sort the List prior to calling binary. Search. The return value is the same for both forms. If the List contains the search key, its index is returned. If not, the return value is (- (insertion point) - 1), where the insertion point is the point at which the value would be inserted into the List, or the index of the first element greater than the value or list. List are less than the specified value. This admittedly ugly formula guarantees that the return value will be > = 0 if and only if the search key is found. It's basically a hack to combine a boolean (found) and an integer (index) into a single int return value. The following idiom, usable with both forms of the binary. Search operation, looks for the specified search key and inserts it at the appropriate position if it's not already present. Both of these operations come in two forms. The simple form takes only a Collection and returns the minimum (or maximum) element according to the elements' natural ordering. The second form takes a Comparator in addition to the Collection and returns the minimum (or maximum) element according to the specified Comparator.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |