First, insert class sort
1. Direct Insert Sort
Thought: Inserting the first I into the proper position of the former i-1
Time complexity: T (n) = O (n2).
Space complexity: S (n) = O (1).
Stability: Stable sorting.
If you encounter an element equal to the insert, the insertion element places the element you want to insert behind the equal element.
So, the order of the equal elements is not changed, the order from the original unordered sequence is the order of the sequence, so the insertion sort is stable
哨兵有两个作用:
① 进人查找(插入位置)循环之前,它保存了R[i]的副本,使不致于因记录后移而丢失R[i]的内容;
② 它的主要作用是:在查找循环中"监视"下标变量j是否越界。一旦越界(即j=0),因为R[0].可以和自己比较,循环判定条件不成立使得查找循环结束,从而避免了在该循环内的每一次均要检测j是否越界(即省略了循环判定条件"j>=1")
public void Insertsort (int[] array) { for (int i=1;i<array.length;i++)//No. 0 bit alone as an ordered series, starting from the 1th bit to walk backwards { if ( ARRAY[I]<ARRAY[I-1])//0~i-1 bit is ordered, if the first bit is less than I-1 bit, continue to seek and insert, otherwise think 0~i bit is also ordered, ignoring this cycle, the equivalent of continue { int temp=array [i];//holds the value of the I-bit int k = i-1; for (int j=k;j>=0 && temp<array[j];j--)//traverse forward from the i-1 bit and shift until found less than the I-bit value stop { array[j+1]=array[j]; k--; } array[k+1]=temp;//Insert the value of the I-bit }} }
2. Binary Insert Sort
Thought: Inserting data into a sequence that has already been sequenced, and determining the position by continuously comparing the size to the middle point
Time complexity: The time to compare is reduced to O (n㏒n), but the time spent on moving elements remains the same, so it is always time complexity or O (N2).
Space complexity: S (n) = O (1).
Stability: Stable sorting.
3. Hill sort
Thought: Also known as narrowing the incremental sorting method. The ordered sequence is divided into several smaller sub-sequences, then sorted by using the direct insertion sorting method, and then the order of a more orderly sequence is made, mainly to reduce the number of moves and improve the efficiency. The principle should be from disorder to gradually orderly, than directly from the disorder to the orderly movement of fewer times.
Time complexity: O (1.5 times of N)
Space complexity: O (1)
Stability: unstable sorting. {2,4,1,2},2 and 11 groups of 4 and 21, for the hill sort, the first 2 and the last 2 will have a change in position.
public static void Main (String [] args) {int[]a={49,38,65,97,76,13,27,49,78,34,12,64,1}; System.out.println ("Before sorting:"); for (int i=0;i<a.length;i++) {System.out.print (a[i]+ ""); }//Hill sort int d=a.length; while (true) {D=D/2; for (int x=0;x<d;x++) {for (int i=x+d;i<a.length;i=i+d) { int temp=a[i]; Int J; for (j=i-d;j>=0&&a[j]>temp;j=j-d) {a[j+d]=a[j]; } a[j+d]=temp; }} if (D==1) {break; }} System.out.println (); System.out.println ("After sorting:"); for (int i=0;i<a.length;i++) { System.out.print (a[i]+ ""); } }}
Ii. Sorting of Exchange classes
1. Bubble sort
Time complexity: T (n) = O (n2).
Space complexity: S (n) = O (1).
Stability: Stable sorting.
public class bubblesort{public void sort (int[] a) { int temp = 0; for (int i = a.length-1; i > 0; i.) {for (int j = 0; J < i; ++j) { if (a[j + 1] < a[j])
{ temp = a[j]; A[J] = a[j + 1]; A[j + 1] = temp;}}}}
2. Quick Sort
Thought: The improvement of the bubble sort, through a sort of sorting will be sorted into two separate parts, one part of all the data is smaller than the other part of all the data, and then this method to the two parts of the data are quickly sorted, the entire sorting process can be recursive, To achieve the entire data into an ordered sequence.
Time complexity: Average t (n) = O (n㏒n), Worst O (N2).
Space complexity: S (n) = O (㏒n).
Stability: Unstable sorting
First, the first number of the array is taken out as a key, before and after setting a i,j as the identity, and then take this key to the array from behind to traverse, and j--, until the first to find the number less than this key, and then exchange the two values, after the exchange is complete, We take this key to go backwards from I, and i++, and loop until the end of I=j, and when this is over, we'll find that the value larger than this key will go behind the key.
Third, select the class sort
1. Simple selection of sorting
Time complexity: T (n) = O (n2).
Space complexity: S (n) = O (1).
Stability: Unstable sorting
Ideas:
1) from the sequence you want to sort, find the element with the smallest keyword
2) If the smallest element is not in the first place, swap the position with the first element
3) from the remaining N-1 elements, find the smallest element of the keyword, repeat 1), 2) step
public class Selectionsort {
public void Selectionsort (int[] list) {
The number of times you need to traverse to get minimum values
It is important to note that when the number of N is to be sorted, it is already an ordered sequence after N-1 traversal.
for (int i = 0; i < list.length-1; i++) {
int temp = 0;
int index = i; Used to save the least worth index
Looking for the small number of the first I
for (int j = i + 1; j < List.length; J + +) {
if (List[index] > List[j]) {
index = j;
}
}
Place the small number I found in the first position.
temp = List[index];
List[index] = List[i];
List[i] = temp;
System.out.format ("%d trip: \ t", i + 1);
Printall (list);
}
}
Print Complete sequence
public void printall (int[] list) {
for (int value:list) {
System.out.print (value + "\ T");
}
System.out.println ();
}
public static void Main (string[] args) {
Initialize a random sequence
Final int max_size = 10;
int[] array = new Int[max_size];
Random random = new random ();
for (int i = 0; i < max_size; i++) {
Array[i] = Random.nextint (max_size);
}
Call Sort method
Selectionsort selection = new Selectionsort ();
System.out.print ("before sorting: \ t");
Selection.printall (array);
Selection.selectionsort (array);
System.out.print ("After sorting: \ t");
Selection.printall (array);
}
}
2. Tree selection sorting
Thought: In order to reduce the number of comparisons, 22 is compared, the smaller values are compared to 22, until the smallest output is obtained, then the original position is ∞, and then compared. Until all is output.
Time complexity: T (n) = O (n㏒n).
Spatial complexity: a simpler choice of sorting, adding n-1 additional storage space for the intermediate comparison results, is the tree structure of all the root nodes. S (n) = O (n).
Stability: Stable sorting.
3. Heap Sequencing
Stay
Four. Merge sort
Merge sort:
Thought: Assuming that the initial sequence has n records, first of all the N records as n ordered sub-sequence, each subsequence length of 1, and then 22 merge, get N/2 up to take the entire length of 2 (n is odd, the last sequence length is 1) ordered sub-sequence
On this basis, the ordered subsequence with a length of 2 is 22 merged, and a number of ordered sub-sequences with length 4 are obtained.
· Repeat until you get an ordered sequence of length n.
Time complexity: T (n) = O (n㏒n)
Space complexity: S (n) = O (n)
Stability: Stable sequencing
public class MergeSort {public static void merge (Int[] A, int. low, int mid, Int. high) {int[] temp = new Int[hi Gh-low + 1]; int i = low;//left pointer int j = mid + 1;//right pointer int k = 0; Move the smaller number first to the new array while (I <= mid && J <= High) {if (A[i] < a[j]) {temp[ k++] = a[i++]; } else {temp[k++] = a[j++]; }}//Move the left remaining number into the array while (I <= mid) {temp[k++] = a[i++]; }//Move the remaining number in the right side into the array while (J <= High) {temp[k++] = a[j++]; }//Overwrite the number in the new array with the Nums array for (int k2 = 0; K2 < temp.length; k2++) {a[k2 + low] = Temp[k2]; }} public static void MergeSort (Int[] A, int. low, int.) {int mid = (low + high)/2; if (Low < high) {//left mergesort (A, low, mid); Right MergeSort (A, mid + 1, high); Merge Right and Left Merge (A, low, mid, high); System.out.println (Arrays.tostring (a)); }} public static void Main (string[] args) {int a[] = {51, 46, 20, 18, 65, 97, 82, 30, 77, 50}; MergeSort (A, 0, a.length-1); System.out.println ("Sort result:" + arrays.tostring (a)); }}
Five, the allocation class sort
1. Multi-Keyword ordering:
Stay
2. Chain-Base sorting:
Thought: First allocation, then collection, is to first follow a keyword collection, and then collect (the first sort), and then another keyword to assign a new sequence, and then collect it, and then complete a sort, so that all the keyword allocation after the collection, the completion of the sorting.
Time complexity: T (n) = O (d (n + rd))
Space complexity: S (n) = O (RD)
Stability: Stable sequencing
Summarize the various sorting algorithms "Java implementation"