ordering Elements (1)--Simple sorting
This series of articles mainly introduces the knowledge of commonly used algorithms and data structures, records the contents of "Algorithms i/ii" course, adopts "algorithm (4th edition)" This Red Cookbook as a learning material, language is java. I don't have to say much about the fame of this book. Watercress Rating 9.4, I myself also think is an excellent learning algorithm books.
with this series of articles, you can deepen your understanding of data structures and basic algorithms (individuals think more clearly than schools) and deepen their understanding of Java.
Ordering of elements 1 simple sort sort Problem 1 callback function 2Java in the route diagram 3 full order 4 comparable API 41 Implementation Principle 42 Example Select Sort 1 Select Sort Idea Animation 2 Select sort the inner loop invariance 3 Select Sort Code Insert sort 1 Insert Sort idea Animation 2 Insert Sort inner loop invariance 3 Insert Sort code 4 Insert Sort Analysis
1. Sorting Issues
Sorting problems are very common in everyday life, such as the simplest, student list, there are many sorts, names, grades, numbers, dorm numbers ...
The question is, how do we sort on multiple types. The answer is--the callback function.
The idea is simple: we call the system's sort function, and the system sorts the specific objects according to the CompareTo function of the callback object.
1.1 callback function
In different languages, there are different ways to implement callback functions, in which Java uses the interfaces
Roadmap for callback functions in 1.2Java
1.3 Full Order
Set set X has a full order relationship, if we use this relationship with ≤, then the following statements for all A, B and C in X are established: if A≤b and B≤a are a=b (inverse symmetry) if a≤b and B≤c are a≤c (transitive) a≤b or b≤a (complete)
Note: a double ≤ is not a full-order 1.4 comparable API 1.4.1 Implementation principle must be a total order less than, equal to, greater than the return of negative numbers (-1), 0, positive (1) for incompatible types, thrown Abnormal
1.4.2 Example
2 more useful sort abstractions: less than swap
These two functions are useful in a sort order.
2. Select Sort 2.1 Select Sort Idea (animation)
Choosing a sort is a very simple idea:
Choose the smallest and first number exchanges, the second small one with the second number exchange ...
2.2 Selecting the internal loop invariance of the sort
2.3 Choosing a Sort code
public class Selection
{public
static void sort (comparable[] a)
{
int N = a.length;
for (int i = 0; i < N; i++)
{
int min = i;
for (int j = i+1; J < N; J + +)
if (less (a[j], a[min])
min = j;
Exch (A, I, min);
}
}
private static Boolean less (comparable V, comparable W)
{/
* as before
*
/} private static void Exch (C Omparable[] A, int i, int j)
{/
* as before */
}
}
3. Insert Sort
3.1 Insert Sort idea (animation)
The idea of inserting a sort algorithm is also simple:
Each to a number, insert him in front of the corresponding position, so that the front has been orderly
3.2 Insert Sort internal loop invariance
3.3 Inserting sort codes
public class insertion
{public
static void sort (comparable[] a)
{
int N = a.length;
for (int i = 0; i < N; i++)
for (int j = i; j > 0; j--)
if (less (a[j], a[j-1])
Exch (A, J, j-1);
else break;
}
private static Boolean less (comparable V, comparable W)
{/
* as before
*
/} private static void Exch (C Omparable[] A, int i, int j)
{/
* as before */
}
}
3.4 Insert Sort Analysis
The best case for inserting a sort is to compare N-1 times 0 times, worst case N22 and N22 times.
The insertion sort is highly efficient for partially ordered arrays, especially for fractional groups