A few days ago, I saw a previous blog post titled "eight essential orders for programmers". I couldn't help but start to review Yan Yumin's "Data Structure, I will use Java to implement these functions one by one. I hope you can correct them.
Now we need to insert the nth number into the previous ordered number so that the N number
It is also sorted. This repeats until all the rows are sorted.
(1) Basic Idea: the number of groups to be sorted by an algorithm is divided into several groups according to a certain increment D (n/2, n is the number of the numbers to be sorted, the subscript difference of records in each group is D. insert and sort all elements in each group, group them with a small increment (D/2), and insert and sort them directly in each group. When the increment value is reduced to 1, the sorting is completed after direct insertion and sorting.
3. Simple selection and sorting
(1) Basic Idea: In the number of groups to be sorted, select the minimum number and the number at the first position;
Then find the smallest number in the remaining number and exchange it with the number in the second position, so that the loop ends until the last number is compared with the last number.
(2) instance:
(3) implemented in Java
publicclass selectSort { public selectSort(){ int a[]={1,54,6,3,78,34,12,45}; int position=0; for(int i=0;i<a.length;i++){ int j=i+1; position=i; int temp=a[i]; for(;j<a.length;j++){ if(a[j]<temp){ temp=a[j]; position=j; } } a[position]=a[i]; a[i]=temp; } for(int i=0;i<a.length;i++) System.out.println(a[i]); }}
4. Heap sorting
(1) Basic Idea: heap sorting is a kind of tree-based selection and sorting, which effectively improves Direct selection and sorting.
The heap is defined as follows: a sequence with n elements (H1, H2 ,..., HN), if and only if (HI> = h2i, HI> = 2I + 1) or (Hi <= h2i, hi <= 2I + 1) (I = 1, 2 ,..., n/2) is called heap. Here we only discuss the heap that meets the conditions of the former. From the definition of heap, we can see that the heap top element (that is, the first element) must be a heap top element (large top heap ). A full binary tree can intuitively represent the heap structure. The heap top is the root, and the others are the left and right subtree. Initially, the sequence of the numbers to be sorted is regarded as a binary tree for sequential storage, and their storage order is adjusted to make it a heap. At this time, the root node of the heap has the largest number. Then, the root node is exchanged with the last node in the heap. Then adjust the preceding (n-1) number to make it heap. Wait until there are only two nodes in the heap and exchange them. Finally, an ordered sequence of N nodes is obtained. According to the algorithm description, heap sorting requires two processes: creating a heap and switching the last element of the heap. Therefore, heap sorting consists of two functions. One is the heap build penetration function, and the other is the function that calls the penetration function repeatedly to implement sorting.
(2) instance:
Initial Sequence :,
Heap creation:
Swap, maximum number of kicks out from the heap
Create a heap at the remaining node, and then swap out the maximum number.
And so on: The last two remaining nodes in the last heap are exchanged, kicked out, and sorted.
(3) implemented in Java
Import Java. util. arrays; publicclass detail {INTA [] = {49,38, 65,97 }; public heapsort () {heapsort (a);} public void heapsort (INT [] A) {system. out. println ("start sorting"); int arraylength =. length; // cyclically build heap for (INT I = 0; I <arrayLength-1; I ++) {// build heap 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;} // create a large top heap privatevoid buildmaxheap for the data array from 0 to lastindex (INT [] data, int lastindex) {// todo auto-generated method stub // start from the parent node of the node (last node) at lastindex for (INT I = (lastIndex-1)/2; i> = 0; I --) {// K Save the node int K = I; // If the subnode of the current K node has a while (K * 2 + 1 <= lastindex) {// int biggerindex = 2 * k + 1 index of the Left subnode of the k node; // If biggerindex is smaller than lastindex, that is, if (biggerindex <lastindex) {// if the value of the right subnode is large, if (data [biggerindex] <data [biggerindex + 1]) {// biggerindex always records the index of a large subnode biggerindex ++ ;}} // if the value of a K node is smaller than the value of a large sub-node, if (data [k] <data [biggerindex]) {// exchange their swap (data, K, biggerindex); // assign biggerindex to K to start the next loop of the while loop, and re-ensure that the value of K nodes is greater than the value of its left and right subnodes K = biggerindex ;} else {break ;}}}}}Programmer-required 8 major sorting (3) ------- Bubble sorting and quick sorting (implemented in Java)
5. Bubble Sorting
(1) Basic Idea: In the number of a group to be sorted, compare and adjust the two adjacent numbers from top to bottom based on the total number in the range not sorted yet, let a large number sink, a small number rises. That is, when the numbers of two adjacent parties are compared and their sorting and sorting requirements are opposite, they are exchanged.
(2) instance:
(3) implemented in Java
publi cclass bubbleSort {public bubbleSort(){ int a[]={1,54,6,3,78,34,12,45}; int temp=0; for(int i=0;i<a.length;i++){ for(int j=i+1;j<a.length;j++){ if(a[i]>a[j]){ temp=a[i]; a[i]=a[j]; a[j]=temp; } } } for(int i=0;i<a.length;i++) System.out.println(a[i]); }}
After a reminder from taoyou, I found that the above is not an authentic bubble sort, So I corrected it:
Authentic Bubble Sorting:
public class bubbleSort {publicbubbleSort(){ int a[]={49,38,65,97,76,13,27,49,78,34,12,64,5,4,62,99,98,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]){temp=a[j];a[j]=a[j+1];a[j+1]=temp;}}}for(int i=0;i<a.length;i++)System.out.println(a[i]);}}
But what sort is the first? It's a bit similar to sorting, but it's not. You can just look at the top of your fingers and try again and again!
6. Quick sorting
(1) Basic Idea: select a benchmark element. Generally, select the first or last element and divide the columns to be sorted into two parts, one part smaller than the benchmark element, A part is greater than or equal to the reference element. At this time, the reference element is in the correct position after sorting, and then uses the same method to recursively sort and divide the two parts.
(2) instance:
(3) implemented in Java
Public class quicksort {INTA [] = {49,38, 65,97, 35, 25 }; public quicksort () {quick (a); For (INT I = 0; I <. length; I ++) system. out. println (A [I]);} publicint getmiddle (INT [] list, int low, int high) {int TMP = list [low]; // The first of the array is used as the axis while (low 8 major sorting required by programmers (4) ------- Merge Sorting and base sorting (implemented in Java)
7. Merge and sort
(1) Basic sorting: the Merge Sorting method combines two (or more) ordered tables into a new ordered table, that is, the sequence to be sorted is divided into several subsequences, each sub-sequence is ordered. Then combine the ordered subsequences into the overall ordered sequence.
(2) instance:
(3) implemented in Java
Import Java. util. arrays; public class mergingsort {int A [] = {, 25, 53,51}; publicmergingsort () {sort (A, 0,. length-1); For (INT I = 0; I <. length; I ++) system. out. println (A [I]);} public void sort (INT [] data, int left, int right) {// todo auto-generated method stub if (left <right) {// locate the intermediate index int center = (left + right)/2; // recursive sort (data, left, center) on the left array ); // recursive sort (data, center + 1, right) for the right array; // merge Merge (data, left, center, right );}} public void merge (INT [] data, int left, int center, int right) {// todo auto-generated method stub int [] tmparr = new int [data. length]; int mid = center + 1; // index of the middle array of the third record int third = left; int TMP = left; while (left <= center & Mid <= right) {// extract the smallest value from the two arrays and put it into the intermediate array if (data [left] <= data [Mid]) {tmparr [third ++] = data [left ++];} else {tmparr [third ++] = data [Mid ++];} // The remaining parts are sequentially placed in the middle array while (mid <= right) {tmparr [third ++] = data [Mid ++];} while (left <= center) {tmparr [third ++] = data [left ++];} // copy the content in the intermediate array back to the original array while (TMP <= right) {data [TMP] = tmparr [TMP ++];} system. out. println (arrays. tostring (data ));}}
8. Base sorting
(1) Basic Idea: unify all the values to be compared (positive integers) into the same digit length, and add zero before the numbers with shorter digits. Then, sort the data by bit. In this way, the sequence is changed to an ordered sequence after the ranking is completed until the sorting is completed by the highest bit.
(2) instance:
(3) implemented in Java
Import Java. util. arraylist; import Java. util. list; public class radixsort {int A [] =, 51}; Public radixsort () {sort (a); For (INT I = 0; I <. length; I ++) system. out. println (A [I]);} public void sort (INT [] array) {// first determine the number of sorted partitions; int max = array [0]; for (INT I = 1; I <array. length; I ++) {If (array [I]> MAX) {max = array [I] ;}} int time = 0; // number of digits to be determined; while (max> 0) {max/= 10; time ++;} // create 10 queues; List <arraylist> queue = new arraylist <arraylist> (); for (INT I = 0; I <10; I ++) {arraylist <integer> queue1 = new arraylist <integer> (); queue. add (queue1);} // time allocation and collection; For (INT I = 0; I <time; I ++) {// array element allocation; for (Int J = 0; j <array. length; j ++) {// obtain the time + 1 digit of the number. Int x = array [J] % (INT) math. pow (10, I + 1)/(INT) math. pow (10, 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 <10; k ++) {While (queue. get (k ). size ()> 0) {arraylist <integer> queue3 = queue. get (k); array [count] = queue3.get (0); queue3.remove (0); count ++ ;}}}}}