- 在线时间
- 0 小时
- 最后登录
- 2004-7-22
- 注册时间
- 2004-5-28
- 听众数
- 1
- 收听数
- 0
- 能力
- 0 分
- 体力
- 124 点
- 威望
- 0 点
- 阅读权限
- 20
- 积分
- 43
- 相册
- 0
- 日志
- 0
- 记录
- 0
- 帖子
- 4
- 主题
- 7
- 精华
- 0
- 分享
- 0
- 好友
- 0
升级   40% 该用户从未签到
 |
< >
! E9 i* M- U+ a5 ~$ p#include <malloc.h>3 @; `0 t( K2 @. Q3 o6 C R- {* _
#include<stdio.h>
! U8 B8 S$ z; p- u/ N#define N 11% e+ j5 q3 \( _8 p- Z; e5 {0 t {
/*用监视哨查找*/
) g0 z, q" r/ K9 `0 I$ Eint search(int array[],int n,int k)
6 m" ^8 Y5 t6 m" x% y{int i;: j6 L% P9 h8 R" a' Q+ k% i
i=n-1;0 n( Q6 Z4 B1 v8 R; x, S/ B
array[0]=k;# x1 K9 ?- S2 ^6 O6 X, H+ ` ^$ `
while(array!=k) i--;( V* Y( ~, @9 V0 y. U
return(i);( S* P2 B/ w1 ?, q6 j$ {) ?2 E
}
$ x3 F5 g+ T; E! r/*折半查找法*/3 [5 ?) `/ `& o- }$ }, Q4 ?! O
int halfsearch(int array[],int n,int k) ^8 ]% H8 t( e
{int i,j,mid;
. p% V. ~( x$ E4 J* y0 t" { _" R i=1;j=n;) b; ^5 }% Y8 E' K7 x; b5 T: ~
while(i<=j)
j W6 y+ Q5 X3 {8 v# s; f& w7 d{mid=(i+j)/2;
% `5 w. k# z4 R if(k==array[mid]) return(mid);
' e7 x- ~* {5 r6 jelse if(k<array[mid]) j=mid-1;# b5 k5 ?( z% W# V
else i=mid+1;2 X, S* g/ J, _/ A; i$ T; r+ ?
}
7 B7 n; u, ?" `; x6 `6 r- U6 ?8 areturn(0);
" B7 |3 g) S% G}</P>' }& \, X8 L- J, a
< >/*冒泡排序法*/
7 Q- c. Z# k$ xvoid mpsort(int array[])
) x2 V+ m* f1 Z3 [ o4 W# Z* w1 N{int i,j,a;5 P5 m& `$ I5 K; ^2 E7 E
a=0;
* S+ t% ]9 D5 D, l* h for(i=1;i<N;i++)5 H3 P- U0 a5 s0 i/ G; B; [
for(j=i+1;j<N;j++)2 Q! n+ s0 y7 h7 b9 Y A c9 {, J
if(array>array[j])
) s/ b: w' Q7 |3 A; d6 u {a=array;
4 T, l. ` ?! [: |/ _0 d, [) [" z; L array=array[j];+ _# s N: C# g; S; V+ ?: C' ^; x
array[j]=a;}
! ^ R- d! D. ^ k/ O3 N}
3 h. a! h+ R) V/*直接插入排序*/2 u& S# F* ?* G/ X- r, J
void insertsort(int array[])
, t }3 w# u2 v7 q' R{int i,j; c8 S/ [! x' M g$ G1 U: N. B
for(i=2;i<N;i++)+ j+ `; F2 ]3 h7 _* L/ N5 ?
{array[0]=array;3 I/ _) s7 U( p/ ]& C% w" r
j=i-1;. a! A; e; u4 ]* M$ z; w
while(array[0]<array[j])* B0 }" t5 N: `
{array[j+1]=array[j--];
: ] w1 W+ |, ?+ U) B$ D' j2 Z array[j+1]=array[0];
8 _6 U( f8 x6 A8 ~}
! B2 u+ v+ x" @4 q}' z$ c6 P% c7 \* P3 d
} ]5 ?; Q2 \0 M7 j1 E0 h( @
/*建立*/9 T9 N- Z8 ?2 v+ O2 u, { \# K
void creat(int array[])
- }4 O- C; g1 e8 _1 c3 M: g{int i;6 J: @( }8 E( I
printf("enter the array:\n");% x& O2 A" {! e7 M, c' w' U
for(i=1;i<N;i++)* l5 V9 A. H: `
scanf("%d",&array);; @1 |; m, V: J1 r" Q. `+ W: O
}</P>
& f! F. J- G! |3 y0 i< >/*显示*/9 u1 G' h& [. i/ I) i6 [
void print(int array[])
3 ^- z3 @& j9 \7 f- x: K {int i;, m. a" d4 H' C3 l/ ]; | m4 e
printf("The numbers after sort is:\n");
4 b& L5 N2 C3 W2 [, z for(i=1;i<N;i++)
# P2 w2 L% I2 ]* { printf("%d ",array);
W/ c- o$ Z2 K' i) t printf("\n");
/ [6 i; c3 Q# t# O; F A( } }</P>
: Y# V: F! k( W< >
4 R- o' Z- \3 ]0 F& I- Y: V1 ?5 H1 ]main()2 f2 o- {# Q; i- J- f
{int a[11],i,x,chang;' O9 M9 @$ ]4 o( ^" f* ~' y
/*printf("enter the array\n");
9 o6 ^5 @# n0 |+ e) Z9 i2 R: g for(i=1;i<11;i++)
- ^* u2 f% _. S( W! N k% H# s8 p _ scanf("%d",&a);*/</P>) v3 a' k+ Z. @3 ?- b3 h4 {
< >aga:
# B; j" i2 w* W/ E2 A 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");
+ _ O# @8 C1 v% i' o& {2 v* p+ \% H scanf("%d",&chang);; u/ Q& d( T. y# g: Y
switch (chang)
* I R8 ?5 q9 J5 n& q; n8 I% {# r {case 1:
& C3 W8 ]- |1 C! ^2 _6 ] {creat(a);
+ F# @! g" R8 ^ printf(" lease enter the search number:\n");) b, Z+ U8 O" w% |7 ?# E+ w
scanf("%d",&x);
: x0 z% }( r% y! j& m printf("The number station is:%d\n",search(a,N,x));
" T- }: d8 D) ^/ `1 J$ q. s, x goto aga;
8 a4 t* S, A* s0 u' G7 g }) r. ^2 A3 F$ [7 Q* |8 J5 w
case 2:
2 Z( e) X/ D6 U5 F { creat(a);
: M Q; V: s9 y insertsort(a);8 m& k# ~* [4 L# R
print(a);
: f( W m0 a+ A, f% p printf(" lease int the search number:\n");: x. W( ]0 n9 ^/ L+ U% n/ F
scanf("%d",&x);
& Q" B8 P- l4 [3 X/ D6 ]" q* i printf("The number station is:%d\n",halfsearch(a,N,x));4 s0 w4 @& y4 I5 q7 m: z
goto aga;
' B! g$ X; D3 w( H- f }8 ^# P5 Q0 N8 b
case 3:# G& h, Q& i) u; |$ |
{creat(a);1 I+ e2 J( A6 F: J i2 P) V
insertsort(a);- |6 `2 R9 y7 s! ?2 L
print(a);' B, x. `0 X y& T8 v- m Q
goto aga;1 S" N; K/ q& j; G9 y
}</P>
2 {/ Q) A6 H1 Q4 B: n( e< > case 4:
! k( g7 s2 N* n9 z {creat(a);0 P* z, H, a' D( [
mpsort(a);) M, H: e" d3 B+ V3 d
print(a);
$ U/ ~) g6 n; o1 n9 M goto aga;
1 N3 ?! c4 C/ f/ S1 H$ H" ?+ j }</P>/ r, ? C* `: W1 a
< > case 5:{ printf("exit!\n");break;}
8 E! ~5 {* P* Y0 E; ~ default:{printf("Error!\n"); goto aga;}
% w5 C/ w7 o' Z n6 j}
3 Y6 w6 L1 q# I7 P}
I, I9 u+ d. U1 w* g7 J
2 N3 Y9 ~( d* k) L 4 |, i. O7 u' Y
</P>
! [" `6 V9 \7 E4 ~( l, L[此贴子已经被作者于2004-6-3 12:16:43编辑过] |
zan
|