C language for bubble and direct insertion sorting

Source: Internet
Author: User

# Include "stdlib. H"
# Include "stdio. H"
# Deprecision MAX 1000
Void print (int A [], int N ){
Int I = 0;
Printf ("the last:/N ");
While (A [I]! = (-1) {// print
Printf ("% d/N", a [I]);
I ++;
}
}

Void bubble (int A [], int N) {// sort by bubble
Int I, j, temp;
For (I = 0; I <n-1; I ++)
For (j = I + 1; j <n; j ++ ){
If (A [I]> A [J]) {temp = A [I]; A [I] = A [J]; A [J] = temp ;}
}
Print (A, N );
}

Void direct_insert (int A [], int N) {// sort by insert
Int I, j, temp;
For (I = 1; I <n; I ++ ){
Temp = A [I]; j = I-1;
While (temp <A [J] & J> = 0 ){
A [J + 1] = A [J];
J --;
}
A [J + 1] = temp;
}
Print (A, N );
}

Main (){
Int A [Max];
Int I, B;
For (I = 0; I <Max; I ++) {// init
A [I] =-1;
}

I = 0;

While (B! =-1) {// save numbers using an array, set a number for a way out
Scanf ("% d", & B );
A [I] = B;
I ++;
}

I = 0;
Printf ("the form:/N ");
While (A [I]! = (-1) {// print
Printf ("% d/N", a [I]);
I ++;
}
Bubble (A, I); // maopao sort
Direct_insert (A, I); // direct insert sort

Int K;
Scanf ("% d", k );

}

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.