Eight kinds of sorting algorithms for Java----

Source: Internet
Author: User

Eight kinds of sorting algorithms for Java----

Read Catalogue

    • 1. Direct Insert Sort
    • 2. Hill sort
    • 3. Simple selection of sorting
    • 4. Heap Sequencing
    • 5. Bubble sort
    • 6. Quick Sort
    • 7. Merge sort
    • 8. Base sorting
Back to Table of Contents 1. Direct Insert Sort

This sort of problem is often encountered: inserting new data into rows that have already been sorted.

Sorts the first and second numbers and then forms an ordered sequence

The third number is inserted to form a new ordered sequence.

The fourth number, the fifth number ... Until the last number, repeat the second step.

How to Write code:

First set the number of inserts, that is, the number of cycles, for (int i=1;i<length;i++), 1 number of the time without inserting.

Sets the number of inserts and the number of digits that are the last number of the sequence that has been ranked. Insertnum and J=i-1.

Loops forward from the last number, and moves the current number one bit backwards if the number of insertions is less than the current number.

Places the current number in an empty position, that is, j+1.

The code is implemented as follows:

Back to Catalog 2. Hill sort

For a direct insert sorting problem, the amount of data is huge.

Set the number of numbers to N, take odd k=n/2, and divide the book of the subscript difference to K into a group, forming an ordered sequence.

After taking K=K/2, the book dividing the subscript difference to K is divided into a group, which forms an ordered sequence.

Repeat the second step until K=1 performs a simple insert sort.

How to Write code:

First determine the number of groups to divide.

The elements in the group are then inserted into a sort order.

Then LENGTH/2, repeat 1, 2 steps until length=0.

The code is implemented as follows:

Back to Table of Contents 3. Simple selection sorting

Often used to fetch the largest and smallest number of numbers in a sequence.

(if each comparison is exchanged, then it is an exchange sort; If you compare one cycle to another, it is simple to select the sort.) )

Traverse the entire sequence, placing the smallest number at the front.

Traverse the remainder of the sequence, placing the smallest number at the front.

Repeat the second step until there is only one number left.

How to Write code:

First determine the number of loops, and remember the current number and current position.

Compares all the numbers after the current position to the current number, assigns a decimal value to key, and remembers the position of the decimal.

When the comparison is complete, the minimum value is exchanged with the value of the first number.

Repeat steps 2, 3.

The code is implemented as follows:

Back to catalog 4. Heap Sort

The optimization of simple selection sorting.

Build the sequence into a large top heap.

Swaps the root node with the last node, and then disconnects the last node.

Repeat the first to second step until all the nodes are disconnected.

The code is implemented as follows:

public void Heapsort (int[] a) {System.out.println ("start sort");       int arraylength=a.length;           Loop build heap for (int i=0;i<arraylength-1;i++) {//Jian Yu buildmaxheap (a,arraylength-1-i);           Swap heap top and last element swap (a,0,arraylength-1-i);       System.out.println (Arrays.tostring (a));       }} private void Swap (int[] data, int i, int j) {//TODO auto-generated method stub int tmp=data[i];       DATA[I]=DATA[J];   data[j]=tmp; }//to the data array from 0 to LastIndex build a large top heap private void Buildmaxheap (int[] data, int lastIndex) {//TODO auto-generated Meth             OD stub//starting from the parent node of the LastIndex node (the last node) for (int i= (lastIndex-1)/2;i>=0;i--) {//k Saving the node being judged           int k=i;                If the child node of the current K-node exists while (K*2+1<=lastindex) {//k The index of the left child node of the node int biggerindex=2*k+1; If Biggerindex is less than lastindex, that is, the right child node of the K node represented by biggerindex+1 exists if (biggerindex<lastIndex) {//If the value of the right child node is greater if (data[biggerindex]<data[biggerindex+1]) {                   Biggerindex always records the index biggerindex++ of the larger child nodes; }}//If the value of the K-node is less than the value of its larger child node if (Data[k]<data[biggerindex]) {/                   /swap them swap (DATA,K,BIGGERINDEX);               The Biggerindex is given a K, starting the next loop of the while loop, and re-guaranteeing that the value of the K node is greater than the value of its left and right child nodes k=biggerindex;               }else{break; }           }       }   }

Back to catalog 5. Bubble sort

Generally not.

Compares all elements in a sequence to 22, placing the largest in the last face.

Compares all elements in the remaining series to 22, placing the largest in the last face.

Repeat the second step until there is only one number left.

How to Write code:

Sets the number of cycles.

Sets the number of bits to start comparing, and the number of bits to end.

22 Compare, put the smallest to the front.

Repeat steps 2 and 3 until the number of loops is complete.

The code is implemented as follows:

Back to Table of contents 6. Quick Sort

Ask for the fastest time.

Select the first number is p, the number less than P is on the left, and the number greater than P is on the right.

Recursively, the left and right numbers of P follow the first step until they are not recursive.

The code is implemented as follows:

Back to Table of contents 7. Merge sort

The speed is second only to the fast row, when the memory is low, it can be used in parallel computation.

Select two adjacent numbers to form an ordered sequence.

Select two contiguous sequential sequences to form an ordered sequence.

Repeat the second step until the whole sequence is formed.

The code is implemented as follows:

 public static void MergeSort (int[] numbers, int left, int. right) {int t = 1;//number of elements per group int size = Right-lef     T + 1;         while (T < size) {int s = t;//This loop is the number of elements per group T = 2 * s;         int i = left;             while (i + (t-1) < size) {merge (numbers, I, I + (s-1), i + (t-1));         i + = t;     } if (i + (s-1) < right) merge (numbers, I, I + (s-1), right);     }} private static void merge (int[] data, int p, int q, int r) {int[] B = new Int[data.length];     int s = p;     int t = q + 1;     int k = p;             while (s <= q && t <= r) {if (Data[s] <= data[t]) {b[k] = Data[s];         s++;             } else {b[k] = data[t];         t++;     } k++;     } if (s = = q + 1) b[k++] = data[t++];     else b[k++] = data[s++];  for (int i = p; I <= R; i++) Data[i] = B[i]; }

Back to Table of contents 8. Cardinality sorting

Used for a large number, when a long number is sorted.

The single digit of all the numbers is taken out, sorted by one-digit number, forming a sequence.

The 10-digit number of all the newly constituted numbers is taken out and sorted by 10 digits to form a sequence.

The code is implemented as follows:

public void sort (int[] array) {//First determine the number of sorts of trips;       int max = array[0];           for (int i = 1; i < Array.Length; i++) {if (Array[i] > max) {max = array[i];       }} int time = 0;           Determine the number of digits;           while (Max > 0) {max/= 10;       time++;           }//Set up 10 queues;       list<arraylist> queue = new arraylist<arraylist> ();           for (int i = 0; i < i++) {arraylist<integer> queue1 = new arraylist<integer> ();       Queue.add (queue1);           }//For time allocation and collection;               for (int i = 0; i < time; i++) {//allocate array elements;                 for (int j = 0; J < Array.Length; J + +) {//Get the number of time+1 digits;               int x = array[j]% (int) Math.pow (ten, i + 1)/(int) Math.pow (ten, I);               arraylist<integer> queue2 = queue.get (x);               Queue2.add (Array[j]);           Queue.set (x, queue2); } int CounT = 0;//element counter;               Collect queue elements; for (int k = 0, K < k++) {while (Queue.get (k). Size () > 0) {arraylist<intege                   r> Queue3 = Queue.get (k);                   Array[count] = queue3.get (0);                   Queue3.remove (0);               count++; }           }       }

Eight kinds of sorting algorithms for Java----

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.