Insert sorting directly and select sorting directly

Source: Internet
Author: User

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 );
}
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.