Programmers must know the 8 largest sorting, and programmers must know the sorting.

Source: Internet
Author: User

Programmers must know the 8 largest sorting, and programmers must know the sorting.

Relationship between the eight sorting types:

 

1. Insert the sorting directly.

(1) Basic Idea: In the number of groups to be sorted, assume that the number (n-1) [n> = 2] is already

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.

(2) Instances

 

(3) implemented in java

 

Package com. njue; public class detail {public insertSort () {inta [] =, 35,25, 53,51}; int temp = 0; for (int I = 1; I <. length; I ++) {int j = I-1; temp = a [I]; for (; j> = 0 & temp <a [j]; j --) {a [j + 1] = a [j]; // move the value greater than temp to a unit.} a [j + 1] = temp ;} for (int I = 0; I <. length; I ++) System. out. println (a [I]) ;}}

 

2. Hill sorting (minimum incremental sorting)

(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.

(2) instance:

 

 

(3) implemented in java

public class shellSort {publicshellSort(){int a[]={1,54,6,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=i-d;temp=a[i];for(;j>=0&&temp<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 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

public class 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

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; public class HeapSort {int a [] =, 53,51}; 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 private void 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 ;}}}}}


 

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

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]);}}

 

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 {int a [] = {49,38, 65,97, 35, 25 }; publicquickSort () {quick (a); for (int I = 0; I <. length; I ++) System. out. println (a [I]);} public int getMiddle (int [] list, int low, int high) {int tmp = list [low]; // The first of the array is used as the axis while (low 

 

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 (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 ++ ;}}}}}

 

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.