First, bubble sort
Thought: N number of loops to find the maximum (or minimum) value, and then repeat the comparison to find the maximum value in the remaining number
Code:
Public Static void Bubblesort (int[] numbers) {
int temp;
int k=numbers.length;
boolean Didswap;
for (int i=0;i<k-1;i++) {
Didswap=false; When the rounds are not exchanged, the instructions are in order.
for (int j=0;j<k-i-1;j++) {
if (Numbers[j]>numbers[j+1]) {
TEMP=NUMBERS[J];
NUMBERS[J]=NUMBERS[J+1];
Numbers[j+1]=temp;
Didswap=true;
}
}
if (didswap==false)
return;
}
}
optimal Time Complexity | O (n) Worst | N (n-1)/2-->o (n2)
Space complexity O (1)
Second, choose the sort
Thought: N number of loops to find the maximum (or minimum) value, and then repeat the comparison in the remaining number to find the maximum value, the number of comparisons with the same amount of bubbles, reducing the number of exchanges.
Code:
Public Static void Selectsort (int[] numbers) {
for (int i=0,len=numbers.length-1;i<len;i++) {
int min=numbers[0],min_id=0;
for (int j=i;j<numbers.length;j++) {
if (Min>numbers[j]) {
MIN=NUMBERS[J];
Min_id=j;
}
}
Numbers[min_id]=numbers[i];
Numbers[i]=min;
}
}
Time complexity N (n-1)/2-->o (n2)
Space complexity O (1)
Third, insert sort
Thought: Each loop comparison inserts a number into an ordered sequence (one that is ordered), which is compared from the second number.
Code:
Public Static void Insertsort (int[] numbers) {
for (int i=0;i<numbers.length;i++) {
int currentnumber=numbers[i];
int j=i-1;
while (J>=0&&numbers[j]>currentnumber) {//Interrupt condition period is better than bubbling, interrupt expectation is LENGTH/2, so 1 time times more than bubble block
NUMBERS[J+1]=NUMBERS[J];
j--;
}
Numbers[j+1]=currentnumber;
}
}
optimal Time Complexity | O (n) worst |n* (n-1)/2-->o (n2)
Complexity of Space O (1)
Iv. sort of Hill
Thought : According to the insertion algorithm each time self-can move one of the shortcomings of the addition of the interval value DK (=LENGTH/2), each round compared to the DK data placed in a group of direct insertion sorting, in the reduction of the DK repeat cycle, until the DK is 1.
Code:
Public Static void Shellsort (int[] numbers) {
int DK=NUMBERS.LENGTH/2;
while (dk>=1) {
Shellinsertsort (NUMBERS,DK); log2n
DK=DK/2;
}
}
Public Static void Shellinsertsort (int[] numbers,int dk) {
for (int i=dk;i<numbers.length;i++) {//all combinations separated by DK
if (NUMBERS[I]<NUMBERS[I-DK]) {//Direct insert sort for each combination
int x=numbers[i];
int j=i-dk;
Do {
NUMBERS[J+DK]=NUMBERS[J];
J=J-DK;
}
while (J>0&&NUMBERS[J]>X);
Numbers[j+dk]=x;
}
}
}
Time complexity O (NLOG2N)
Space complexity O (n)
Five, merge sort
Thought: Divide and conquer thought + merge two ordered sequences. Loop the array in two minutes until only one set of data begins to merge upward.
Code:
Public Static void sort (int[] numbers) {
Sort (numbers,0,numbers.length);
}
Public Static void sort (int[] numbers,int pos,int end) {
if (end-pos>1) {
int mid= (end+pos)/2;
Sort (numbers,pos,mid);
Sort (numbers,mid,end);
MergeSort (Numbers,pos,mid,end); LOG2N Times
}
}
Public Static void MergeSort (int[] numbers,int pos,int mid,int end) { int len1=mid-pos,len2 =end-mid;
int [] array1=new int[LEN1]; ?
int [] array2=new int[LEN2]; ?
System.arraycopy (Numbers, POS, array1, 0, LEN1);
System.arraycopy (Numbers, Mid, array2, 0, len2);
for (int i=pos,j=0,k=0;i<end;i++) {
if (J==LEN1) {
System.arraycopy (Array2, K, numbers, I, len2-k); break;
}
if (K==LEN2) {
System.arraycopy (Array1, J, numbers, I, len1-j); break;
}
if (Array1[j]<=array2[k]) {
Numbers[i]=array1[j++];
}
Else {
Numbers[i]=array2[k++];
}
}
}
Time complexity O (N*LOG2N)
Complexity of Space O (n)----> The spatial complexity of the above code is nlog2n, you can request an array of n before recursion
instead of the code indicated by the Red arrows above, you can reduce the complexity of the space O (N)
Six, quick sort
thought: Divide and conquer set Sentinel, will be larger than the Sentinel data on one side, small points to one side, and then recursive repetition.
Code:
Public Static void quickSort (int[] A,int left,int. right) {
if (Left<right) {
int Key=a[left]; Sentinel
int low=left;
int high=right;
while (Low
while (Low
high--;
}
if (Low
A[low++]=a[high];
while (Low
low++;
}
if (Low
A[high--]=a[low];
}
A[low]=key;
QuickSort (a,left,low-1);
QuickSort (A,low+1,right);
}
}
optimal Time Complexity | O (nlog2n) Worst | O (N2)
Complexity of Space Best | O (log2n) Worst | O (N)
Vii. sequencing of Heaps
Thought: First the data is initialized to a large heap of top (or small heap top) of the two-tree, then each round to remove the heap top, and then the last fulcrum assigned to the heap top. Large piles of top are destroyed, and the loops are adjusted to get a large heap of top, so loop until an orderly sequence is obtained.
Code:
Public Static void Heapsort (int[] numbers,int length) {
Buildingheap (Numbers,length);
int len=numbers.length;
for (int i=len-1;i>=0;i--) {
int temp=numbers[i];numbers[i]=numbers[0];numbers[0]=temp;
Heapadjust (Numbers,0,i);
}
}
Public Static void Buildingheap (int[] numbers,int length) {
for (int i=length/2;i>0;i--) {
Heapadjust (Numbers,i-1,length);
}
}
Public Static void Heapadjust (int[] numbers,int s,int length) {
int Tmp=numbers[s];
int child=2*s+1;
while (child<length) {
if (Child+1<length&&numbers[child]<numbers[child+1]) {
++child;
}
if (Numbers[s]<numbers[child]) {
Numbers[s]=numbers[child];
S=child;
child=2*s+1;
}Else{
break;
}
numbers[s]=tmp;
}
}
Complexity of Time nlog2n
Space complexity O (1)
Eight, barrel sorting
Thought: For the uniform distribution of the sequence is good, the N data pattern of its range into m-groups, and then the groups are sorted by other sort.
Summary of basic sorting algorithm