- 在线时间
- 0 小时
- 最后登录
- 2004-7-22
- 注册时间
- 2004-5-28
- 听众数
- 1
- 收听数
- 0
- 能力
- 0 分
- 体力
- 124 点
- 威望
- 0 点
- 阅读权限
- 20
- 积分
- 43
- 相册
- 0
- 日志
- 0
- 记录
- 0
- 帖子
- 4
- 主题
- 7
- 精华
- 0
- 分享
- 0
- 好友
- 0
升级   40% 该用户从未签到
 |
< >
7 L8 |6 x" k! L% H5 Q) ]#include <malloc.h>
# |& r8 O* l i, f- g1 y#include<stdio.h>& K P$ d5 n# Z, Y
#define N 111 k" F! q. V& B4 t' \! t
/*用监视哨查找*/
9 _& Y( h- l4 Kint search(int array[],int n,int k)
3 f$ E6 ]8 i J. ^5 q. s) _{int i;: }2 o, P' _; Y2 w! a
i=n-1;
& N$ ~& ~4 n* G+ `! x1 ?7 R$ rarray[0]=k;6 X5 ]! m5 C4 Q5 c3 T9 G
while(array!=k) i--;
( {* {. \6 I; ^ Xreturn(i);
/ [0 h% A g# I; ~* i5 O* g}9 I5 i* h H9 `0 ^* |% Q9 M# A
/*折半查找法*/- j) i) i4 h, j* z; Y0 o5 V) \- D. ]
int halfsearch(int array[],int n,int k)
+ v1 _3 m& ^4 X- h- b- Y- {+ E{int i,j,mid;
+ ?2 k/ x$ R; t8 t `7 I# G i=1;j=n;
2 j `8 R, q! a* L" o3 F+ W6 K3 q" zwhile(i<=j)2 |2 X5 Y7 v5 k H" ^
{mid=(i+j)/2;) q }9 F4 n1 e1 i2 r _5 B1 K
if(k==array[mid]) return(mid);
6 u4 A [$ {( B1 }) Xelse if(k<array[mid]) j=mid-1;
8 U) x2 V3 Z# C9 \1 H) ` else i=mid+1;6 Y" W- Z& @% ~2 i0 }# \% t1 D7 E
}
: W w' _- j( v* b, I9 mreturn(0);$ K& t4 a, S! m' Y
}</P>. O$ }: ^" ?3 [/ n+ P( ~( |1 g
< >/*冒泡排序法*/( Y& i; }9 @8 z9 [5 m
void mpsort(int array[])
4 z+ S6 c* G* b# }3 D# q4 ~{int i,j,a;
/ j$ G6 \; C/ K7 n# c% o! c: |a=0; u" z9 K R+ s, a& L2 g* r7 l
for(i=1;i<N;i++)
9 K) a: m6 d( A: f! p2 r6 g for(j=i+1;j<N;j++)1 @5 R) I! [; y, W& n- X4 d! \* i# ?# O" h
if(array>array[j])! s0 Q) x; z) d" H+ X
{a=array;
# H: }3 i1 {+ h( ~" y array=array[j];' A0 ~( j2 P/ E. Y- X9 M. g0 G7 t; j! Q
array[j]=a;}
) E7 t/ u, {& Q3 |}' \' H6 u! l/ P
/*直接插入排序*/
! s7 V. W/ D0 S: w( qvoid insertsort(int array[])! h; t* |# R8 p% N( V- k$ H8 B
{int i,j;
! v! I, Y. B3 C/ J& {$ d( p2 y for(i=2;i<N;i++), v: u9 J \: d9 k$ N
{array[0]=array;. K) A, M) o1 J2 z
j=i-1;
5 W! |9 v; W! D# Uwhile(array[0]<array[j])
) i5 E0 C$ W! L5 r8 Y {array[j+1]=array[j--];: X5 S2 A) N0 n2 y) B. O, v% W
array[j+1]=array[0];
' E* V& s/ z% G}
2 w: f; x# {1 S: I" K}+ A' s& Q$ H2 q0 k& S0 k
}9 K1 k6 ^6 }3 j0 M2 h
/*建立*/1 p: E: t" _7 k) @* [ w
void creat(int array[])" a9 U' ^ r4 F$ d! ^
{int i;
: [2 F( m0 l2 ]' I$ A printf("enter the array:\n");2 w8 h4 f9 k4 r% u
for(i=1;i<N;i++)
8 Q6 o- ^% K5 G scanf("%d",&array);1 C6 z0 H& \2 z8 e( l w$ L
}</P>( m7 A1 W8 x; C* G( A" z1 X0 O
< >/*显示*/
; V7 `# u1 J$ z( R2 C8 mvoid print(int array[])4 B; T) _2 O% E. R5 @
{int i;0 ~& Y5 N* G' j
printf("The numbers after sort is:\n");* ^1 K3 N! D) D) y2 l
for(i=1;i<N;i++)4 B3 k) _) a& j# \
printf("%d ",array);2 o4 v/ z% e" f6 q9 P% U9 S
printf("\n");
. M! G" S4 r1 g7 f2 H }</P>
) \. T- g& I: C< >* }6 R' ]. w7 W$ `* o
main()1 f" ]- a/ ]9 U" E9 o! y7 k, Y
{int a[11],i,x,chang;/ c0 K$ U+ J% Z4 z1 | T$ T" o8 J- u
/*printf("enter the array\n");
' a7 o8 G) s/ t4 i% r } for(i=1;i<11;i++)
0 |) l3 p. {: f scanf("%d",&a);*/</P>& h2 d; c' b. U1 s3 z
< >aga:
/ v4 }6 O8 g: G, y' W 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");
8 {' D/ O/ E+ G; c) {; K/ Q scanf("%d",&chang);" c0 m' _- [, L0 v8 O; S& i
switch (chang)+ z. [& y1 {2 U& ?! ]! R; q9 @
{case 1:, s; M) k$ x+ s& v
{creat(a);+ S- N9 `. u/ N* s9 ?
printf(" lease enter the search number:\n");6 k) f# ~ S3 j8 [9 V# D
scanf("%d",&x);# U/ k" Q; v; |% }- H
printf("The number station is:%d\n",search(a,N,x));
2 n4 p, r h9 ~ x, }; R& y& Q goto aga;
; s; u8 p# N* r+ { }: Z0 k; Z* T$ g7 n
case 2:
$ c8 ^8 e2 ~# V% f4 L2 Z { creat(a);
: m t$ c% c# {( [ insertsort(a);
; ~; p1 \) c( ?0 M" Q print(a);% l' q# n9 Q7 K: ^. X! T
printf(" lease int the search number:\n");
! \% C% r E6 ~0 N6 Q5 R$ x8 w4 I scanf("%d",&x);
, i1 b; d0 Y8 B `. w; P, e printf("The number station is:%d\n",halfsearch(a,N,x));
% k/ s8 Z3 H# ^9 M) R goto aga;
) N! b: s! W' x( X2 w6 t }
4 x/ V: ]# ?0 h4 G) H/ p case 3:
5 _: P+ R4 c& l {creat(a);
% s1 u, q$ N$ T, y, U insertsort(a);4 }" x* L) R6 Q
print(a);
9 ?% x( H7 c' U7 x5 { i9 {& Q1 F goto aga;3 l8 Z8 }4 x/ b- Y' H1 h
}</P>, D$ e5 q3 z7 C: ^7 j. i8 @
< > case 4:
2 h" F$ [. F' K8 {) P {creat(a);7 p2 ~8 ?9 N* j
mpsort(a);
8 F. I' R& f0 b6 z7 F& T print(a);7 k, l2 g5 u# r4 p7 v* d
goto aga;* Q) G6 U2 b; t# ]. |+ L
}</P>
R' @. J# v* N< > case 5:{ printf("exit!\n");break;}' ?* R0 r9 W! N4 s, j3 X
default:{printf("Error!\n"); goto aga;}; T- ~7 z# F& y+ s ^0 P
}" p8 p7 {& `, l: H0 E: Q1 ~
}
% E6 ^' J, p3 ?) n4 {0 p. L ) F1 o0 N( A/ ?/ \: g
0 \# X; I' f8 Q, z. Y! j7 }/ Z
</P>
, |( n7 R& i% a0 M3 Y+ P# \[此贴子已经被作者于2004-6-3 12:16:43编辑过] |
zan
|