Address Source Text http://blog.csdn.net/hijiankang/article/details/9207735
Note: This is illustrated by the example of the increment sequence
First, bubble sort
1, principle: Starting from the first position of the array 22 compare Array[index] and array[index+1], if ARRAY[INDEX] greater than array[index+1] then Exchange Array[index] and array[index+1] Position, ending to the end of the array;
Starting from the first position of the array, repeat the above action, ending with an array length minus one position;
Starting from the first position of the array, repeat the above action, ending with an array length minus two positions;
。。。。
2. Time complexity: O (N²), carried out (n-1) * (n-2) .... =n* (n-1)/2 comparisons and the number of exchanges in half (in all cases), then the time complexity is O (n^2) According to the large O notation
3. Code
/**
* Bubbling algorithm for adding sequences
* @param array
*/
public static void Bubblingsort (int[] array) {
for (int i=0;i<array.length;i++) {
for (int j=0;j<array.length-i-1;j++) {
if (Array[j]>array[j+1]) {
Swap (array,j,j+1);
}
}
}
}
private static void Swap (int[] array,int x,int y) {
int temp = array[x];
ARRAY[X] = Array[y];
Array[y] = temp;
}
Second, choose the sort
1, principle: Select a value array[0] as a benchmark, and then loop to find the smallest value except this value (find the minimum value less than the benchmark), exchange these two values, then the minimum value is placed on the array[0], and then the array[1] as a benchmark, from the remaining unsorted value found in the minimum value, And swap the two values.
(graphs in data structures and algorithms)
2. Time complexity: O (n^2), reducing the number of array exchanges compared to bubble sort
3. Code
/**
* Select sort Add Order
* @param array
*/
public static void Selectsort (int [] array) {
for (int i=0;i<array.length;i++) {
int index = i;
for (int j=i+1;j<array.length;j++) {
if (Array[j] < Array[index]) {
index = j;
}
}
if (i! = index) swap (I,index,array);
Display (array);
System.out.println ();
}
}
public static void swap (int a,int b,int array[]) {
int temp = Array[a];
Array[a] = array[b];
ARRAY[B] = temp;
}
Third, insert sort
1. Principle: The idea of inserting a sort is that the array is ordered by the department, and then the unordered part loops are inserted into the ordered sequence.
(picked from data structures and algorithms)
2. Time complexity: The time complexity of the sequence in which the order is inserted is also O (n^2), but the order time complexity for the basic ordered sequence is O (N)
3. Code
/**
* Insert Sort Ascending
* @param array
*/
public static void Insertsort (int [] array) {
for (int out=1;out<array.length;out++) {
int temp = array[out];//The marked value or the value that is currently required to be inserted
int in = out;
Move backward if the round-robin value is greater than the marked value
while (in > 0 && temp < array[in-1]) {
Array[in] = array[in-1];
In--;
}
Inserts the tagged value into the empty position that was eventually moved out
Array[in] = temp;
}
}
Next write: Hill sort, quick sort
Bubble sort, select sort, insert sort