Algorithm Manual (5) Preliminary Sorting Algorithm

Source: Internet
Author: User

Sorting is the process of sorting a group of objects according to certain rules. Even if you can use the sorting function in the standard library, learning the sorting algorithm has great significance:
Learning sorting algorithms helps you fully understand the algorithm performance comparison methods;

Similar technologies can effectively solve other types of problems;

Sorting algorithms are usually the first step to solve the problem;

More importantly, these algorithms are classic, elegant, and efficient.

Sorting plays an important role in business data processing and analysis and modern science. Among them, quick sorting algorithms are hailed as one of the top ten algorithms in science and engineering in the 20th century. Today we have to look at a relatively simple but classic preliminary sorting algorithm, including selecting sorting, inserting sorting, and shell sorting.

Preparation

Before getting started, we first agree on the algorithm class template form, where we put the sorting code into the sort () method, and the less () method compares the elements and returns the bool value, exch () the method is used for element position exchange in the following format:

Public class example {public static void sort (icomparable [] A) {// sort code} Private Static void exch (icomparable [] A, int I, Int J) {icomparable temp = A [I]; A [I] = A [J]; A [J] = temp;} Private Static bool less (icomparable V, icomparable W) {return v. compareto (w) <0;} Private Static void show (icomparable [] A) {// print data} Private Static bool issorted (icomparable []) {for (INT I = 1; I <. length; I ++) {If (less (A [I], a [I-1]) return false;} return true ;}}

Here I use the C # implementation, the less () method uses the icomparable interface method, applicable to any data types that implement the icomparable interface for sorting, the system int, string and other types all implement this interface.

Select sort

Overview:

First, find the smallest element of the array and swap it with the first element of the array. Again, find the smallest element in the remaining element and exchange it with the second element. Until the entire array is sorted. Because it constantly selects the smallest of the remaining elements, it is called selection sorting.

Analysis:

For an array with a length of N, the selection of sorting requires approximately (N ^ 2)/Two comparisons and N exchanges. It is a simple sorting algorithm that is easy to understand and implement. It has two distinctive features:

1. The running time has nothing to do with the input. Experiments show that the input of an ordered array is as long as the input of a random array.

2. Data movement is minimal. Only n exchanges are selected for sorting, which cannot be done by other sorting algorithms.

Code implementation:

Public class selection {public static void sort (icomparable [] A) {// sort code for (INT I = 0; I <. length; I ++) {int min = I; for (Int J = I + 1; j <. length; j ++) {If (less (A [J], a [Min]) min = J;} exch (A, Min, I );}} private Static void exch (icomparable [] A, int I, Int J) {icomparable temp = A [I]; A [I] = A [J]; A [J] = temp;} Private Static bool less (icomparable V, icomparable W) {return v. compareto (w) <0;} Private Static void show (icomparable [] A) {// print data} Private Static bool issorted (icomparable []) {for (INT I = 1; I <. length; I ++) {If (less (A [I], a [I-1]) return false;} return true ;}}

The selection algorithm class is designed according to our agreed rules. After running the class, we can find the correct sorting result.

After executing the arrays of, and respectively, it is found that it takes a long time to select sorting for large-scale sorting. Small and Medium Scale sorting can be basically used.

 

Insert sort

Overview:

Just as you sort cards, insert each element to an appropriate position in an ordered sequence to free up space for the inserted element, we need to move all other elements right before insertion. This algorithm is called insert sorting.

Analysis:

Unlike sorting, the time required for inserting sorting depends entirely on the initial position of the element in the input. For example, sorting a large ordered array is much faster than sorting a random or reverse array.

For an array with a random length of N and NO duplicate primary keys, it is required to insert the sorting on average ~ (N ^ 2)/4 comparisons and ~ (N ^ 2)/4 exchanges. In the worst case ~ (N ^ 2)/2 comparisons and ~ (N ^ 2)/2 exchanges, preferably requires N-1 times to compare and 0 exchanges.

Implementation:

Public class insertion {public static void sort (icomparable [] A) {// sorting code int n =. length; For (INT I = 1; I <n; I ++) {for (Int J = I; j> 0 & less (A [J], A [J-1]); j --) exch (A, J, J-1); }}// optimize the number of public static void fastersort (icomparable []) {int n =. length; For (INT I = 1; I <n; I ++) {icomparable temp = A [I]; Int J = I-1; while (j> 0 & less (temp, a [J]) {A [J + 1] = A [J]; j --;} A [J + 1] = temp;} Private Static void exch (icomparable [] A, int I, Int J) {icomparable temp = A [I]; A [I] = A [J]; A [J] = temp;} Private Static bool less (icomparable V, icomparable W) {return v. compareto (w) <0;} Private Static void show (icomparable [] A) {// print data for (INT I = 1; I <. length; I ++) {console. writeline (A [I]) ;}} Private Static bool issorted (icomparable [] A) {for (INT I = 1; I <. length; I ++) {If (less (A [I], a [I-1]) return false;} return true ;}}

 

After Multiple Input experiments, we found that insertion sorting is quite slow in processing large data volumes. However, unlike the sorting method, when the input is sorted data of a large scale (such as 100000 in size), the speed is much faster, and the sorting method still takes a long time, this is better than sorting by choice.

 

Algorithm Manual (5) Preliminary Sorting Algorithm

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.