This article mainly introduces how Java achieves eight commonly used sorting algorithms: inserting sorting, bubbling sorting, selecting sorting, Hill sorting, fast sorting, merger sorting, stacking order, and LST base sorting. Share it with everyone to learn.
Classification
1) Insert sort (directly inserted sorting, Hill sorting)
2) Exchange sorting (bubbling sorting, fast sorting)
3) Select sorting (directly select sorting, stacked sorting)
4) Merge sorting
5) Distribution and sorting (base sorting)
The required auxiliary space is the most: the most auxiliary space for mergers and sorting: the fastest theverage speed of the stacking order: fast sorting
Unstable: Quickly sort, Hill sort, stacked sorting.
Let's take a look at the relationship between 8 sorting:
1. Directly insert sort
(1) Basic ideas: Among the sets to be sorted, assuming (n-1) [n> = 2] number is already row
In order, now the nn number is inserted into the previous order, so that this n number
It is also in order. This is repeatedly circulated until the whole order is in order.
(2) Example
(3) Realize with java
package com.njue; PublicClass Insertsort {Public Insertsort () {inta [] = {49,38,65,97,76,49,78,34,64,5,4,62,99, 98,54,56,17,18,23,34,15,35,25,53,51}; int Temp = 0; for (int i = 1; i <a <.Length; I ++) {int j = i -1; Temp = a [i]; for (; J> = 0 && TEMP <a [j]; j-) {a [j+1] = a [j]; // A unit} a [j+1] = test;} for (int i = 0; i <a.length; i ++) {system.out.println (a [i]);}}}}}2. Hill sort (minimum incremental sorting)
(1) Basic ideas: The number of groups to be sorted by the algorithm is divided into several groups according to the number of increased D (n/2, n as the number to be sorted). All elements in the group are directly inserted and sorted, and then use a smaller increase (D/2) to group it, and then directly insert sort in each group. When the increment is reduced to 1, the sort is directly inserted and the sort is completed.
(2) Example:
(3) Realize with java
PublicClass Shellsort {PublicShellsort () {int a [] = {1,54,3,78,34,12,45,56,100}; Double d1 = A.Length; int Temp = 0; While (TRUE) {D1 = Math.ceil (d1/2); int d = (int) d1; for (int x = 0; x <d; x ++) {for (int i = x+d; i <a.Length; i+= d ) {int j = id; temp = a [i]; for (; j> = 0 && test <a [j]; j- = d) {a [j+d] = a [j];} a [j+ d] = Temp;}} if (d == 1) {Break;} for (int i = 0; I <a.Length; i ++) {system.out.println (a [i]);}}}3. Simple selection of sorting
(1) Basic ideas: In a group to be sorted, select the smallest number of the number of numbers and the first position;
Then find the smallest number of the second position in the remaining numbers, so that the loop is compared with the last number of the last number and the last number.
(2) Example:
(3) Realize with java
public class selectsort {public selectsort () {int a [] = {1,54,3,78,34,12,45}; int position = 0; for (int i = 0; i <.length; I ++) {int j = i+1; pose = i; int Temp = a [i]; for (;; j <a.Length; j ++) {if (a [j] <temp) {temp = a [j] ; pose = j;}} a [posity] = a [i]; a [i] = test;} for (int i = 0; i <a.Length; i ++) systerm.println (a [i] );}}4. Heap sorting
(1) Basic ideas: Packing sorting is a tree -shaped selection of sorting, which is an effective improvement of direct selection of sorting.
The definition of the heap is as follows: a sequence (H1, H2, ..., Hn) with N elements, and only as satisfied (hi> = h2i, hi> = 2i+1) or (hi <= h2i, hi <= 2i 2i) +1) (i = 1,2, ..., n/2) is called a pile. Here we only discuss the piles that meet the conditions of the former. It can be seen from the definition of the heap that the top element (that is, the first element) must be the largest item (a large top dump). Complete binary tree can intuitively represent the structure of the heap. The top of the pile is rooted, and the others are left sub -trees and right sub -trees. Initially, the sequences to be sorted are regarded as a binary tree stored in order, adjusting their storage order so that they become a pile. Then exchange the root node with the last node of the heap. Then re-adjust the previous (N-1) number to make it pile. According to this type, until there are only two nodes, and exchange them, and finally get an orderly sequence of n nodes. From the perspective of algorithm description, the stacking sequencing requires two processes. One is to establish a pile, and the other is the last element exchange position of the top and the pile. Therefore, there are two functions composition of heap sorting. One is the penetration function of the pile, and the other is to repeatedly call the infiltration function to implement the sorting function.
(2) Example:
Initial sequence: 46,79,56,38,40,84
Construction:
Exchange, kick the maximum number from the pile
The remaining nodes are built again, and the maximum number is exchanged
Push in order: The last two nodes in the last pile of the last two nodes are exchanged, one is kicked out, and the sort is completed.
(3) Realize with java
Import Java.util.arrays; PublicClass Heapsort {inta [] = {49,38,65,97,76,27,78,34,264,5,4,62,98,54 , 56,17,18,23,34,15,35,25,53,51}; Public Heapsort () {Heapsort (a);} Public void Heapsort (int [] a) {system.out.println (" Start sorting "); int ARrayLength = A.Length; // Circle the built-in for (int i = 0; I <arrayLength-; i ++) {// build Buildmaxheap (A, ArrayLength-I); // Swap the top and last element Swap (a, 0, arrayLength-I); System.out.println (arrays.tostring (a));}} Private void swap (int [] data, int i, int j, int j, int j ) {// Todo Auto-Goneralted Method Stub int TMP = Data [i]; data [i] = data [j]; data [j] = tmp;} // to build a top diler Privatevoid from 0 to LastIndex Buildmaxheap (int [] data, int lastIndex) {// Todo Auto-Gonement Method Stub // The parent node of the node (last node) from LastIndex start for (int i = (Lastindex-); i> = 0; i-) {// k Save the node INT K = i; // If the sub-node of the current K node exists While (K*2+1 <= LastIndex) {// k Node's left child node Index INT BIGGERINDEX = 2*K+1; // If the BiggerIndex is smaller than LastIndex, that is, the right node of the K node representing the K node represented by BiggerIndex+1 exists if (BiggerIndex <Lastindex) {// If the value of the right node is larger, (Data [BiggerIndex] <data [BiggerIndex+1]) {// BiggerIndex always records the index BiggerIndex ++;} // If the value of the K node is less than the value of the K node if (data [data [data [data K] <data [biggerIndex]) {// exchange their swap (data, k, biggerIndex); // Give BiggerIndex to K, start the next cycle of the whole loop, and re -guarantee that the value of the K node is greater than the value of the left and right nodes. k = biggerIndex;} else {break;}}}}}5. Bubble sorting
(1) Basic ideas: In a group to be sorted, all the numbers within the scope that are not yet discharged, comparison and adjustment of the two adjacent two numbers from top to bottom, so that the larger ones Several sinking, smaller up. That is: Whenever two adjacent numbers are compared, they find that their sorting and sorting requirements are opposite, and they will interchange.
(2) Example:
(3) Realize with java
public class bubblesort {publicbbitsort () {inta [] = {49,38,65,97,76,13,49,78,34,5,4,62,98,54,54,56 , 17,18,23,34,15,35,25,53,51}; int Temp = 0; for (int i = 0; I <a.length-1; i ++) {for (int j = 0; j <A.Length-1-I; J ++) {if (a [j]> a [j+1]) {test = a [j]; a [j] = a [j+1]; a [j [j +1] = Temp;}}} for (int i = 0; I <a.Length; i ++) {system.out.println (a [i]);}}6. Quickly sort
(1) Basic ideas: Choose a benchmark element, usually choose the first element or the last element. Through a scanning, the arrangement to be sorted into two parts, one is smaller than the benchmark element, and part is greater than equal to the benchmark element. At this time The correct position after the element is ranked, and then the two parts are sorted in the same way.
(2) Example:
(3) Realize with java
PublicClass Quicksort {Inta [] = {49,38,65,97,76,13,27,78,34,264,5,4,99,98,54,56,17,18 23,34,15,35,25,53,51}; publicquicksort () {quick (a); for (int i = 0; i <.Length; i ++) {system.out.println (a [i] );}} Publicintt GetMiddle (int [] list, int low) {int TMP = list [Low]; // The first one of the array as the central axis (Low <high) {While (LOW <High && List) [HIGH]> = TMP) {high-;} list [low] = list [high]; // The record smaller than the middle shaft moves to the low-end While (Low <hIGH && List [Low] <= tmp) {Low ++ ;} List [high] = list [low]; // The record larger than the middle shaft moves to the high -end} list [Low] = tmp; // The middle shaft is recorded to the ending low _quicksort (int [] list, int low, int high) {if (low <high) {int Middle = getmiddle (list, low, high); // The list array is divided into two _quicksort (list, low, low MIDDLE -1); // Recursive sorting of low -character tables_quicksort (list, middle + 1, high); // Recursive sorting of high -character tables} publicvoid quick (int [] a2) {if (A2.. length> 0) {// View whether the array is empty_quicksort (A2,0, a2.length -1);}}}7. Merge sorting
(1) Basic sorting: Attachment (Merge) Sorting method is to merge two (or more) or more forms into a new order, that is Preface. Then combine the order sequence into the overall order.
(2) Example:
(3) Realize with java
Import Java.util.arrays; PublicClass Mergingsort {inta [] = {49,38,65,97,76,27,78,34,264,5,4,62,98,54 , 56,17,18,23,34,15,35,25,53,51}; PublicMergingsort () {sort (a, 0, a.Length-); for (int i = 0; I <A. length; I ++) System.out.println (a [i]);} Publicvoid sort (int [] data, int left, int Right) {// Todo-GoneraltedMethod Stub if (Left <RIGHT) { / / /Find Intermediate index int Center = (left+right)/2; // recursively sort (data, left, center) on the left array; // recursively sort (data, center+1, right) on the right array; // merge Merge (data, left, center, right);}} Publicvoid Merge (int [] data, int left, int center, int right) {// Todo-Goneorates int [data.length]; int MID = Center+1; // Third records the indexing int project of the middle array int project = left; int TMP = left; while (left <= center && mid <= right) {// Remove the minimum in the middle array from the two arrays into if (data [left] <= data [mid]) {tmparr [Third ++] = Data [left ++];} else {tmparr [Third ++] = data [mid ++]; MID <= Right) {TMPARR [Third ++] = Data [MID ++];} While (left <= Center) {tmparr [Third ++] = Data [left ++]; TMP <= Right) {data [tmp] = tmparr [tmp ++];} System.out.println (arrays.tostring (data));8. Base sorting
(1) Basic ideas: Uniform all the comparison values (positive integers) to the same number length, and the number of short digits is made up before zero. Then, starting from the lowest position and sorting in turn. This will become an orderly sequence from the lowest position to the highest bit sorting.
(2) Example:
(3) Realize with java
Import java.util.arrayList; Import Java.util.List; Public Class Radixsort {inta [] = {49,38,65,97,76,49,78,34,64,5,44 , 62,99,98,54,101,56,17,18,23,34,15,35,25,53,51}; Public Radixsort () {sort (a); for (Inti = 0; I <A. length; i ++) {system.out.println (a [i]);}} Public void sort (int [] array) {// First determine the number of sorting trips; int max = Array [0]; for (inti = 1; I <array.length; I ++) {if (array [i]> max {max = array [i];}} int Time = 0; // Judgment bit; while (max> 0) {max/ = 10; Time ++;} // Create 10 queues; list <arrayList> queue = newArrayList <arrayList> (); for (int i = 0; i <10; i ++) {ArrayList <infleger> Queue1 = NEW ARRR aylist <ins > (); Queue.add (queue1);} // Time secondary allocation and collection; for (int i = 0; i <time; i ++) {// allocate array elements; for (intj = 0; j <array; j <array; .length; j ++) {// get the time+1 digit number of numbers; int x = array [j]%(int) math.pow (10, i+1)/(int) math.pow (10, i i 10, i ); ARRAYLIST <integer> queue2 = queue.get (x); queue2.add (array [j]); queue.set (x, queue2);} int power = 0; // element counter; // collect queue element element element ; INT K = 0; K <10; K ++) {While (queue.get (k) .size ()> 0) {ArrayList <Integer> queue3 = queue.get (k); array [count] = queue3 .get (0); queue3.remove (0); count ++;}}}}}The above is all the contents of this article. I hope it will be helpful to everyone's learning.