The shell sort of Java common sorting algorithm

Source: Internet
Author: User

In the process of learning the algorithm, we will inevitably touch many and sort-related algorithms. All in all, the basic sorting algorithm must be mastered for any programmer.

Starting today, we're going to be explaining the basic sorting algorithm. Is it ready? Let ' s go~~~

1, the basic concept of sorting algorithm to explain

Time complexity: The number of times to compare the number of keywords that need to be sorted and the corresponding moves.

Spatial complexity: Analyze how much secondary memory is needed.

Stability: If you record A and B of two keywords whose values are equal, and if they are not exchanged in their relative positions after sorting, then we call this sort algorithm stable.

Otherwise we call this sort algorithm to be unstable.

Common classifications for sorting algorithms:

1, internal sorting (the most common sort of method, do not need to use third-party secondary storage tools)

2, external sorting (need to use external storage to assist with related sorting operations)

If the data element that participates in the sort is very much, the amount of data is very large, the computer cannot put the whole sort process into memory,

We have to use external memory such as disk to do, this sort of way, we call it external sort.

The most common of the external sort is the multi-way merge sort, the original file is decomposed into several parts that can be loaded into memory one at a time, each part is transferred into

The memory is sorted accordingly, and then the multiple sequential external files are sorted in a multi-merge order.

For most of us programmers, we often run into internal sorting. Next, we will explain the common internal ordering accordingly.

The internal ordering to be explained today is:

Shell Sort

A brief explanation of the basic concepts of 1.Shell sequencing

2.Shell sorted Java Code implementation

Package com.yonyou.test;/** * Internal sorting algorithm Shell sort * By default in order from small to large * @author Small Hao * @ Creation date 2015-3-27 */public class Test{public St atic void Main (string[] args) {//array required to be sorted int[] array=new int[]{8,3,2,1,7,4,6,5};//Output the contents of the original array Printresult (array);//sh Ell sort operation Shellsort (array);//output sorted by the relevant result Printresult (array);} /** * Shell Sorting algorithm * Incremental h= (h*3) +1; This increment formula is given by Knuth * If you do not know well, please Baidu. * @param array */private static void Shellsort (int[] array) {///first determine the maximum increment based on the length of the array I   NT H=1;   Press H * 3 + 1 to get the maximum value of the increment sequence while (H <= array.length/3) {h = h * 3 + 1;} Incremental find and sort while (h>0) {for (int i=h;i<array.length;i++) {for (int k=i;k<array.length;k+=h) {//   Determine if a reorder is required, if it is less than the value at K-h, the IF (Array[k]<array[k-h]) {int tempvalue=array[k] will need to be reordered;   int j=k;    for (; j>=i&&tempvalue<array[j-h];j-=h) {array[j]=array[j-h];   } Array[j]=tempvalue;   }} printresult (array);   } h= (H-1)/3; }}/** * * Output the result of the corresponding array* @param array */private static void Printresult (int[] array) {for (int value:array) System.out.print ("" +valu      E+ ""); System.out.println ();} /** * Swap the values of two variables in the array * @param array * @param i * @param j */private static void Swap (int[] Array,int i,int j) {int temp=array[ I];array[i]=array[j];array[j]=temp;}}

  

The shell sort of Java common sorting algorithm

Related Article

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.