- 在线时间
- 0 小时
- 最后登录
- 2004-7-22
- 注册时间
- 2004-5-28
- 听众数
- 1
- 收听数
- 0
- 能力
- 0 分
- 体力
- 124 点
- 威望
- 0 点
- 阅读权限
- 20
- 积分
- 43
- 相册
- 0
- 日志
- 0
- 记录
- 0
- 帖子
- 4
- 主题
- 7
- 精华
- 0
- 分享
- 0
- 好友
- 0
升级   40% 该用户从未签到
 |
< >
" n& j. ~4 t: r( u4 s#include <malloc.h>8 ~) E k1 k$ U; v6 D3 u
#include<stdio.h>2 M4 D7 `* p& a; h/ T5 s+ y
#define N 11+ `# e* Z1 M7 S
/*用监视哨查找*/$ Z2 ^0 \7 I1 m( f6 B! q! @
int search(int array[],int n,int k)9 b5 k+ x: l7 h) Y f. W; _
{int i;
3 q9 L4 b6 W7 Z1 C# t i=n-1;
+ K w' I9 O2 f! ^1 A3 J/ C* M, Narray[0]=k;' f! v) b7 n+ u$ w$ h8 K& W
while(array!=k) i--;
( a; \* j. a5 C. oreturn(i);4 ?, O& s. u3 M; \
}
: ^" ]8 J2 w O4 d$ ^3 `/*折半查找法*/
: ~9 a, c- c% X: I$ [8 kint halfsearch(int array[],int n,int k)
; I6 H5 I) C8 [9 c, _5 v{int i,j,mid;( _& {* L8 e5 Y" U1 o$ F( d8 q
i=1;j=n;! b# B& w8 C" @ K$ L/ y8 U. k
while(i<=j)
2 x' `3 L, ]: X& V( s* G5 u{mid=(i+j)/2;
/ P: a$ n' O3 r3 m2 Y$ o if(k==array[mid]) return(mid);
* `' F* t" u' Z# }0 telse if(k<array[mid]) j=mid-1;
9 S* h1 m. V3 v+ E' t$ A else i=mid+1;, \) M& z ?8 Y8 w9 T4 }7 g
}
4 k0 R' x. F4 Y0 I' Z& l% j/ Ureturn(0);
+ m5 \8 `% e2 q( F/ p}</P>1 T a9 Q* |) l+ m4 f3 ]& x
< >/*冒泡排序法*/
* s; o, ?" H/ c6 p$ u1 A/ h5 N# U \void mpsort(int array[])4 ]+ z8 C7 S: O- z+ ?$ H( I, M
{int i,j,a;! m# ]9 C4 \$ E) N& \
a=0;
' g5 p" x0 h3 H# @/ G for(i=1;i<N;i++)
, U, P+ }, N r+ t r for(j=i+1;j<N;j++)
: s, _4 k4 p b& j6 v1 {8 X7 j2 ` if(array>array[j])% _. r) V; ~+ |1 o& w5 A* f
{a=array;& z% o; v. b' t6 Z" v
array=array[j];3 C8 n& f/ \) O
array[j]=a;}8 m+ [. p0 {1 W2 c7 X* s# r
}8 g" S, L/ M' }( ^. X+ [; V, A% c
/*直接插入排序*/
4 Z' p6 l# x/ z3 L9 w% Fvoid insertsort(int array[])
* Q) N9 G& `/ Z% ?7 y9 v2 {3 |{int i,j;" L. ]' S2 o5 W8 q% d
for(i=2;i<N;i++)4 D/ q& c3 y: ]% W" F. M( {
{array[0]=array;( ^* |/ P; h, c d6 F1 {
j=i-1;
" ` z$ u* v/ n5 pwhile(array[0]<array[j])7 r( O/ k$ e! E& E
{array[j+1]=array[j--];
8 b3 n u. Y8 R* E0 S array[j+1]=array[0];- H: Q; G9 }* B
}
3 a6 O' v+ v" V) l9 H" `}
+ G% M2 n: ~. Y}* b' }; m) [6 n2 M* E
/*建立*/4 T' g/ m2 L! O/ K& j: t
void creat(int array[])
3 y5 n( a- y9 A* `' h{int i;6 k' @+ |$ C- z( y7 [( x# Z
printf("enter the array:\n");& A- }; _2 `7 G/ U' }( x: I
for(i=1;i<N;i++): v% N7 l9 H$ \5 @! }# p; f- I
scanf("%d",&array);) U3 z5 W8 V" ?9 x. \$ I) n5 Z
}</P>, Z0 B# a0 b( B9 G T% B
< >/*显示*/
5 A! `* Z2 x* d4 [2 Y4 c) e3 U0 Fvoid print(int array[])
; q( s: l- i6 N/ I9 R) G2 D {int i;1 d, ^% }( w7 [' @$ l
printf("The numbers after sort is:\n");' y: e. b; p/ d& w- ^% g
for(i=1;i<N;i++)/ E. W. U U* U" c/ h5 y) w
printf("%d ",array);
9 Q' G5 { j9 T- P( Z' y: x printf("\n");
* N* N& u& \7 ~9 d+ M }</P>5 A, D) |+ x }9 R% L. r5 b
< >% z/ t, l4 ~& I! s
main()
x" R- D5 L4 V( E{int a[11],i,x,chang;
/ Z: t" S4 u' i' H2 l2 h /*printf("enter the array\n");
& m9 O0 d7 O' _& C) ?& T( {# E for(i=1;i<11;i++)
8 u) a( _( ^2 W scanf("%d",&a);*/</P>& A" U( s# \* L- ]8 B) M
< >aga:; u: _- u6 w( e
printf("\nchang:1: use watching method finding\n 2:use half method finding\n 3: use directness intsert method sort\n 4:use bubble up method sort\n 5:exit\n");
, _- y8 z; K- y scanf("%d",&chang);
7 b7 {* K! |" p1 r switch (chang)) ]) F' j& G7 [( \) r4 S
{case 1:9 t3 L. g# b" f$ q0 L5 k4 w3 g9 o; c
{creat(a);0 q @3 s3 E/ d" U
printf(" lease enter the search number:\n");6 c/ m' J. w. W& y9 N7 X
scanf("%d",&x);# t) T; K4 O) @' o* U: R
printf("The number station is:%d\n",search(a,N,x));
1 x# A6 P, W- Z% b goto aga;
3 \& L" S- ^% R# z }0 C' D1 E) @, _+ W7 Y
case 2:" ~: y6 |5 D; x N
{ creat(a);# t+ m; {! F4 L( W" p1 N! k. o
insertsort(a);
5 J. C: ]8 {& V print(a);
; T- K1 k* n/ ~1 I! @; I printf(" lease int the search number:\n");
4 v) E8 m0 s9 [, \4 M scanf("%d",&x);) u0 `8 _# j' K, V- [8 P
printf("The number station is:%d\n",halfsearch(a,N,x));
: [3 y& K) ]7 p goto aga;
7 |1 P w* h: ]" Y7 V }& d9 M8 W- `6 c& R
case 3:6 y" r9 c" j# M
{creat(a);
5 |" d' ^2 `2 w: N: q* d3 e. R insertsort(a);
, `+ L t% l6 \' ? print(a);# t+ ?& |' E. ]; F. m1 o! ], ]
goto aga;1 G9 S9 f) B1 M( L
}</P>
9 q7 t7 H- U& m! B! z9 B< > case 4:
- k: E% [+ \2 V6 O {creat(a);
7 H4 @7 K/ m- [) I& g/ X mpsort(a);5 p6 s4 p+ {& e
print(a);" v* z- ]$ t, _$ C
goto aga;
+ D) X" ]9 o2 a }</P>: u2 N: z" J" `/ |! @
< > case 5:{ printf("exit!\n");break;}
2 e4 m1 ?2 r7 q# L* N) k, f default:{printf("Error!\n"); goto aga;}4 Q+ u4 x) ~. T: k
}
/ S% H+ C! E1 O) C- R* i" R}, L; L ^9 u& J. d5 X
# t' L; y% Z0 {. S) J _" R W # R+ k* ?9 x! W N$ L* B
</P>4 J$ E8 e. V3 M, u4 U1 s }
[此贴子已经被作者于2004-6-3 12:16:43编辑过] |
zan
|