package com.fh.sort; /** * @author * * @create 2018-03-06 at 10:22 pm **/ public class Sort { public static void main(String[] args){ Sort sort=new Sort(); // sort.quickSort(); // sort.xRSort(); // sort.simpleSort(); sort.bubbleSort(); } /** * Insertion sort - direct insertion sort *Basic idea: insert a record into the sorted sorted table to achieve a new sorted table with the number of records plus 1 * First treat the first record of the sequence as an ordered self-sequence, and then insert into the sequence one by one from the second sequence until the entire sequence is ordered */ public void quickSort(){ int [] array={12,23,35,67,89 }; int len= array.length; for ( int i=1;i<len;i++ ){ int courrentData=array [i]; // Data to be inserted int a=i-1; // Ordered data while (a>0&&courrentData<array[a]){ // Compare, compare data in a loop, when the data in the array is greater than the current data When array[a+1]=array[a]; // Move data, a-- ; } array[a+1]=courrentData;//赋值 } for (int a:array ) { System.out.println("\n"+a); } } /** * Hill sort--processing large amounts of data * Set the number of arrays to n, k=n/2, and divide the subscripts with a gap of k into a group to form an ordered sequence * When taking k=k/2, the subscript value is k to form a set of ordered sequences */ public void xRSort(){ int[] array={12,23,34,45,56,78,34,56,12,78,99}; int b=array.length; while (b!=0){ b =b/2 ; for ( int x=0;x<b;x++){ // Number of groups for ( int i=x+b;i<array.length;i+=b){ //In a group Elements of int courrent=array[i]; // something in direct insertion sort int j=i- b; while (j>0&&courrent< array[j]){ array[j+b]=array[j]; j=j-b; } array[j+b]=courrent; } } } for (int c:array ) { System.out.println("\n"+c); } } /** * Simple selection sort * If each comparison is swapped, it is a swap sort, if the loop ends with a swap, it's a simple selection sort * Traverse the smallest number and put the smallest number at the top * Traverse the numbers after traversing, putting the smallest number at the top * Repeat the previous process */ public void simpleSort(){ int[] array={12,32,22,45,67,89,9,2,23,34}; int length=array.length; for (int i=0;i<length;i++){ int key=array[i]; int position=i; for (int j=i+1;j<length;j++){ if(array[j]<key){ key=array[j]; position=j; } } array[position]=array[i]; array[i]=key; } for (int a:array ) { System.out.println("\n"+a); } } /** * Bubble Sort * Compare the elements in the sequence pairwise, and put the largest one at the end * Compare the remaining elements and put the largest at the end */ public void bubbleSort() { int [] array = {12, 23, 34, 12, 45, 23, 34, 2, 67, 9 }; int length = array.length; int tem; for ( int i = 0; i < length; i++ ) { for ( int j = 0; j < length - i - 1; j++) { // Need -1, otherwise the subsequent j+1 will be out of bounds if (array[j] > array[j + 1]) { // If the previous data is greater than the next data, data data exchange tem = array[j]; array[j] = array[j + 1]; array[j + 1] = tem; } } } for (int a : array ) { System.out.println("\n" + a); } } /** * Quick sort * Select the first number as p, put the number less than p on the left, and put the number greater than p on the right */ public void quickSorts(){ } /** * merge sort * Select two adjacent numbers to form an ordered sequence * Select two ordered sequence groups to form an ordered sequence * Repeat the above steps */ public void gBSorts(){ } /** * Radix sort * Take out the digits of the array and sort them according to the digits to form an ordered sequence * Take out the ten digits of the formed ordered sequence and sort it */ public void jsSorts(){ } }