Insert sorting directly and select sorting directly
Direct insertion sorting is a simple sorting method. Its principle is similar to that when we play cards, cards in our hands are arranged in a certain order, then, you can find the cards in order and insert them into the place where they should be inserted until the last card is inserted. The cards in your hands are sorted.
This is also true for the direct insertion and sorting of a series. The first number is regarded as an ordered series, and the subsequent numbers are inserted into the series in sequence:
The Code is as follows:
void _insert_sort(int *A,int size){int temp=0;for(int i=1;i<size;i++){if(A[i]<A[i-1]){temp=A[i];int j=i-1;for(;j>0 && A[j]>temp;j--){A[j+1]=A[j];}A[j+1]=temp;}}}
Urgent Program Design: Direct insertion sorting
I will use C ++ to help you implement it!
# Include <iostream. h>
Void insertsort (int a [], int n) // insert sorting
{
Int I;
Cout <"Insert before sorting" <n <:";
For (I = 0; I <n; I ++)
Cout <a [I];
Cout <endl;
For (I = 1; I <n; I ++) // compare the number of workers
{
Int temp = a [I]; // defines a temporary space for storage.
Int j = I-1;
While (j> = 0) & (temp <a [j])
{
A [j + 1] = a [j]; // move back
J --;
}
A [j + 1] = temp;
}
Cout <"inserted sorted" <n <:";
For (I = 0; I <n; I ++)
Cout <a [I];
Cout <endl;
}
Void main ()
{
Void insertsort (int a [], int n );
Int m, B [100];
Cout <"number m = ";
Cin> m;
Cout <"enter" <m <"count ";
For (int I = 0; I <m; I ++)
Cin> B [I];
Insertsort (B, m );
}
Urgent Program Design: Direct insertion sorting
I will use C ++ to help you implement it!
# Include <iostream. h>
Void insertsort (int a [], int n) // insert sorting
{
Int I;
Cout <"Insert before sorting" <n <:";
For (I = 0; I <n; I ++)
Cout <a [I];
Cout <endl;
For (I = 1; I <n; I ++) // compare the number of workers
{
Int temp = a [I]; // defines a temporary space for storage.
Int j = I-1;
While (j> = 0) & (temp <a [j])
{
A [j + 1] = a [j]; // move back
J --;
}
A [j + 1] = temp;
}
Cout <"inserted sorted" <n <:";
For (I = 0; I <n; I ++)
Cout <a [I];
Cout <endl;
}
Void main ()
{
Void insertsort (int a [], int n );
Int m, B [100];
Cout <"number m = ";
Cin> m;
Cout <"enter" <m <"count ";
For (int I = 0; I <m; I ++)
Cin> B [I];
Insertsort (B, m );
}