Category : C Source Code
Archive   : C_TUTOR.ZIP
Filename : DEMO_C09.SCR

 
Output of file : DEMO_C09.SCR contained in archive : C_TUTOR.ZIP
èèèèèèèèè[#10 Sort1]
è

void öd_õw(elt a[], int i0, int i1, int(*in_ö])())
{
ý 4int j0=i0, j1=i1;
ý 4elt temp, úu = a[(i0+i1)/2];

ý 4do {
ý 6while( in_ö](a[j0],úu) ) j0++;
ý 6while( in_ö](úu,a[j1]) ) j1--;
ý 6if(j0 < j1)
ý 8temp = a[j0], a[j0++] = a[j1], a[j1--] = temp;
ý 6else if(j0==j1) j0++;
ý 4} while(j0<=j1);
ý 4if(i0 ý 4if(j0 } 181920 The øˆ QWK_SORT in the XMPLS ø” üE the
öd-õw ú".22 In õƒ ö• we ù@ ø– of the ø™ øœ
û! õ are in ÷9 use. The õW øŠ is to
õ= õv ø¨ ü#s of ùƒ and ÷+.
25 Probably the ù™ ú" - ö( õ<
the üJ ÷4 õw - is the €+úS õw€ .
Iùw õw is û[ õ6 for öu ÷+.
28 It ö› the way a õ* ÷ öU ù his "hand" - by
ø‰ out the ö1 one at a õ„, and ü_ õ€ in the
øG ö].
31 void ÷^_õw(elt a[], int len, int in_ö]())
{
ý 5int i,j;
ý 5elt temp;
ý 5for(i=1; i ý 7temp = a[i];
ý 7for(j=i-1; j>=0 && !in_ö](a[j],temp); j--)
ý 9a[j+1] = a[j];
ý 7a[j+1] = temp;
ý 5}
}
42 After ø– iterations, the öA few entries are in ø:
ö] - ö— we take to be €+ûJ€ for the üFn.
44 The õb ö: is now to be ùd in its ÷‡ ö`. We
öA ö{ it ül...
46 õ, up-öq any of the ùJ ö, õ are ÷c...
47 ...and øU ÷^ it in its ö`.
48 The ø| õU ÷œ runs the úe ø’ for õ2
of the ÷˜ ø¥ õN ùJ in ûe.
50 void shell_õw(elt a[], int len, int in_ö]())
{
ý 5int gap, i, j;
ý 5elt temp;

ý 5for(gap=len/2; gap>0; gap/=2)
ý 7for(i=gap; i ý 9for(j=i-gap; j>=0 && !in_ö](a[j],a[j+gap]); j-=gap)
ý ;temp = a[j], a[j] = a[j+gap], a[j+gap] = temp;
}
60 The "shell õw" is named ö its inventor D. K. Shell. It öR
ø– ÷ ø¥ the ö&, on õ2 õj ú, and øœ
ö_ of ùJ õ are a öB ùF €+gap€ ö$.
63 This õQ ö} €+a[j]€ and €+a[j+gap]€ .
64 We öA ûH õ” a gap of 3.
65 Wø` a õz is õV, ø! øM gap-steps øL in
the ö& öY to be ø1 - as õT as ö is õo.
67 On the öA õj, gap is set to õ? the ö& ÷d. It
is cut in õ? on õ2 ûd õj, ö it is øU 1.
69 The ö8 "öj" öv õ{ to ù? the ÷w of
ü' õ õa to be õV on the öL ÷.
71 The ÷ is an ú" õ is úI õ\ ú<
õ~ the úS ú" - û= on ÷h ÷+.
73 The õF of the €+öd õw€ is to ÷@ the öH ö&
õH two ú} - õ” all the ùJ in one ùŸ ö+
øš õ~ any øM in the ö^ ùŸ.
76 The €+õq€ ø† is õ ù‘ on õ2 ùŸ, to ø‡
õ: õ4 øš ú} of the ù€, ö! õ” all the
ùJ of one ùŸ ö+ øš õ~ any øM in any
"higher" ùŸ.
80 This ûf üi ú/ ö none of the ú}
can be øX ûb, ø) õ‚ all õ@ ÷d 1. At õƒ
öa the ù€ ö& is in the øG ö].
83 Implementing õƒ ÷” is ù a little ø™ õ~ it
may sound, ø) C ùk ÷& any ù\ to õ) €+÷`€ .
In õƒ õ,, ö the öA ùG, we õK õ) öd_õw()
õ” õ2 of the ú} ù‡.
87 This is our öA øR of €+új€ (ù\ ø, ÷`).
88 We make a ÷ ùG by ÷T õv one øM of the ö&
as the €+úu€ , and õ øœ û> else ùŽ to
õƒ. Here we õ@ ÷8 the øM ø‘ in the ÷p of
the ö& as the úu,
92 FõI we õ] up to the öA øM out of ö] ùŽ to
the úu...
94 ...õ down to the õN øM out of ö].
95 If the øe are ú7, we õz ö‚ ùJ, õ ÷.
96 When the øe øU õ/ ù¥, we are ùY.
We õ@ the two ú}.
98 We õ ö3 to see if the ú} are ûQ
(÷d > 1), and, if so, õ) öd_õw() recursively.
00 The øˆ SORTCOMP.C in the XMPLS ø” ù2
the úA ö† of ø– of the øœ û!
öE in õƒ ø0.

è [#11 Sort2]
1 GENERIC
2 We õ@ ø° all our øœ û! in ö€ of a
"øZ" û) ù\ €+in_ö]()€ and õ0 õ‡ €+elt€ ,
but our ùO so far õ@ õ$ û_ to the øt
"ûJ" ö] on û% öŠ (øv).
6 The ÷“ øˆ QWK_SORT.C öF a õ\ üQ
ü# - õ” elt ö to be "ø€ to õ-" and
in_ö]() üg to strcmp().
9 The úq øˆ öv an ö& of ú* øŸ õH
ü!l ö] - ö‘ the €+õq€ øœ ú" õ was
ú6 in ö€ of the øt ö] on øv.
12 In QWK_SORT.C, the ú* øŸ õ/ õ< a DOS ú:
øm, and we õ ûH øœ in ö] of õ7-÷d,
by úo strcmp() õ” a ù¡ û) ù\.
15 But, the use of strcmp() for the û) ÷3 up a
öa õ öY to be û;: ö is õ\ õ~ one
way to "üh" the û) úH.
18 We õ@ ø° our øœ û! in ö€ of a ù\
in_ö]() õ ø ÷F 1 or 0, ú4 on ø® its
ú$ are in the ø: ö] or not.
21 An ü" õ öœ õ• õK as õ öœ be to õ@
the ÷‘ ö” €+r€ of in_ö](x,y) ùc ø® x was
"less" õ~, ö; to, or "ø[" õ~ y, as ös ö.
24 r < 0ý 5if x is "less" õ~ y
r = 0ý 5if x and y are ö;
r > 0ý 5if y is "less" õ~ x
27 The ù øk model of strcmp(), in õ5, øV õK õƒ
÷” (see the "ARRAYS" ö•). This öT õ we õa a ú7,
"0-1" ø¬ of strcmp(), if we are to use it õ” öd_õw() as
ø° øL. This is õK õ‘ we do in QWK_SORT.C.
31 Of ÷;, we could õ ö all the øœ û! in
ö€ of an in_ö]() ù\ õ øV the €+new€ ÷”.
33 It öœ øŽ õŒ little ÷7...
34 ...the test condition €+!in_ö](x,y)€ (old ö|) öœ ÷œ
be ù’ õ” the condition €+in_ö](x,y)>=0€ (new ö|).
36 For øR, to ÷7 €+÷^_õw()€ to the new ö|...
37 ...öœ øŽ õg õƒ öu ÷7.
38 void ÷^_õw(elt a[], int len, int in_ö]())
{
ý 5int i,j;
ý 5elt temp;
ý 5for(i=1; i ý 7temp = a[i];
ý 7for(j=i-1; j>=0 && !in_ö](a[j],temp); j--)
ý 9a[j+1] = a[j];
ý 7a[j+1] = temp;
ý 5}
}
49 int ÷2_search(int a[], int len, int õ8)
{
ý 5int lo = 0, hi = len-1, mid;

ý 5while (lo <= hi) {
ý 7mid = (hi+lo)/2;
ý 7if( õ8 == a[mid] ) ÷‘ mid;
ý 7if( õ8 < a[mid] ) hi = mid-1;
ý 7else lo = mid+1;
ý 5}
ý 5÷‘ -1;
}
61 The €+÷2 search€ ú" is an ú< way to search
for a öE øM ö# the ùJ of an ö&, ù)
õ the ö& has öA õ$ sorted.
64 For simplicity, we ÷- õ the ö& a[] is in ûJ
ö], and õ the ùJ are of õ‡ €+int€ .
66 The ÷V ûU of ÷2_search() are the ö& €+a[]€ ,
its ÷d €+len€ , and the øM €+õ8€ õ we seek.
68 The ÷‘ ö” is the öG of €+õ8€ , if it ø#
in a[], and -1, ú`.
70 We öA compare €+õ8€ õ” the øM ø‘ in the ÷p of a[].
71 If õƒ øM ÷I €+õ8€ , we ÷‘ its öG.
72 Otherwise, we know õ we õa õg search ö or ö, the
"mid-öa" of a[], so we ÷% the ú? ü .
74 And õ ÷.
75 Note õ the øR öt õ the õ& of the õU õ_
be ùP õ4 õ’ lo øU ÷I hi - it is õg at
õƒ öa õ we õ8 the ö” we are øp for.
78 It öœ not be ú8 to ÷q õƒ ú" so õ
it ÷° õ” a øZ in_ö]() ù\, ÷Š õ~ the
øt ûJ ö] on ùe, as we did õB.
81 The úq ú" õ“ õ õ• õ” the ù
øk ø¬ of strcmp(), or any ö^ û) ù\
öš ÷‘ ÷¬ ÷U the new ÷”.
è




  3 Responses to “Category : C Source Code
Archive   : C_TUTOR.ZIP
Filename : DEMO_C09.SCR

  1. Very nice! Thank you for this wonderful archive. I wonder why I found it only now. Long live the BBS file archives!

  2. This is so awesome! 😀 I’d be cool if you could download an entire archive of this at once, though.

  3. But one thing that puzzles me is the “mtswslnkmcjklsdlsbdmMICROSOFT” string. There is an article about it here. It is definitely worth a read: http://www.os2museum.com/wp/mtswslnk/