As soon as I have to take a written exam, and took out a dusty data structure textbook, want to see the basic data types and algorithms, because looking for a job is the Java aspect, so the algorithm of the sort is intended to write in Java, the level is generally, hope oneself can step, steadfast go down, It would be better if we could give you some inspiration. The code has all been tested and, if it is defective, please criticize it.
First introduced from Baidu---insert sort of the basic idea is: Each step will be a sorted record, by its key value of the size of the previous sorted file inserted in the appropriate position, until all inserted.
The basic operation of the insertion sort is to insert a data into the ordered data in order to obtain a new ordered data, and the algorithm is suitable for ordering a small number of data, and the time complexity is O (n^2). is a stable sort of method.
Stable definition: Assuming that in the sequence of records to be sorted, there are multiple records with the same keyword, and if sorted, the relative order of the records remains unchanged, that is, in the original sequence, RI=RJ, and RI before RJ, and in the sorted sequence where Ri is still before RJ, this sort algorithm is said to be stable , otherwise known as unstable.
My understanding is that:
1. The number to be sorted is divided into two parts, the first is ordered and the second is disordered.
2. In the beginning, the first number can be viewed as orderly, and one number in order from the second unordered number at a time.
3. Compare the obtained number with the last one in the ordered number, if the former is less than the latter, find the right place to insert.
4. Loop step three, then sort the end.
Java implementation (data using Min version)
public void Insertsort (int []s) {
int J;
for (int i = 1; I <s.length; i++) { //starting from I=1, that is, the first number of arrays is ordered group
if (S[i]<s[i-1]) {
int temp=s[i];
Starting from the back of an ordered array, find the right position for
(j = i-1 J >=0&&temp<s[j]; j--) {
s[j+1]=s[j];//, to Array1[i] Leave the insertion position
}
//Insert
s[j+1]=temp;
}
for (int i=0;i<s.length;i++) {
System.out.print (s[i]+ "");
}
}
Writing test methods
public static void Main (string[] args) {
//TODO auto-generated method stub
insertsort insertsort=new Insertsor T ();
int array []={49,38,65,97,76,13,27,49};//array assignment
insertsort.insertsort (array);
}
Print results are: 13 27 38 49 49 65 76 97
The second bubble sort: The bubble sort algorithm works as follows: (from the back-forward)
1. Compare adjacent elements. If the first one is bigger than the second one, swap them both.
2. For each pair of adjacent elements to do the same work, from the beginning to the end of the first pair. At this point, the final element should be the largest number.
3. Repeat the above steps for all elements except the last one.
4. Continue to repeat the steps above for fewer elements at a time until no pair of digits need to be compared.
is a stable sort.
My understanding: This is better to understand, because very image, like bubbles from the bottom of the floating up, every round of sorting, will appear a maximum number, until all the number of sorting completed.
Java implementation
public void Bubblesort (int. []s)
{for
(int i = s.length-1;i>0; i--) {for
(int j = 0; J < i; J +) {
if (S[j]>s[j+1]) {//The previous one is greater than the last
int temp=s[j];//swap position
s[j]=s[j+1];
S[j+1]=temp
}
}} for (int i = 0; i < s.length i++) {
System.out.print (s[i]+ "");
}
}
Main function test
public static void Main (string[] args) {
//TODO auto-generated method stub
bubblesort bubblesort=new Bubblesor T ();
int array []={49,38,65,97,76,13,27,49};
Bubblesort.bubblesort (array);
Print results are: 13 27 38 49 49 65 76 97
The third quick sort (the improvement for bubble sort) is the basic idea: by a trip to sort the data to be sorted into two separate parts, where all of the data is smaller than the other part of all the data, and then in this way to the two parts of the data are sorted quickly, the entire sorting process can be recursive, This achieves the entire data into an ordered sequence.
is an unstable sort.
My understanding: What does it mean to take the idea of divide and conquer? is to first divide the data into two parts, take an intermediate value (pivot), the median to the left of each number is smaller than the right one, and then for the left of the number, then take the middle number, so that the loop, and finally the entire array into order. Here is a dynamic picture to illustrate, very clear.
Java implementation
public class QuickSort {public
static void Main (string[] args) {
//TODO auto-generated method stub
int []s={ 49,38,65,97,76,13,27};
QuickSort qs=new QuickSort ();
Qs. QuickSort (S, 0, s.length-1);
Print for
(int i = 0; i < s.length i++) {
System.out.print (s[i]+ "");
}
Recursive sort public
void QuickSort (int []s,int low,int High)
{
if (low
Printed results: 13 27 38 49 65 76 97
The last sort of selection
Select an explanation of the sort: compare the size of the previous element in the array with the latter. If the following element is smaller than the preceding element, remember his position with a variable k, and then the second comparison, the previous "element" now becomes the "previous element" and continues with his "latter element" To compare if the following element is smaller than he is, use the variable K to remember its position in the array (subscript), by the end of the loop, we should have found the lowest number of the subscript, and then to judge, if the subscript is not the first element of the subscript, let the first element to exchange values with him, This will find the smallest number in the entire array. Then find the second small number in the array and let him exchange values with the second element in the array, and so on.
My understanding: The above is very clear, generally speaking, the first traversal to find the smallest number, the second traversal to find the second small number, and so on.
Java implementation
public class Selectsort {public
static void Main (string[] args) {
//TODO auto-generated method stub
Selectso RT selectsort=new Selectsort ();
Selectsort.selectsort ();
}
The public void Selectsort ()
{
int temp;//holds the smallest numeric
int []s={100,52,68,59,57} in each loop;
The position of the minimum numeric value
int position=0;
for (int i = 0; I <s.length; i++) {
position=i;
Temp=s[i];
For
(int j = i+1 J < s.length; J + +) {
if (s[j]<temp) {
temp=s[j] when
found to be smaller than s[i]; Position=j
}
}
Exchange position
S[position]=s[i];
s[i]=temp;
}
Print for
(int i = 0; i < s.length i++) {
System.out.print (s[i]+ "");}
}}
Printed results: 52 57 59 68 100
So far, the three types of sorting algorithms have been introduced.