Data Structure and algorithm sorting (Summary 1)
Sorting enables one of the most commonly used algorithms in our actual development to classify the internal sorting according to the principles in the sorting process, in general, it can be divided into insertion sorting, exchange sorting, selection sorting, Merge Sorting and other sorting methods. Let's first look at the insertion sorting algorithms and their specific implementations. The basic sorting principle of insert sorting is to examine each element to be sorted one by one and insert each new element to the appropriate position in the sequence that has already been sorted, so that the new sequence is still an ordered sequence. This sort mainly introduces three sorting methods: Direct insertion sorting, semi-insertion sorting, and Hill sorting. 1. insert the sort directly.. direct insertion and sorting of algorithm descriptions is the simplest insertion and sorting method. Its basic idea is that the sequence of only one element is always ordered. Therefore, for the sequence of n records, the insert operation can be performed one by one from the second element to the nth element in the ordered sequence to obtain the sequence of n elements ordered by keywords. In general, the way to insert an element in an ordered sequence containing the J-1 elements is to search for the expected position from the beginning of the J-1 element, in addition, you can move the elements behind the insert position while searching for the insert position. In this way, you can insert the elements directly when you find the appropriate insert position. Taking the keyword sequence {26, 53, 48, 11, 13, 48, 32, 15} as an example, the process of directly inserting the sort is as follows. B: The algorithm implements public void insertSort (int [] r, int low, int high) {for (int I = low + 1; I <= high; I ++) if (compare (r [I], r [I-1]) {// when the value is smaller than, you need to insert r [I] into the ordered table int temp = r [I]; r [I] = r [I-1]; int j = I-2; for (; j> = low & compare (temp, r [j]); j --) r [j + 1] = r [j]; // record backward shift r [j + 1] = temp; // insert to the correct position} [Efficiency Analysis] space efficiency: use only one secondary storage unit. Time Efficiency: assuming that the number of elements to be sorted is n, N-1 1 queries are performed for insert records to the ordered table one by one. Each row is divided into comparison key codes and moving records, the number of comparisons and the number of records to be moved depend on the initial arrangement of the columns to be sorted by the key code. The time complexity of direct insertion and sorting is O (n²) c: the implementation example is StraightInsertionSort. java package com. test. sort. insertion; public class StraightInsertionSort {/*** @ param args */public static void main (String [] args) {// TODO Auto-generated method stub System. out. println ("Implementation of direct insertion sorting function" ""); int [] arr = {23, 54, 6, 2, 65, 34, 2, 67, 7, 9, 43}; StraightInsertionSort sort = new StraightInsertionSort (); System. out. println ("sequence before sorting :" ); Sort. printArr (arr); sort. insertSort (arr, 0, arr. length-1); System. out. println ("sorted sequence:"); sort. printArr (arr);} public void insertSort (int [] r, int low, int high) {for (int I = low + 1; I <= high; I ++) if (compare (r [I], r [I-1]) {// when the value is smaller than, you need to insert r [I] into the ordered table int temp = r [I]; r [I] = r [I-1]; int j = I-2; for (; j> = low & compare (temp, r [j]); j --) r [j + 1] = r [j]; // record backward shift r [j + 1] = temp; // insert Correct location} public boolean compare (int paramA, int paramB) {if (paramA <paramB) {return true;} else {return false ;}} /*** print the array element in sequence */public void printArr (int [] arr) {if (arr! = Null) {for (int temp: arr) {System. out. print (temp + "");} System. out. println () ;}}d: result output 2. hill sort. the method description is also called "downgrading incremental sorting". It is also a sort method that belongs to the insert sorting class and an improvement on direct insertion sorting, however, the time efficiency has been greatly improved. From the analysis of direct insertion sorting, although the time complexity of direct insertion sorting is O (n², the time complexity can be increased to O (n ). It can be seen that the efficiency of directly inserting sorting can be greatly improved when the elements to be sorted are in basic order. On the other hand, because the direct insertion sorting method is simple, the efficiency of the n value is also relatively high. Hill sorting is a sorting method obtained by improving the direct insertion sorting based on these two points. The basic idea of hill sorting is: first, the elements to be sorted are divided into multiple sub-sequences, so that the number of elements in each sub-sequence is relatively small, and each sub-sequence is directly inserted for sorting, after the sorting sequence is "basic order", insert and sort all elements directly. Based on the above sorting ideas, we will show the sort process of hill sorting: (1) Select a step sequence t1, t2 ,..., Tk, where ti> tj (I <j), tk = 1; (2) k sort by the number of step sequence, k sort by sort element sequence; (3) sort by each step, based on the corresponding step ti, the columns to be sorted are divided into ti sub-sequences, and the sub-sequences are inserted and sorted respectively. When the step size factor is 1, all elements are processed as a sequence and the length is n. Take the keyword sequence {26, 53, 67, 48, 57, 13, 48, 32, 60, 50} as an example. Assume that the selected step sequence is {5, 3, 1 }, the process of sorting by hill is 9-2. Because the length of the step sequence is 3, a total of three sort orders are required for the sorting sequence. First, the keyword sequence is divided into five sub-SEQUENCES {26, 13}, {53, 48}, {67, 32}, {48, 60}, {57, 50}, directly insert and sort them, and the result is displayed. Then, sort the second hill. If the step size is 3, the keyword sequence is divided into three sub-sequences: {13, 48, 53, 57}, {48, 50, 67 }, {32, 26, 60}, insert the sorted results directly. Finally, insert and sort the entire sequence directly. Then, a sequence with the keyword order is obtained, and the hill sorting ends. B. public void shellSort (int [] r, int low, int high, int [] delta) {for (int k = 0; k <delta. length; k ++) shellInsert (r, low, high, delta [k]); // One step is the direct insertion sorting of delta [k]} private void shellInsert (int [] r, int low, int high, int deltaK) {for (int I = low + deltaK; I <= high; I ++) if (compare (r [I], r [I-deltaK]) {// when the value is smaller than, you need to insert r [I] into the ordered table int temp = r [I]; int j = I-deltaK; for (; j> = low & comp Are (temp, r [j]); j = j-deltaK) r [j + deltaK] = r [j]; // record backward shifting [j]; r [j + deltaK] = temp; // insert to the correct position} [Efficiency Analysis] space efficiency: use only one secondary storage unit. Time Efficiency: assuming that the number of elements to be sorted is n, N-1 1 queries are performed for insert records to the ordered table one by one. Each row is divided into comparison key codes and moving records, the number of comparisons and the number of records to be moved depend on the initial arrangement of the columns to be sorted by the key code. The time complexity of direct insertion sorting is O (n²) c. example HashInsertSort. java package com. test. sort. insertion; public class HashInsertSort {/*** @ param args */public static void main (String [] args) {// TODO Auto-generated method stub System. out. println ("Implementation of the hill sorting function" "); int [] arr = {23, 54, 6, 2, 65, 34, 2, 67, 7, 9, 43}; int [] delta = {5, 3, 1}; HashInsertSort sort = new HashInsertSort (); System. out. println ("sequence before sorting:"); sort. p RintArr (arr); sort. shellSort (arr, 0, arr. length-1, delta); System. out. println ("sorted sequence:"); sort. printArr (arr);} public void shellSort (int [] r, int low, int high, int [] delta) {for (int k = 0; k <delta. length; k ++) shellInsert (r, low, high, delta [k]); // One step is the direct insertion sorting of delta [k]} private void shellInsert (int [] r, int low, int high, int deltaK) {for (int I = low + deltaK; I <= high; I ++) if (compar E (r [I], r [I-deltaK]) {// when the value is smaller than, you need to insert r [I] into the ordered table int temp = r [I]; int j = I-deltaK; for (; j> = low & compare (temp, r [j]); j = j-deltaK) r [j + deltaK] = r [j]; // record backward shifting [j]; r [j + deltaK] = temp; // insert to the correct position} public boolean compare (int paramA, int paramB) {if (paramA <paramB) {return true;} else {return false ;}} /*** print the array element in sequence */public void printArr (int [] arr) {if (arr! = Null) {for (int temp: arr) {System. out. print (temp + "");} System. out. println () ;}} d. result output 3. semi-insert sorting. the algorithm description can be directly inserted into the sorting algorithm, which is simple and easy to implement. When the number of elements to be sorted is n hours, this is a good sorting method, but usually the number of elements to be sorted is n, it is not appropriate to directly Insert the sorting method, in this case, we need to improve the direct insertion sorting. The basic operation of directly inserting sorting is to insert an element into the ordered sequence. The insertion position is determined by comparing the elements in the ordered sequence by keywords one by one. Since the insertion position is determined in the ordered sequence, the insertion position can be determined by the binary sequence continuously. That is, the method for searching the insertion position can be implemented using the semi-query. B. public void binInsertSort (int [] r, int low, int high) {for (int I = low + 1; I <= high; I ++) {int temp = r [I]; // Save the int hi = I-1; int lo = low; // set the initial interval while (lo <= hi) {// semi-definite insert position int mid = (lo + hi)/2; if (compare (temp, r [mid]) hi = mid-1; else lo = mid + 1 ;}for (int j = I-1; j> hi; j --) r [j + 1] = r [j]; // move element r [hi + 1] = temp; // insert element} // for} [Efficiency Analysis] space efficiency: use only one auxiliary Save the ticket. Time Efficiency: semi-insert sorting only reduces the number of comparisons of elements, but does not reduce the number of Moving Elements. the time complexity of semi-insert sorting is O (n² ). C. application Example: BinaryInsertSort. java package com. test. sort. insertion; public class BinaryInsertSort {/*** @ param args */public static void main (String [] args) {// TODO Auto-generated method stub System. out. println ("Implementation of semi-insertion sorting function" ""); int [] arr = {23, 54, 6, 2, 65, 34, 2, 67, 7, 9, 43}; BinaryInsertSort sort = new BinaryInsertSort (); System. out. println ("sequence before sorting:"); sort. printArr (arr); sort. binInsertS Ort (arr, 0, arr. length-1); System. out. println ("sorted sequence:"); sort. printArr (arr);} public void binInsertSort (int [] r, int low, int high) {for (int I = low + 1; I <= high; I ++) {int temp = r [I]; // Save the int hi = I-1; int lo = low; // set the initial interval while (lo <= hi) {// semi-definite insert position int mid = (lo + hi)/2; if (compare (temp, r [mid]) hi = mid-1; else lo = mid + 1 ;}for (int j = I-1; j> hi; j --) r [J + 1] = r [j]; // move element r [hi + 1] = temp; // insert element} // for} public boolean compare (int paramA, int paramB) {if (paramA <paramB) {return true;} else {return false ;}} /*** print the array element in sequence */public void printArr (int [] arr) {if (arr! = Null) {for (int temp: arr) {System. out. print (temp + "") ;}system. out. println ();}}}