1 PackageInsert.sort;2 3 ImportJava.util.Scanner;4 5 /*also known as narrowing the incremental sort, essentially inserting sort, dividing the sequence increment into several sub-sequences, making a direct insertion order for each subsequence6 * Increments of 5 o'clock, take 1, 6, 11, 16 ... for a group, 2, 7, 12, 17 ... for a group, and so on, the direct
O (n ^ 2), which is better than the Bubble Method and the selected sorting method */void InsertionSort (int arr [], int num) {int temp; int I, j; for (I = 1; I
Iii. Simple selection and sorting
Through the comparison between the n-I keywords, select the record with the minimum keyword from the n-I + 1 record and the I (1 Although the same as the Bubble Sorting is O (n ^ 2), the performance of simple selection sorting is slightly better than that of Bubble sorting.
The
data Structure-Hill sort (Shell's sort) specific explanation and code (c + +)This address: http://blog.csdn.net/caroline_wendy/article/details/24363981Hill Sort (Shell's sort), also known as "narrowing incremental sort" (diminishi
to the step 8/2=4, respectively:[1,18], [24,27], [12,17], [15,19].The final result is the direct insertion Order of each group:
1, 24, 12, 15, 18, 27, 17, 19
2. Continue to reduce the step size, in the previous step of the results Series 1, 24, 12, 15, 18, 27, 17, 19, based on the step to the previous step length divided by 2, that is, step to 4/2=2 repeat the first step as shown in the following figure.
The results are divided into two groups based
Sort Algorithm Analysis [5]: Merge and sort (with Python C ++ code), and algorithm analysis with python
Merge and sort: combines two sorted sequences into one.Algorithm principle
First look at the dynamic diagram:
The algorithm is described as follows:
Algorithm Implementation Python version:
#-*-Encoding: UTF-8-*-d
sequence as the largest number to begin the sorting process, which is a 364-increment sort. Then, each time the outer loop of the sort routine is completed, the interval is reduced with the backward-push of the formula provided earlier:h = (h-3)/3It is shown in the third column of Table 7.1. This inverted formula generates an inverse sequence of 364, 121, 40, 13, 4, 1 starting with 364, sorted by each numb
(space here does not consider saved stack-bottom pointers, local variables, etc.).Here is the implementation://Insert sort (first partial order gradually expands ordered array to all order) stable/*Pseudo code Insertion-sort (A) 1 for j=2 to a.length 2 key = A[j] 3 i=j-1 4 while (I>0 and A[i]>key) 5 A[i+1]=a[i] 6 i=i-
1 、-------MessageFor beginners of Java students, the sort is in a strange but a basic problem. But because I have just stepped into the world of learning Java, a lot of things can not be well understood, resulting in their own can be knocked out of code or roughly to understand,But the idea of solving the problem is no
}} 3. Quick Sort provides method invocation/** * Quick sort @param numbers with sorted array */ void Quick (int// view array is empty {quickSort (numbers, 0, numbers.length-1);}} Analysis:Fast sorting is generally considered to be the best average performance in a sorting method of the same order of magnitude (O (nlog2n)) . However, if the initial sequence is ordered or basically
so on at the end of the first round, the largest data is already in the last item. In a round of comparisons, there are more and more data items in the back row, and fewer items need to be sorted until they are zero.Select sort: The bubble sort is optimized to reduce the number of exchanges, in the first round the selection of the smallest number is placed in the initial, after one round the first item is
Hill SortPackage Shellsort;import utils.sortutils; Public classDemo { Public Static void Shellsort(int[] a) {intTemp,j;intincrement = A.length; Do{increment = increment/3+1;//If it is a direct insert sort, the increment here are 1. //system.out.println (increment); for(inti = increment; i if(A[i]//Staged in temp for(J=i-increment; j>=0 a[j]>temp; j-=increment) {A[j+increment] = a[j];//record and move back to
Java sort Merge sort 1. Brief introductionThe algorithm of merging sorting is to combine multiple ordered data tables into an ordered data table. If there are only two ordered tables participating in the merge, it becomes a two-way merge. For a primitive column to be sorted, it can often be attributed to a multi-path merge so
Java Collections. sort () implements the default List sorting methods and custom methods [convert], collections. sort sorting
1. default list sorting method provided by java
Main Code:
List
List. add ("Wang Shuo ");List. add ("Li Ming ");List. add ("Liu Di ");List. add ("Li
1 PackageCom.array;2 3 Public classSort_insert {4 /*5 * Project name: Insert sort;6 * Project requirements: Using the Java array to sort, and using the insertion sorting algorithm;7 * SEVCK;8 */9 Public voidSortintarr[]) {Ten for(inti = 1; i ) { One intInsertval =Arr[i]; A
, the second largest number in the whole series) in the penultimate position. So go on, repeat the process until you finish sorting.
Third, the realization of ideas:
With the double loop, the outer loop variable is set to I and the inner loop variable is set to J. If there are n number needs to be sorted, the outer loop repeats n-1 times, and the inner loop repeats n-1,n-2,...,1 times in turn. Each of the two elements to be compared is related to inner Loop j, which can be identified by a[j] a
position of each row of data in an ordered sequence, and then put each item in the specified position in turn.
Bucket sort has a large space overhead, it requires two arrays, and the 1th buckets array is used to record the number of elements in each bucket, saving the positions of the elements in an ordered sequence, and the 2nd array to cache the data to be sorted.
The bucket sort is stable.
If the range
element back. The comparison is an adjacent two element comparison, and the interchange also occurs between these two elements. So, if the two elements are equal, I think you will not be bored to exchange them again, if the two equal elements are not adjacent, then even through the preceding 22 exchange two adjacent together, this time will not be exchanged, so the same elements of the order has not changed, so bubble sort is a stable sorting algorit
Examples of four basic sorting methods in JAVA and four sort instances in java
This article describes four basic sorting methods in JAVA. Share it with you for your reference. The details are as follows:
JAVA includes four basic sorting methods: Bubble, insert, choice, and S
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.