] for(J=i-increment; j>0 l[0]increment) {L[j+increment] = L[j];//record and move back to find the insertion position} l[j+increment] = l[0];//Insert } } } while(Increment >1); } Public Static voidSortint[] list) { //Prelude Work int[] L =New int[list.length+1]; l[0] =0;//let the No. 0 element be the location of the staging data for(intI=1; i) {L[i]= list[i-1]; } shellso
backwards in the sorted sequence of elements.If the element (sorted) is greater than the new element, move the element to the next position.Repeat step 3 until you find the position where the sorted element is less than or equal to the new element.Inserts a new element after the location.Repeat steps 2~5.
Copy Code code as follows:
public static void Insertionsort (int[] data) {
for (int index
Reference: 151 recommendations for improving Java code-73 recommendations: sorting using CompatatorIn Java, there are two ways to sort the data:
Implementing the Comparable interface
Implementing the Comparator interface
In the JDK class library, a subset of the classes implement the comparable interf
of arr[i], the condition in the inner loop is j>i, because Arr[i] is lined up in front of the order. Each time you compare from backward to arr[i] you don't have to go on.Of course, the core code above can also be written in the following form:1Private Static voidBubbleSortTest2 (intarr[]) { 2inttemp = 0; 3 for(inti = arr.length-1; i > 0; i++) { 4 for(intj = 0; J ) { 5if(Arr[j] ]) { 6 temp = arr[j+1]; 7 Arr[j+1] =Arr[j];8 Arr[j] =temp;9 }
unaffected. These two methods accept parameters that are almost identical, and they all accept a key parameter, which is used to specify which part of the object to sort by:
Data_list = [(0, +), (+ +), (+)] Data_list.sort (Key=lambda x:x[1]) # We want to sort based on the second number of list items print (data_list) G t;>> [(77, 34), (55, 97), (0, 100)]
Another frequently used parameter is reverse, which
not allow the value of key to be null. is not synchronized.
Hashtable: Similar to HashMap, the difference is that the value of key and value is not allowed to be null; it supports thread synchronization, which means that only one thread can write Hashtable at any one time, and it also causes Hashtale to be slower when writing.
Linkedhashmap: Save the Insertion Order of the records, and when traversing linkedhashmap with iterator, the first record m
invocation). At the bottom of the recursive scenario, the size of the sequence is 0 or one, which is always sorted. Although it is always recursive, the algorithm always exits, because in each iteration (iteration), it will at least put an element to its last Position. Quick Sort detailed plots: "sitting on the toilet" algorithm 3: the most commonly used sort--quick so
Bubble Sort Algorithm (JAVA)
I. Overview
The day before yesterday, someone asked the next sorting algorithm implementation, so draw time to write a bubble sorting algorithm. The bubble sort algorithm works by starting with the first number, then comparing the current number with each subsequent number, and swapping the current two digits if the current number is
Insert Sort algorithmSimple insertion Sorting algorithm principle: Select an element from the entire backlog to be inserted into an ordered subsequence, to get an ordered, element plus one subsequence, until the entire sequence of elements to be inserted is 0, then the entire sequence is all ordered.In the actual algorithm, we often select the first element of the sequence as an ordered sequence (because an
elements, and then place it at the end of the sorted sequence.
C, repeat the second step until all the elements are sorted.
2. Dynamic Diagram Demonstration
3. JavaScript Code Implementation
12345678910111213141516
functionselectionSort(arr){ varlen=arr.length; varminIndex,temp; for(vari=0;i minIndex=i; for(varj=i+1;j if(arr[j]//寻找最小的数 minIndex=j;//将最小数的索引保存 } } temp=arr[i]; arr[i]=arr[minIndex]; arr[minIndex]=temp; }
First, the basic conceptThe basic operation of inserting a sort is to insert a data into the ordered data that is already sorted, so as to get a new sequential data with a number plus one, the algorithm is suitable for the ordering of a small amount of data, and the time complexity is O (n^2). is a stable sorting method. The insertion algorithm divides the array to be sorted into two parts: the first part c
multicore when used. But when it is not possible to use the increase in workload, it may not be as efficient as a single thread. Interested friends can use the code address below to find running all the source of their own run to try to run.Five, the code address of this articleIncluding this post, all code is stored in the following uniform address:Https://gith
}, number of comparisons: 4;after the third merge: {1,6,8,38,100,202,301}, number of comparisons: 4;the total number of comparisons is: 3+4+4=11,;the number of reverse order is;2. Merge sorted Java code implementationPackage com.yonyou.test;/** * Merge sort with internal sorting algorithm * default to order from small to large * @author Small Hao * @ Created date
Efficiency: O (N*LOGN)Package Sort;import Utils. util;/** * Hill Sort * In the interval of H, compare. According to a certain formula, we first find the maximum interval h * When the H value is large, the number of elements that need to be moved is less, but the distance traveled long. Interior with an h step element for insertion
Insert Sort
The insertion sort is implemented in this way:
First, create an empty list to hold sorted ordered sequences (which we call "ordered lists").
Remove a number from the original series and insert it into an ordered list so that it remains in an orderly state.
Repeat step 2nd until the original sequence is empty.
The average time complexity of the
) into groups . the * The difference in subscript between the records in each group D. Direct insertion of all elements in each group the * Then use a smaller increment (D/2) to group it, followed by a direct insert sort in each group. the * When the increment is reduced to 1 o'clock, the sort is completed after the direct insert
1 //: Sorteddirlist.java2 ///This program can list a directory. 3 4 PackageC10;5 6 ImportJava.io.File;7 ImportJava.io.FilenameFilter;8 Importjava.util.Arrays;9 ImportJava.util.Comparator;Ten One /** A * @time: Morning 11:52:04 - * @date: April 29, 2017 - * @auther: Skyfffire the * @version: v0.1 - * - * Sortable read-in file directory, value sort=-1 to descending order - * Value sort=1 for traditional
Problem Description:The input is a real number that is evenly distributed on the [0, 1) interval generated by a random process. Divide the interval [0, 1) into n equal-sized sub-intervals (barrels), 1/n:[0 per barrel size, 1/n), [1/n, 2/n], [2/n, 3/n), ..., [k/n, (k+1)/n), ... Assign n input elements to these buckets, sort the elements in the bucket, and note that the ordering of the elements in each bucket can actually select many algorithms, and the
;"> // if the object is less than, equal to, or greater than the specified object o, negative integers, zero, or positive integers are returned respectively. Int compareTo (Object o );
Sample Code:
Import java. util. *; class Student implements Comparable {// note the difference between private String stuNum; private int score; public Student (String stuNum, int score) {this. stuNum = stuNum; this. sc
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.