数学建模社区-数学中国
标题:
几种常用的查找和排序算法
[打印本页]
作者:
matrix_spaceman
时间:
2004-6-3 12:13
标题:
几种常用的查找和排序算法
<
>
% H+ Z+ q$ J4 r2 @" y0 c
#include <malloc.h>
+ |( y7 p, {' g- _7 u m) o& z
#include<stdio.h>
) K" H0 u! }9 o- C
#define N 11
! k: Z. p+ v; V0 Z
/*用监视哨查找*/
4 r6 U4 _ \( C4 B- `$ \
int search(int array[],int n,int k)
4 M% X/ L# T, A( p6 m4 Z
{int i;
- a$ D7 G. @. t A
i=n-1;
( Z) h- [( g a# u% f- _+ b" ^7 I
array[0]=k;
/ k7 G, f% i* N& Y1 ]7 B- [) R0 F
while(array
!=k) i--;
" c$ N2 c* A$ c3 G
return(i);
: w0 [& M, ]1 H* y* l6 d2 V& u! U" w0 o
}
3 H& v$ l5 O" g1 H: s
/*折半查找法*/
' F8 W) Q5 s* T) ~) D
int halfsearch(int array[],int n,int k)
: c. X# w1 x% L' F0 A: I
{int i,j,mid;
( C' t: f+ P: l
i=1;j=n;
5 o- @8 U- G, ]7 ^7 L. F0 s- f1 g" X
while(i<=j)
! U1 u" w5 i- P7 {; q) K# b- q
{mid=(i+j)/2;
i3 `& o& S% v" i
if(k==array[mid]) return(mid);
1 R! N8 g& a( o& D: V2 {
else if(k<array[mid]) j=mid-1;
" O. H, h4 r: g8 u2 @+ `
else i=mid+1;
- [) Y/ a* j9 W1 b3 Y# E5 J
}
3 ~1 A: d' Y: ]5 w* y: j/ K7 E t
return(0);
% a6 f* h1 L, \/ Z2 A; n/ m
}</P>
( c: U/ I$ \6 P. {4 W+ [
<
>/*冒泡排序法*/
: p; S, G9 q( S' z6 f5 M; {; W8 ~
void mpsort(int array[])
( d8 \, Z' j; S8 |
{int i,j,a;
* b C0 T. y( o/ i" |
a=0;
; _. Z6 b, G9 L) r: O' ]* W9 v+ ^
for(i=1;i<N;i++)
9 I+ e" ^* Q, _$ ^, E, m
for(j=i+1;j<N;j++)
" }: r$ ` y3 j3 d: E
if(array
>array[j])
4 T: f7 N, ~9 M! `# f# ?/ F6 H& K
{a=array
;
- W. U, }! q" v/ Y& @
array
=array[j];
* h- A! q( T0 ], x5 \1 E
array[j]=a;}
a9 k' C* Z: z+ v
}
/ ~5 z0 I* {* f: x$ g' U# H1 f
/*直接插入排序*/
7 K' |0 p3 @/ o* B* s# X5 D
void insertsort(int array[])
) R1 _8 J3 ~: o
{int i,j;
$ x Y9 }% x3 R5 k% o% H
for(i=2;i<N;i++)
e+ \6 h }: W& Y
{array[0]=array
;
. b$ U7 H: d/ p' g% x
j=i-1;
% W% n8 k. s- I3 A. l5 u
while(array[0]<array[j])
3 x( R4 u6 @* M, @$ E# ?+ n: i% b
{array[j+1]=array[j--];
$ j* S2 T4 y' ]. u/ }6 W1 K
array[j+1]=array[0];
* q4 {* {1 Z7 _4 h% }7 ]4 N H
}
" k9 K& S( D" a8 O4 y5 r+ L4 C# E
}
8 O) ~; l, h3 A
}
7 c# f8 k, {' y# e& T, b# O
/*建立*/
- G9 L$ a' q8 x( [
void creat(int array[])
9 S+ R& w8 ~7 Z8 v3 S! J! c
{int i;
! w9 K) x" R/ M( L& I& T
printf("enter the array:\n");
& w/ {* D9 j$ y W. H
for(i=1;i<N;i++)
3 V: l" `& _0 d
scanf("%d",&array
);
4 `/ W, O) U& _/ k2 O' m
}</P>
$ Z( D. b' u) @+ t* G, G
<
>/*显示*/
: l# D- V' q/ h H$ k/ s
void print(int array[])
1 @0 n! Z5 ^$ C
{int i;
" I5 ^4 U2 W+ L1 F& o3 n
printf("The numbers after sort is:\n");
! J1 k% O/ \% P& d. ?4 ?) {
for(i=1;i<N;i++)
0 r, `9 d5 _9 l3 o/ w
printf("%d ",array
);
: U+ E) {- V9 N, V2 L
printf("\n");
" p4 o; A) A8 N1 I- q8 b
}</P>
2 C: O1 l6 A) u6 }2 i
<
>
. |/ p: u# C8 D' [
main()
. K$ ~& u' g6 s1 m L
{int a[11],i,x,chang;
6 {: r2 N% N5 z- e' |1 Z. ~! B
/*printf("enter the array\n");
: _) g1 U( G& N5 ]4 N
for(i=1;i<11;i++)
: D- t% _) I1 H2 C. T
scanf("%d",&a
);*/</P>
* W9 y! t0 z. q; r5 E: a
<
>aga:
& S; B/ s/ w! v3 H" c- n0 {' I, t/ u
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");
x. v9 u7 Y; l" U, s+ @
scanf("%d",&chang);
h8 m% g1 B+ j4 ?- |, T+ r% o
switch (chang)
% h+ e* k+ K) S! Q$ W
{case 1:
( H S5 R- N D' {; N
{creat(a);
* E5 B3 b. d- q4 R4 Q; N/ F6 [
printf("
lease enter the search number:\n");
; ^( c! i% [/ ?3 N
scanf("%d",&x);
3 P; u$ }3 Y% Z* \2 v
printf("The number station is:%d\n",search(a,N,x));
" Q9 R, I, b; A9 T# x5 h6 j# J0 T
goto aga;
) A- n6 ^( e" ?, ^/ ^: T y8 `4 B( z
}
" n8 H& h0 j6 F, ]4 K7 U% h
case 2:
8 i% D; a! [9 O1 R
{ creat(a);
1 [; s Q" g" v7 r0 z6 K5 R) X
insertsort(a);
7 P7 m. [* F& m" l+ L
print(a);
0 C1 J, l% Z$ O* H
printf("
lease int the search number:\n");
0 @$ s. f' M0 ]# E. j
scanf("%d",&x);
7 ?8 p, x' ?0 C
printf("The number station is:%d\n",halfsearch(a,N,x));
4 E0 p$ I: r5 Q
goto aga;
, C$ A e9 j% O+ ]: h7 h
}
+ f; R# P |( p$ T
case 3:
5 Z4 a- \9 ?$ X9 |3 k
{creat(a);
9 B( O+ {7 ~" m* V+ g; _. f
insertsort(a);
, k/ S# o3 Y5 ?. Y/ d
print(a);
9 @) X5 Q4 e5 G( l
goto aga;
) f* d! F) F1 I. f
}</P>
- V3 c* ]0 P. ~; r
<
> case 4:
8 V- X( O) @- p* `6 k v& w
{creat(a);
# _$ u- O+ G( t. y0 V
mpsort(a);
' Q t) J8 |" N! d0 ^1 X
print(a);
j: y/ L8 U; J; W& w* O
goto aga;
# n0 y& c& T% \ m$ A) ]
}</P>
7 @; v+ x0 P' p3 M# \( a: U
<
> case 5:{ printf("exit!\n");break;}
/ t4 L* q, r" d2 l' O7 w
default:{printf("Error!\n"); goto aga;}
+ v9 ]8 I, b9 c3 ~& s# U; z
}
+ y" R8 g3 V$ \& V$ C. i) K
}
( c( o5 e1 D' t
; x) j4 f6 V6 K0 l! W
* H( b5 i$ |$ _" G
</P>
: _* I' `) s: h) L6 D
[此贴子已经被作者于2004-6-3 12:16:43编辑过]
作者:
ilikenba
时间:
2004-6-3 12:43
<
>不错!</P>
作者:
xpwei
时间:
2004-6-25 23:46
有没有奇偶校验排序的算法!!
作者:
Angel52416
时间:
2005-8-25 15:12
<
>不错!顶一下!!!</P>
作者:
ltlt00111
时间:
2007-9-2 15:17
hao
作者:
jerrychan
时间:
2010-2-10 19:43
顶一个,很好,谢谢分享,加油,大家一起努力,
作者:
为你奋斗
时间:
2010-4-15 10:27
不错,还是不错~整理成文件可下载就更好了
作者:
pingshaluoyan
时间:
2011-9-22 10:47
看起来蛮熟悉的
作者:
廷植斌_972
时间:
2011-10-31 02:31
呵呵,看大家评论如何
作者:
xiaoqiang00
时间:
2011-11-28 18:00
看看、、、
作者:
www.5dy5.com
时间:
2011-12-6 11:23
看了就留个记念
作者:
GraBUAA
时间:
2012-4-2 16:35
帮顶一下~~~
作者:
gopsjsnz
时间:
2012-4-12 13:52
标题:
大家好我是新来的请多多关照!
大家好我是新来的请多多关照!
作者:
天气不错rsq
时间:
2012-4-18 13:01
很好,谢谢你啊,辛苦了~~
作者:
qaz11sc0616
时间:
2012-5-13 21:03
哈 谢谢啦 !谢谢分享
作者:
凝香夜雪
时间:
2012-7-30 10:47
这是C吧 O(∩_∩)O~
欢迎光临 数学建模社区-数学中国 (http://www.madio.net/)
Powered by Discuz! X2.5