- 在线时间
- 0 小时
- 最后登录
- 2004-7-22
- 注册时间
- 2004-5-28
- 听众数
- 1
- 收听数
- 0
- 能力
- 0 分
- 体力
- 124 点
- 威望
- 0 点
- 阅读权限
- 20
- 积分
- 43
- 相册
- 0
- 日志
- 0
- 记录
- 0
- 帖子
- 4
- 主题
- 7
- 精华
- 0
- 分享
- 0
- 好友
- 0
升级   40% 该用户从未签到
 |
< >
7 [8 H% X$ C' `- f. Y#include <malloc.h>* P* k& G: T" b7 U# h
#include<stdio.h>
% k8 G- N: x1 _- ~+ q5 j: ~#define N 11
: L& L- R$ W' m4 Y* d/*用监视哨查找*/+ W4 t1 x$ S; m" J. }9 R
int search(int array[],int n,int k)* w& g! e! k' t; n; M ^- ~. ?
{int i;
1 z0 m& L' y H: A! ^# @ i=n-1;
+ C" d& n5 h5 J, n4 Z! j' [% parray[0]=k;
1 C( X+ _9 p4 n2 s6 @; _. b" o" S4 E$ Vwhile(array!=k) i--;
8 M2 R5 e/ }0 f+ m+ \return(i);3 {1 U9 l" x9 I
}* j# N7 f. X W( K5 g ^
/*折半查找法*/. _! n6 h5 p" T- s9 a' J8 I. j2 F: L1 a: b
int halfsearch(int array[],int n,int k)
& e0 k. G. x C+ y. m5 z{int i,j,mid;
; s: Z+ {. S! ^- N# U: | i=1;j=n;
" j2 f0 ~; f7 e8 E9 c7 o. Jwhile(i<=j)9 P0 s* T+ Z" c j4 S) Y9 E' `
{mid=(i+j)/2;' L$ }+ z3 c. \$ D
if(k==array[mid]) return(mid);
5 M( V2 i/ C1 @0 `0 \3 G+ Gelse if(k<array[mid]) j=mid-1;$ A# z3 V: r/ w6 N; Y9 ]
else i=mid+1;
4 Y9 M$ B2 y+ b7 c% m W}& n5 w T9 \7 Z/ j3 p
return(0);3 ~% y8 b/ w6 u. H" C& O
}</P>& @" a5 V7 t3 M) X
< >/*冒泡排序法*/+ L4 k% K, G% ]- V( W, v3 `
void mpsort(int array[])0 e6 e$ T2 @1 h8 D8 a
{int i,j,a;
1 ~. x; x+ E8 D1 N0 n) ua=0;
, l) }. v5 _3 Q* B' F for(i=1;i<N;i++)' F+ e" W0 E" w) D
for(j=i+1;j<N;j++)1 t, Y6 t& ~1 s+ p c. `: Y
if(array>array[j]), A& B, Y: }( E# W8 s. r- A. M
{a=array;1 s+ S2 L# _7 R) v# p/ U5 L2 ?5 F' C
array=array[j];
* g& ?# Q$ w% L* T' d array[j]=a;}% m% ?9 J: N& d
}
- {+ Y! S7 d" r J7 R+ o/*直接插入排序*/
4 V% Z0 B. E! u0 N: ^7 y) yvoid insertsort(int array[])3 e3 u$ e6 y' u* _ M
{int i,j;4 o0 n3 A3 U9 I2 f
for(i=2;i<N;i++): ^* E7 C5 J7 Z0 v
{array[0]=array;: W# i8 l1 z8 z5 _
j=i-1;
- Z8 l; z' Z0 ~while(array[0]<array[j])
/ m3 s! R1 t) L/ H! i! }2 e0 d {array[j+1]=array[j--];
% h S# N y* O array[j+1]=array[0];6 `1 f: u) \1 d3 e
}
3 T1 d X' q; B& k+ j A}
0 g( t/ t# n1 I: h}
3 D9 t2 u; }. A8 F p) M [1 I4 H/*建立*/" O7 a7 y4 X: H
void creat(int array[])
/ N1 e' Y4 j* M% N5 Z{int i;, c- k9 X6 X) S: a
printf("enter the array:\n");
& t& G y+ m+ ]+ \+ n0 a4 H. V8 ` for(i=1;i<N;i++)& k3 r6 S4 p* M5 \/ l
scanf("%d",&array);
" k. t, ?6 n% S0 F$ @) @8 [* ~' {$ ~}</P># u% T/ A$ R m
< >/*显示*/
+ q# \8 b9 x$ {' \: r& hvoid print(int array[])
, K; |% y6 [0 h. P. P {int i;
9 }8 c4 S9 h0 f5 i j1 G$ { printf("The numbers after sort is:\n");7 F! p: o+ N4 _& D2 v9 ^& W) p$ d+ `/ U
for(i=1;i<N;i++)5 C# H% {8 H6 J" J: L7 T
printf("%d ",array);7 E- N2 S1 g% X. v
printf("\n");
- ^. r0 r' F; T/ o. s* [! V7 V }</P>
6 W4 A3 c) P6 G. m< >. S" ^) G# i+ m- j1 W
main()) g( ]6 D8 d2 ]$ R0 ~
{int a[11],i,x,chang;
' L! ?, v; H$ `+ P# B /*printf("enter the array\n");
" J( D% K/ E- N0 L g4 f) ~5 W$ V for(i=1;i<11;i++)5 F& L! a; y$ g( u0 ]" K% I. Y
scanf("%d",&a);*/</P>* L& _2 Q$ P' c0 q
< >aga:7 z9 J; z; _* ~4 U2 p" E$ w, l
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");- ^! d1 R: ~- Y% W8 K
scanf("%d",&chang);
4 a( o L5 o o0 C/ S/ }* z" w switch (chang)
6 W# v: l( m; S& _2 ? {case 1:
4 C* d- n# y( T$ T& `3 O9 q {creat(a);
! e i k5 z2 C printf(" lease enter the search number:\n");
3 K V- B1 w0 r scanf("%d",&x);$ e& R" T! ^+ Q+ V" s, d$ A( B
printf("The number station is:%d\n",search(a,N,x));
. k" G0 |/ K* @, {- d4 s goto aga;
# V$ \/ k! {$ Q1 ~. p/ U }3 I& I$ s1 ~- f: H& O/ r) G6 t
case 2:
& x+ l j2 d5 J! `; a! ` { creat(a);/ k2 w5 n: b* K% Q
insertsort(a);
2 W% B! j. M. i( t1 \! j print(a);, j9 {# |$ v- E; U6 Q1 R1 u
printf(" lease int the search number:\n");; q! I: ^: i% Q- Q% J: \* R
scanf("%d",&x);! I! d4 M1 i5 M; s0 k L9 t& ?" u g
printf("The number station is:%d\n",halfsearch(a,N,x));
$ R8 x, H, u; ? goto aga;: ]; K& c2 \( E4 @. ]4 U5 b
}
1 V5 [$ a8 |. o4 e I case 3:% }0 U; _% x/ _
{creat(a);- j% L |4 }# {4 R) t
insertsort(a);
- _1 t& a( ^3 [( L print(a);
- H) k9 q. x' [3 P/ r6 v goto aga;
; m9 S# o$ R! [ }</P>
& A( X8 i" A; ]/ E! ?& |# L& a< > case 4:' _- [- Z) X. h& I* Y. G, v9 \
{creat(a);, z2 G) |" d1 y4 H+ v0 |
mpsort(a);" u7 B6 p6 X3 ]: ^# E8 p) t
print(a);% l2 Z8 Q1 d5 C) O6 Q& n
goto aga;* u5 F2 j" D X7 o* ~
}</P>
, J% h3 k' B; ~7 c5 J. J( T' P) [< > case 5:{ printf("exit!\n");break;}
) Q8 Q" R. F4 F/ y- u default:{printf("Error!\n"); goto aga;}6 b0 b: H# L0 R
}
6 }4 [% e) z4 O; v}6 b6 F/ P. g& v9 |& i
" j( O9 I4 s V5 Q m7 @* `% ]: q
' }; h* J5 X0 Z' P% z* p2 t</P>6 ^* b6 y4 R' n, q p
[此贴子已经被作者于2004-6-3 12:16:43编辑过] |
zan
|