锘??xml version="1.0" encoding="utf-8" standalone="yes"?> #include<stdio.h>
(1)鍋囪鐢ㄥ甫鏉冪殑閭繪帴鐭╅樀arcs鏉ヨ〃紺哄甫鏉冩湁鍚戝浘錛宎rcs[i][j]琛ㄧず寮?vi,vj)涓婄殑鏉冨箋傝嫢(vi,vj)涓嶅瓨鍦紝鍒欑疆arcs[i][j]涓烘棤絀峰ぇ銆係涓哄凡鎵懼埌浠巚鍑哄彂鐨勬渶鐭礬寰勭殑緇堢偣鐨勯泦鍚堬紝瀹冪殑鍒濆鐘舵佷負絀洪泦銆傞偅涔堬紝浠巚鍑哄彂鍒板浘涓婂叾浣欏悇欏剁偣錛堢粓鐐癸級vi,鍙兘杈懼埌鐨勬渶鐭礬寰勯暱搴︾殑鍒濆間負錛?br />D[i]=G.arcs[v][vi],vi灞炰簬V
(2)閫夋嫨Vj錛屼嬌寰?br /> D[j]=Min{D[i]|vi 灞炰簬V-S}
vj 灝辨槸褰撳墠姹傚緱鐨勪竴鏉′粠v鍑哄彂鐨勬渶鐭礬寰勭殑緇堢偣銆備護
S=S錛點锝涳綂锝?br />(3) 淇敼浠巚鍑哄彂鍒伴泦鍚圴-S涓婁換涓欏剁偣vk鍙揪鐨勬渶鐭礬寰勯暱搴︺傚鏋淒[j]+arcs[j][k]<D[k]鍒欎慨鏀笵[k]涓?D[k]=D[j]+arcs[j][k]
(4) 閲嶅鎿嶄綔(2),(3)鍏?n-1嬈°傜敱姝ゆ眰寰椾粠v鍒板浘涓婂叾浣欏悇欏剁偣鐨勬渶鐭礬寰勬槸渚濊礬寰勯暱搴﹂掑鐨勫簭鍒椼?br />
錛堜簩錛夊紬媧涗紛寰?Floyd)綆楁硶錛堟椂闂村鏉傚害涓篛(n3)錛?br />寮楁礇浼婂痙錛團loyd錛夌畻娉曟槸姹傚浘涓瘡涓瀵歸《鐐逛箣闂寸殑鏈鐭礬寰勶紝鏃墮棿澶嶆潅搴︿負O(n3).
寮楁礇浼婂痙綆楁硶浠嶄粠鍥劇殑甯︽潈閭繪帴鐭╅樀cost鍑哄彂錛屽叾鍩烘湰鎬濇兂鏄?:
鍋囪姹備粠欏剁偣vi鍒皏j鐨勬渶鐭礬寰勩傚鏋滀粠vi鍒皏j鏈夊姬錛屽垯浠巚i鍒皏j瀛樺湪涓鏉¢暱搴rcs[i][j]鐨勮礬寰勶紝璇ヨ礬寰勪笉涓瀹氭槸鏈鐭礬寰勶紝灝氶渶榪涜n嬈¤瘯鎺€傞鍏堣冭檻璺緞(vi,v0,vj)鏄惁瀛樺湪錛堝嵆鍒ゅ埆寮э紙vi,v0錛夊拰(v0,vj)鏄惁瀛樺湪)銆?濡傛灉瀛樺湪錛屽垯姣旇緝(vi,vj)鍜?vi,v0,vj)鏄惁瀛樺湪(鍗沖垽鍒姬(vi,v0)鍜?v0,vj)鏄惁瀛樺湪).濡傛灉瀛樺湪錛屽垯姣旇緝(vi,vj)鍜?vi,v0,vj)鐨勮礬寰勯暱搴﹀彇闀垮害杈冪煭鑰呬負浠巚i鍒皏j鐨勪腑闂撮《鐐圭殑搴忓彿涓嶅ぇ浜?鐨勬渶鐭礬寰勩傚亣濡傚湪璺緞涓婂啀澧炲姞涓涓《鐐箆1,涔熷氨鏄錛屽鏋?vi,...v1)鍜?v1...vj)鍒嗗埆涓哄綋鍓嶆壘鍒扮殑涓棿欏剁偣鐨勫簭鍙蜂笉澶т簬0鐨勬渶鐭礬寰勶紝閭d箞錛坴i,...,v1,...vj錛夊氨鏈夊彲鑳芥槸浠巚i鍒皏j鐨勪腑闂撮《鐐圭殑搴忓彿涓嶅ぇ浜?鐨勬渶鐭礬寰勩傚皢瀹冨拰宸茬粡寰楀埌鐨勪粠vi鍒皏j涓棿鐨勯《鐐瑰簭鍙蜂笉澶т簬0鐨勬渶鐭礬寰勭浉姣旇緝錛屼粠涓夊嚭涓棿欏剁偣鐨勫簭鍙蜂笉澶т簬1鐨勬渶鐭礬寰勪箣鍚庯紝鍐嶅鍔犱竴涓《鐐箆2,緇х畫榪涜璇曟帰銆備緷嬈$被鎺ㄣ傚湪涓鑸儏鍐典笅錛岃嫢(vi,...,vk)鍜?vk,...vj)鍒嗗埆鏄粠vi鍒皏k鍜屼粠vk鍒皏j鐨勪腑闂撮《鐐圭殑搴忓彿涓嶅ぇ浜巏-1鐨勬渶鐭礬寰勶紝鍒欏皢(vi,...vk,...vj)鍜屽凡緇忓緱鍒扮殑浠巚i鍒皏j涓斾腑闂撮《鐐瑰簭鍙蜂笉澶т簬k-1鐨勬渶鐭礬寰勭浉姣旇緝錛屽叾闀垮害杈冪煭鑰呬究鏄粠vi鍒皏j鐨勪腑闂撮《鐐圭殑搴忓彿涓嶅ぇ浜巏鐨勬渶鐭礬寰勩傝繖鏍鳳紝鍦ㄧ粡榪噉嬈℃瘮杈冨悗錛屾渶鍚庢眰寰楃殑蹇呮槸浠巚i鍒皏j鐨勬渶鐭礬寰勩?br />鐜板畾涔変竴涓猲闃舵柟闃靛簭鍒?br />D(-1),D(0),D(1),...D(k),...D(n-1)
鍏朵腑
D(-1)[i][j]=G.arcs[i][j].
D(k)[i][j]=Min{D(k-1)[i][j],D(k-1)[i][k]+D(k-1)[k][j]} 0<=k<=n-1
浠庝笂榪拌綆楀叕寮忓彲瑙侊紝D(1)[i][j]鏄粠vi鍒皏j鐨勪腑闂撮《鐐圭殑搴忓彿涓嶅ぇ浜?鐨勬渶鐭礬寰勭殑闀垮害銆侱(k)[i][j]鏄粠vi鍒皏j鐨勪腑闂撮《鐐圭殑搴忓彿涓嶅ぇ浜巏鐨勬渶鐭礬寰勭殑闀垮害銆侱(n-1)[i][j]灝辨槸浠巚i鍒皏j鐨勬渶鐭礬寰勭殑闀垮害銆?br />
]]>
璇嗗埆瀹夊叏杈圭殑瀹氱悊錛氳鍥綠=(V,E)鏄竴涓棤鍚戣繛閫氬浘錛屽茍涓斿湪E涓婂畾涔変簡涓涓叿鏈夊疄鏁板肩殑鍔犳潈鍑芥暟w.璁続鏄疎鐨勪竴涓瓙闆嗭紝瀹冨寘鍚簬G鐨勬煇涓渶灝忕敓鎴愭爲涓傝鍓?S,V-S)鏄疓鐨勪換鎰忎竴涓笉濡ㄥA鐨勫壊錛屼笖杈?u,v)鏄氳繃鍓?S,V-S)鐨勪竴鏉¤交杈癸紝鍒欒竟(u,v)瀵歸泦鍚圓鏉ヨ鏄畨鍏ㄧ殑銆?br />
鎺ㄨ錛氳G=(V,E)鏄竴涓棤鍚戣仈閫氬浘錛屽茍涓斿湪E涓婂畾涔変簡鐩稿簲鐨勫疄鏁板煎姞鏉冨嚱鏁皐.璁続鏄疎鐨勫瓙闆嗭紝騫跺寘鍚簬G鐨勬煇涓鏈灝忕敓鎴愭爲涓傝C=(Vc,Ec)涓烘.鏋桮A=(V,A) 鐨勪竴涓繛閫氬垎鏀傚鏋滆竟鏄繛鎺鍜孏a涓叾浠栨煇鑱旈氬垎鏀殑涓鏉¤交杈癸紝鍒?u,v)瀵歸泦鍚圓鏉ヨ鏄畨鍏?
鍦↘ruskal(鍏嬮瞾鏂崱灝?綆楁硶鍜孭rim(鏅噷濮?綆楁硶
鍦↘ruskal綆楁硶涓紝闆嗗悎A鏄竴涓.鏋楋紝鍔犲叆闆嗗悎A涓殑瀹夊叏杈規繪槸鍥句腑榪炴帴涓や釜涓嶅悓鑱旈氬垎鏀殑鏈灝忔潈杈廣傚湪Prim綆楁硶涓紝闆嗗悎A浠呭艦鎴愬崟媯墊爲錛屾坊鍔犲叆闆嗗悎A鐨勫畨鍏ㄨ竟鎬繪槸榪炴帴鏍戜笌涓涓笉鍦ㄦ爲涓殑欏剁偣鐨勬渶灝忔潈杈廣?br />
Kruskal(鍏嬮瞾鏂崱灝?綆楁硶(O(ElgE))錛?br />璇ョ畻娉曟壘鍑烘.鏋椾腑榪炴帴浠繪剰涓ゆ5鏍戠殑鎵鏈夎竟涓紝鍏鋒湁鏈灝忔潈鍊肩殑杈?u,v)浣滀負瀹夊叏杈癸紝騫舵妸瀹冩坊鍔犲埌姝e湪鐢熼暱鐨勬.鏋椾腑銆傝C1鍜孋2琛ㄧず杈?u,v)榪炴帴鐨勪袱媯墊爲錛屽洜涓?u,v)蹇呮槸榪炴帴C1鍜屽叾浠栨煇媯墊爲鐨勪竴鏉¤交杈癸紝鎵浠ョ敱涓婇潰鎺ㄨ鍙煡錛?u,v)瀵笴1鏉ヨ鏄畨鍏ㄨ竟銆侹ruskal 綆楁硶鍚屾椂涔熸槸涓縐嶈椽蹇冪畻娉曪紝 鍥犱負鍦ㄧ畻娉曠殑姣忎竴姝ヤ腑錛屾坊鍔犲埌媯灄涓殑杈圭殑鏉冨奸兘鏄敖鍙兘灝忕殑銆?br />涓嬮潰鏄吉浠g爜:
MST-KRUSKAL(G,w)
A<--絀洪泦
for each vertex v 灞炰簬 V[G]
do MAKE-SET(v)
sort the edges of E into nondecreasing order by weight w
for each edge(u,v)灞炰簬E,taken in nondecreasing order by weight
do if FIND-SET(u)!=FIND-SET(v)
then A<--AU{(u,v)}
UNION(u,v)
return A
FIND-SET(u)榪斿洖鍖呭惈u鐨勯泦鍚堜腑鐨勪竴涓唬琛ㄥ厓绱犮備簬鏄氳繃嫻嬭瘯FIND-SET(u)鏄惁絳夊悓浜嶧IND-SET(v)錛屽氨鍙互紜畾欏剁偣u鍜寁鏄惁灞炰簬鍚屼竴媯墊爲銆傞氳繃榪囩▼UNION錛屽彲浠ュ疄鐜版爲涓庢爲鐨勫悎騫躲?br />
Prim綆楁硶(O(ElgV))
Prim綆楁硶鐨勭壒鐐規槸闆嗗悎A涓殑杈規繪槸褰㈡垚鍗曟5鏍戙傛爲浠庝換鎰忔牴欏剁偣r寮濮嬪艦鎴愶紝騫墮愭笎鐢熸垚錛岀洿鑷寵鏍戣鐩栦簡V涓殑鎵鏈夐《鐐廣傚湪姣忎竴姝ワ紝涓鏉¤繛鎺ヤ簡鏍慉涓嶨a=(V,A)涓煇瀛ょ珛欏剁偣鐨勮交杈硅鍔犲叆鏍慉涓傜敱鎺ㄨ鍙煡錛岃瑙勫垯浠呭姞鍏ュA瀹夊叏鐨勮竟錛屽洜姝ゅ綋綆楁硶緇堟鏃訛紝A涓殑杈瑰艦鎴愪簡涓媯墊渶灝忕敓鎴愭爲銆傚洜姝ゆ瘡嬈℃坊鍔犲埌鏍戜腑鐨勮竟閮芥槸浣挎爲鐨勬潈灝藉彲鑳藉皬鐨勮竟錛屽洜姝わ紝涓婅堪絳栫暐涔熸槸“璐績“鐨勩?br />
浼唬鐮佸涓嬶細
MST-PRIM(G,w,r)
for each u 灞炰簬V[G]
do key[u] <--絀洪泦
n[u]<--NIL
key[r]<--0
Q<--V[G]
while Q!=絀洪泦
do u<---EXTRACT-MIN(Q)
for each v灞炰簬Adj[u]
do if v 灞炰簬Q and w(u,v)<key[v]
then n[u]<---u
key[v]<--w(u,v)
鍙傝冿細綆楁硶瀵艱
]]>
/********************************************************************
created: 2007/08/28
filename: avltree.c
author: Lichuang
purpose: AVL鏍戠殑瀹炵幇浠g爜,
鍙傝冭祫鏂?/span><<鏁版嵁緇撴瀯涓庣畻娉曞垎鏋?/span>-C璇█鎻忚堪>>, 浣滆匒llen Weiss
*********************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
typedef struct AVLTree
{
int nData;
struct AVLTree* pLeft;
struct AVLTree* pRight;
int nHeight;
}AVLTree;
int Max(int a, int b);
int Height(AVLTree* pNode);
AVLTree* Insert(int nData, AVLTree* pNode);
AVLTree* SingleRotateWithLeft(AVLTree* pNode);
AVLTree* SingleRotateWithRight(AVLTree* pNode);
AVLTree* DoubleRotateWithLeft(AVLTree* pNode);
AVLTree* DoubleRotateWithRight(AVLTree* pNode);
void DeleteTree(AVLTree** ppRoot);
void PrintTree(AVLTree* pRoot);
int main()
{
int i;
AVLTree* pRoot = NULL;
srand((unsigned int)::time(NULL));
for (i = 0; i < 100000000; ++i)
{
pRoot = Insert(::rand(), pRoot);
}
//PrintTree(pRoot);
DeleteTree(&pRoot);
return 0;
}
int Max(int a, int b)
{
return (a > b ? a : b);
}
int Height(AVLTree* pNode)
{
if (NULL == pNode)
return -1;
return pNode->nHeight;
}
AVLTree* Insert(int nData, AVLTree* pNode)
{
if (NULL == pNode)
{
pNode = (AVLTree*)malloc(sizeof(AVLTree));
pNode->nData = nData;
pNode->nHeight = 0;
pNode->pLeft = pNode->pRight = NULL;
}
else if (nData < pNode->nData) // 鎻掑叆鍒板乏瀛愭爲涓?br>
{
pNode->pLeft = Insert(nData, pNode->pLeft);
if (Height(pNode->pLeft) - Height(pNode->pRight) == 2) // AVL鏍戜笉騫寵
{
if (nData < pNode->pLeft->nData)
{
// 鎻掑叆鍒頒簡宸﹀瓙鏍戝乏杈? 鍋氬崟鏃嬭漿
pNode = SingleRotateWithLeft(pNode);
}
else
{
// 鎻掑叆鍒頒簡宸﹀瓙鏍戝彸杈? 鍋氬弻鏃嬭漿
pNode = DoubleRotateWithLeft(pNode);
}
}
}
else if (nData > pNode->nData) // 鎻掑叆鍒板彸瀛愭爲涓?br>
{
pNode->pRight = Insert(nData, pNode->pRight);
if (Height(pNode->pRight) - Height(pNode->pLeft) == 2) // AVL鏍戜笉騫寵
{
if (nData > pNode->pRight->nData)
{
// 鎻掑叆鍒頒簡鍙沖瓙鏍戝彸杈? 鍋氬崟鏃嬭漿
pNode = SingleRotateWithRight(pNode);
}
else
{
// 鎻掑叆鍒頒簡鍙沖瓙鏍戝乏杈? 鍋氬弻鏃嬭漿
pNode = DoubleRotateWithRight(pNode);
}
}
}
pNode->nHeight = Max(Height(pNode->pLeft), Height(pNode->pRight)) + 1;
return pNode;
}
/********************************************************************
pNode pNode->pLeft
/ \
pNode->pLeft ==> pNode
\ /
pNode->pLeft->pRight pNode->pLeft->pRight
*********************************************************************/
AVLTree* SingleRotateWithLeft(AVLTree* pNode)
{
AVLTree* pNode1;
pNode1 = pNode->pLeft;
pNode->pLeft = pNode1->pRight;
pNode1->pRight = pNode;
// 緇撶偣鐨勪綅緗彉浜? 瑕佹洿鏂扮粨鐐圭殑楂樺害鍊?br>
pNode->nHeight = Max(Height(pNode->pLeft), Height(pNode->pRight)) + 1;
pNode1->nHeight = Max(Height(pNode1->pLeft), pNode->nHeight) + 1;
return pNode1;
}
/********************************************************************
pNode pNode->pRight
\ /
pNode->pRight ==> pNode
/ \
pNode->pRight->pLeft pNode->pRight->pLeft
*********************************************************************/
AVLTree* SingleRotateWithRight(AVLTree* pNode)
{
AVLTree* pNode1;
pNode1 = pNode->pRight;
pNode->pRight = pNode1->pLeft;
pNode1->pLeft = pNode;
// 緇撶偣鐨勪綅緗彉浜? 瑕佹洿鏂扮粨鐐圭殑楂樺害鍊?br>
pNode->nHeight = Max(Height(pNode->pLeft), Height(pNode->pRight)) + 1;
pNode1->nHeight = Max(Height(pNode1->pRight), pNode->nHeight) + 1;
return pNode1;
}
AVLTree* DoubleRotateWithLeft(AVLTree* pNode)
{
pNode->pLeft = SingleRotateWithRight(pNode->pLeft);
return SingleRotateWithLeft(pNode);
}
AVLTree* DoubleRotateWithRight(AVLTree* pNode)
{
pNode->pRight = SingleRotateWithLeft(pNode->pRight);
return SingleRotateWithRight(pNode);
}
// 鍚庡簭閬嶅巻鏍戜互鍒犻櫎鏍?br>
void DeleteTree(AVLTree** ppRoot)
{
if (NULL == ppRoot || NULL == *ppRoot)
return;
DeleteTree(&((*ppRoot)->pLeft));
DeleteTree(&((*ppRoot)->pRight));
free(*ppRoot);
*ppRoot = NULL;
}
// 涓簭閬嶅巻鎵撳嵃鏍戠殑鎵鏈夌粨鐐? 鍥犱負宸︾粨鐐?nbsp;< 鐖剁粨鐐?nbsp;< 鍙崇粨鐐? 鍥犳鎵撳嵃鍑烘潵鏁版嵁鐨勫ぇ灝忔槸閫掑鐨?br>
void PrintTree(AVLTree* pRoot)
{
if (NULL == pRoot)
return;
static int n = 0;
PrintTree(pRoot->pLeft);
printf("[%d]nData = %d\n", ++n, pRoot->nData);
PrintTree(pRoot->pRight);
}
]]>
鍙傝冪畻娉曞璁虹208欏?br>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void LCS(char *p,char *q)
{
int lenP,lenQ;
int *s,*t;
lenP=strlen(p);
lenQ=strlen(q);
int i,j;
if((s=(int*)malloc(sizeof(int)*(lenP+1)*(lenQ+1)))==NULL)
{
perror("malloc error");
exit(1);
}
if((t=(int*)malloc(sizeof(int)*(lenP+1)*(lenQ+1)))==NULL)
{
perror("malloc error");
exit(1);
}
for(i=0;i<(lenP+1);i++)
s[i]=0;
for(i=0;i<(lenQ+1);i++)
s[i*(lenP+1)]=0;
for(i=1;i<(lenQ+1);i++)
{
for(j=1;j<(lenP+1);j++)
{
if(q[i-1]==p[j-1])
{
s[i*(lenP+1)+j]=s[(i-1)*(lenP+1)+j-1]+1;
t[i*(lenP+1)+j]=3;
}else if(s[(i-1)*(lenP+1)+j]<s[i*(lenP+1)+j-1]){
s[i*(lenP+1)+j]=s[i*(lenP+1)+j-1];
t[i*(lenP+1)+j]=1;
}else{
s[i*(lenP+1)+j]=s[(i-1)*(lenP+1)+j];
t[i*(lenP+1)+j]=2;
}
}
}
/*杈撳嚭鐭╅樀緇撴灉*/
printf("output the result:\n");
for(i=0;i<(lenQ+1);i++)
{
for(j=0;j<(lenP+1);j++)
{
printf("%d ",s[i*(lenP+1)+j]);
}
printf("\n");
}
printf("output the result 綆ご 1琛ㄧず鍚戝乏錛?琛ㄧず鍚戜笂錛?琛ㄧず鏂滃悜涓?\n");
for(i=0;i<(lenQ+1);i++)
{
for(j=0;j<(lenP+1);j++)
{
printf("%d ",t[i*(lenP+1)+j]);
}
printf("\n");
}
i=lenQ;
j=lenP;
/*鍊掑簭杈撳嚭LCS*/
printf("鍊掑簭杈撳嚭LCS\n");
while(i>1 || j>1)
{
if(t[i*(lenP+1)+j]==3)
{
printf("%c ",p[j-1]);
i--;
j--;
}else if(t[i*(lenP+1)+j]==2)
{
i--;
}else
j--;
}
printf("\n");
}
int main()
{
char *p="BDCABA";
char *q="ABCBDAB";
LCS(p,q);
}
]]>
#include<stdlib.h>
#define MAX 65536
void printMatrix(int *s,int len,int i,int j)
{
if(i==j)
printf("A%d",i+1);
else{
printf("(");
printMatrix(s,len,i,s[i*len+j]);
printMatrix(s,len,s[i*len+j]+1,j);
printf(")");
}
}
void matrixOrder(int p[][2],int len)
{
int *m,*s;
int i,j,k;
if((m=malloc(len*len*sizeof(int)))==NULL)
{
perror("malloc error");
exit(1);
}
if((s=malloc(len*len*sizeof(int)))==NULL)
{
perror("malloc error");
exit(1);
}
for(i=0;i<len;i++)
m[i*len+i]=0;
for(i=1;i<len;i++)
{
for(j=0;j+i<len;j++)
{
m[j*len+j+i]=MAX;
for(k=j;k<j+i;k++)
{
if((p[j][0]*p[k][1]*p[i+j][1]+m[j*len+k]+m[(k+1)*len+i+j])<m[j*len+j+i])
{
m[j*len+j+i]=p[j][0]*p[k][1]*p[i+j][1]+m[j*len+k]+m[(k+1)*len+i+j];
s[j*len+j+i]=k;
}
}
}
}
printf("##### then matrix m\n");
for(i=0;i<len;i++)
{
for(j=0;j<len;j++)
{
printf("%d ",m[i*len+j]);
}
printf("\n");
}
printf("##### the matrix s\n");
for(i=0;i<len;i++)
{
for(j=0;j<len;j++)
{
printf("%d ",s[i*len+j]);
}
printf("\n");
}
printMatrix(s,len,0,5);
printf("\n");
}
int main()
{
int p[6][2]={{30,35},{35,15},{15,5},{5,10},{10,20},{20,25}};
matrixOrder(p,6);
}
]]>
瑁呴厤綰胯皟搴﹀疄鐜?綆楁硶瀵艱192欏?
鍙傝冪畻娉曞璁?絎?5绔?br>
#include<stdio.h>
#include<stdlib.h>
int schedule(int a[][6],int t[][5],int e[],int x[])
{
int f[2][6];
int l[2][5];
int totalMin;
int lastL;
int i,k;
f[0][0]=e[0]+a[0][0];
f[1][0]=e[1]+a[1][0];
for(i=1;i<6;i++)
{
if(f[0][i-1]<(f[1][i-1]+t[1][i-1]))
{
f[0][i]=f[0][i-1]+a[0][i];
l[0][i-1]=1;
}else{
f[0][i]=f[1][i-1]+t[1][i-1]+a[0][i];
l[0][i-1]=2;
}
if(f[1][i-1]<(f[0][i-1]+t[0][i-1]))
{
f[1][i]=f[1][i-1]+a[1][i];
l[1][i-1]=2;
}else{
f[1][i]=f[0][i-1]+t[0][i-1]+a[1][i];
l[1][i-1]=1;
}
}
for(i=0;i<2;i++)
{
for(k=0;k<6;k++)
{
printf("%d ",f[i][k]);
}
printf("\n");
}
if((x[0]+f[0][5])<(x[1]+f[1][5]))
{
totalMin=x[0]+f[0][5];
lastL=1;
}else{
totalMin=x[1]+f[1][5];
lastL=2;
}
printf("totalMin=%d\n",totalMin);
if(lastL==1)
{
printf("S (1,6) ");
k=0;
}else{
printf("S (2,6) ");
k=1;
}
for(i=4;i>=0;i--)
{
if(l[k][i]==1)
{
printf("S (1, %d) ",i+1);
k=0;
}else{
printf("S (2, %d) ",i+1);
k=1;
}
}
printf("\n");
}
int main()
{
int a[2][6]={{7,9,3,4,8,4},{8,5,6,4,5,7}};
int t[2][5]={{2,3,1,3,4},{2,1,2,2,1}};
int e[2]={2,4};
int x[2]={3,2};
schedule(a,t,e,x);
}
]]>
鎻掑叆鏃舵湁涓夌鎯呭喌錛堣繖閲屽彧鑰冭檻z鐨勭埗鑺傜偣鏄痾鐨勭鐖惰妭鐐圭殑宸﹀瀛愶紝z鐨勭埗鑺傜偣鏄痾鐨勭鐖惰妭鐐圭殑鍙沖瀛愬縐頒篃涓鏍鳳級
錛?錛?z鐨勫彅鍙攜鏄孩鑹茬殑錛堟敼鍙橀鑹?鎻愬崌x錛?br>錛?錛?z鐨勫彅鍙攜鏄粦鑹茬殑錛岃屼笖z鏄彸瀛╁瓙錛堝乏鏃嬶級
錛?錛?z鐨勫彅鍙攜鏄粦鑹茬殑錛岃屼笖z鏄乏瀛╁瓙錛堝彸鏃嬪姞鏀瑰彉棰滆壊錛?br>
鍒犻櫎鏃舵湁鍥涚鎯呭喌錛堣繖閲屽彧鑰冭檻z鏄痾鐨勭埗鑺傜偣鐨勫乏瀛╁瓙錛寊鏄痾鐨勭埗鑺傜偣鐨勫彸瀛╁瓙瀵圭О涔熶竴鏍鳳級
錛?錛墄鐨勫厔寮焪鏄孩鑹茬殑錛堝乏鏃嬪姞鏀瑰彉棰滆壊錛?br>錛?錛墄鐨勫厔寮焪鏄粦鑹茬殑錛岃屼笖w鐨勪袱涓瀛愰兘鏄粦鑹茬殑錛堟敼鍙橀鑹詫紝鎻愬崌x)
錛?錛墄鐨勫厔寮焪鏄粦鑹茬殑錛寃鐨勫乏瀛╁瓙鏄孩鑹茬殑錛屽彸瀛╁瓙鏄粦鑹茬殑(鏀瑰彉棰滆壊鍔犲彸鏃?
錛?錛墄鐨勫厔寮焪鏄粦鑹茬殑錛岃屼笖w鐨勫彸瀛╁瓙鏄孩鑹茬殑錛堟敼鍙橀鑹插姞宸︽棆錛?br>*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
/*瀹氫箟棰滆壊鏋氫婦綾誨瀷*/
typedef enum color_t
{
RED=0,
BLACK=1
}color_t;
/*瀹氫箟緇撴瀯浣?/
typedef struct rb_node_t
{
int key;
color_t color;
struct rb_node_t *left,*right,*parent;
}rb_node_t;
/* 嫻嬭瘯鏄惁涓虹孩榛戞爲*/
int isRedBlackTree(rb_node_t *root,int count)
{
if(root==NULL)
{
printf("榛戦珮搴︿負 %d\n",count);
if(count!=12)/*閫氳繃嫻嬭瘯璇ユ祴璇曠敤渚嬮粦楂樺害涓?2錛屼笉鍏鋒湁鏅亶鎬?/
{
printf("not a redblack tree\n");
exit(1);
}
}else{
if(root->color==BLACK)
{
count++;
}
else{
if((root->left!=NULL &&root->left->color==RED)||
(root->right!=NULL && root->right->color==RED))
{
printf("child color RED\n");
}
}
isRedBlackTree(root->left,count);
isRedBlackTree(root->right,count);
}
}
/*涓簭鎵撳嵃鑺傜偣鐢ㄤ簬嫻嬭瘯*/
void midPrint(rb_node_t *root)
{
if(root!=NULL)
{
midPrint(root->left);
printf("%d ",root->key);
if(root->color==RED)
printf("RED ");
else
printf("BLACK ");
midPrint(root->right);
}
}
/*鍏堝簭鎵撳嵃鑺傜偣鐢ㄤ簬嫻嬭瘯*/
void prePrint(rb_node_t *root)
{
if(root!=NULL)
{
printf("%d ",root->key);
if(root->color==RED)
printf("RED ");
else
printf("BLACK ");
prePrint(root->left);
prePrint(root->right);
}
}
/*鍒涘緩鑺傜偣*/
rb_node_t * createNode(int key)
{
rb_node_t *newNode;
if((newNode=malloc(sizeof(rb_node_t)))==NULL)
{
printf("malloc error");
return NULL;
}
newNode->color=RED;
newNode->left=NULL;
newNode->right=NULL;
newNode->key=key;
newNode->parent=NULL;
return newNode;
}
/*宸︽棆*/
rb_node_t * leftRotate(rb_node_t *root,rb_node_t *node)
{
rb_node_t *right;
right=node->right;
if(node->right=right->left)
{
right->left->parent=node;
}
right->left=node;
if(right->parent=node->parent)
{
if(node->parent->left==node)
node->parent->left=right;
else
node->parent->right=right;
}else
root=right;
node->parent=right;
return root;
}
/*鍙蟲棆*/
rb_node_t *rightRotate(rb_node_t *root,rb_node_t *node)
{
rb_node_t *left;
left=node->left;
if(node->left=left->right)
left->right->parent=node;
left->right=node;
if(left->parent=node->parent)
{
if(node->parent->left==node)
node->parent->left=left;
else
node->parent->right=left;
}else
root=left;
node->parent=left;
return root;
}
/*鏌ユ壘鑺傜偣錛岃嫢鎵懼埌鍒欒繑鍥炶鑺傜偣錛岃嫢娌℃壘鍒拌繑鍥濶ULL騫朵笖灝嗙埗鑺傜偣淇濆瓨鍒皊ave涓?/
rb_node_t * rb_search_auxiliary(int key,rb_node_t *root,rb_node_t **save)
{
rb_node_t *node,*parent;
node=root;
parent=NULL;
while(node)
{
parent=node;
if(node->key<key)
node=node->right;
else if(node->key>key)
node=node->left;
else
return node;
}
if(save)
*save=parent;
return NULL;
}
/*鏌ユ壘鑺傜偣*/
rb_node_t * rb_search(int key,rb_node_t *root)
{
return rb_search_auxiliary(key,root,NULL);
}
/*鎻掑叆鑺傜偣鍚庤繘琛岃皟鏁達紝浣垮叾婊¤凍綰㈤粦鏍戞ц川*/
rb_node_t * rb_insert_reblance(rb_node_t *root,rb_node_t *node)
{
rb_node_t *parent,*uncle,*grandParent,*temp;
while((parent=node->parent)&&(parent->color==RED))
{
grandParent=parent->parent;
if(parent==grandParent->left)
{
uncle=grandParent->right;
if(uncle!=NULL && uncle->color==RED)
{
parent->color=BLACK;
uncle->color=BLACK;
grandParent->color=RED;
node=grandParent;
}else{
if(node==parent->right)
{
root=leftRotate(root,parent);
temp=parent;
parent=node;
node=temp;
}
//printf("##########\n");
//print(root);
//printf("\n");
parent->color=BLACK;
grandParent->color=RED;
root=rightRotate(root,grandParent);
//printf("##########\n");
// print(root);
// printf("\n");
}
}else{
uncle=grandParent->left;
if(uncle!=NULL && uncle->color==RED)
{
parent->color=BLACK;
uncle->color=BLACK;
grandParent->color=RED;
node=grandParent;
}else{
if(node==parent->left)
{
root=rightRotate(root,parent);
temp=parent;
parent=node;
node=temp;
}
parent->color=BLACK;
grandParent->color=RED;
root=leftRotate(root,grandParent);
}
}
}
root->color=BLACK;
return root;
}
/*綰㈤粦鏍戞彃鍏ヨ妭鐐?/
rb_node_t * rb_insert(rb_node_t *root,int key)
{
rb_node_t *parent,*newNode;
newNode=createNode(key);
if(rb_search_auxiliary(key,root,&parent)!=NULL)
{
printf("already exixt\n");
return root;
}
if(parent==NULL)
{
root=newNode;
}else{
newNode->parent=parent;
if(parent->key<key)
parent->right=newNode;
else
parent->left=newNode;
}
// print(root);
// printf("\n");
root=rb_insert_reblance(root,newNode);
return root;
}
/*鍒犻櫎榛戣妭鐐瑰悗榪涜璋冩暣錛屼嬌鍏舵弧瓚崇孩榛戞爲鎬ц川*/
rb_node_t *rb_delete_reblance(rb_node_t *root,rb_node_t *node,rb_node_t *parent)
{
rb_node_t *brother;
while((node==NULL ||node->color==BLACK)&&((node!=root)))
{
if(node==parent->left)
{
brother=parent->right;
if(brother->color==RED)
{
brother->color=BLACK;
parent->color=RED;
root=leftRotate(root,parent);
brother=parent->right;
}
if((!brother->left || brother->left->color==BLACK)&&
(!brother->right || brother->right->color==BLACK))
{
brother->color=RED;
node=parent;
parent=parent->parent;
}else{
if(!brother->right || brother->right->color==BLACK)
{
brother->color=RED;
brother->left->color=BLACK;
root=rightRotate(root,brother);
brother=parent->right;
}
brother->color=parent->color;
parent->color=BLACK;
brother->right->color=BLACK;
root=leftRotate(root,parent);
node=root;
}
}else{
brother=parent->left;
if(brother->color==RED)
{
brother->color=BLACK;
parent->color=RED;
root=rightRotate(root,parent);
brother=parent->left;
}
if((!brother->left ||brother->left->color==BLACK) &&
(!brother->right ||brother->right->color==BLACK))
{
brother->color=RED;
node=parent;
parent=parent->parent;
}else {
if(!brother->left || brother->left->color==BLACK)
{
brother->color=RED;
brother->right->color=BLACK;
root=leftRotate(root,brother);
brother=parent->left;
}
brother->color=parent->color;
parent->color=BLACK;
brother->left->color=BLACK;
root=rightRotate(root,parent);
node=root;
}
}
}
node->color=BLACK;
return root;
}
rb_node_t *rb_delete(rb_node_t *root,int key)
{
rb_node_t *node,*old,*child,*parent;
color_t color;
child=NULL;
if((node=rb_search(key,root))==NULL)
{
printf("not find the node\n");
return root;
}
old=node;
if(node->left!=NULL && node->right!=NULL)
{
node=node->right;
while(node->left!=NULL)
{
node=node->left;
}
child=node->right;
parent=node->parent;
if(child)
child->parent=parent;
if(parent->left==node)
parent->left=child;
else
parent->right=child;
if(node->parent==old)
{
parent=node;
}
color=node->color;
node->left=old->left;
node->right=old->right;
node->color=old->color;
node->parent=old->parent;
if(old->parent)
{
if(old->parent->left==old)
old->parent->left=node;
else
old->parent->right=node;
}else
root=node;
old->left->parent=node;
if(old->right)
old->right->parent=node;
free(old);
}else{
parent=node->parent;
if(node->left!=NULL)
child=node->left;
else
child=node->right;
if(child)
child->parent=parent;
if(parent)
{
if(parent->left==node)
parent->left=child;
else
parent->right=child;
}else
root=child;
color=node->color;
free(node);
}
if(color==BLACK)
rb_delete_reblance(root,child,parent);
return root;
}
int main()
{
int i,count=900000;
int key;
rb_node_t *root=NULL,*node=NULL;
srand(time(NULL));
int num=0;
for(i=1;i<count;++i)
{
key=rand()%count;
if(root=rb_insert(root,key))
{
printf("[i=%d] insert key %d,success!\n",i,key);
}else{
printf("[i=%d] insert key %d error!\n",i,key);
exit(1);
}
// printf("褰撳墠鏍戜腑鑺傜偣\n");
// midPrint(root);
// printf("\n");
if((node=rb_search(key,root)))
{
printf("[i=%d] search key %d success!\n",i,key);
}else{
printf("[i=%d] search key %d error!\n",i,key);
exit(1);
}
if(!(i%10))
{
// prePrint(root);
if((root=rb_delete(root,key)))
{
printf("[i=%d] delete key %d success\n",i,key);
}else
{
printf("[i=%d] erase key %d error\n",i,key);
exit(1);
}
}
}
/*printf("#########綰垮簭閬嶅巻\n");
prePrint(root);
printf("\n");
printf("$$$$$$$$$涓簭閬嶅巻\n");
midPrint(root);
printf("\n");
*/
printf("the root color %d\n",root->color);
isRedBlackTree(root,0);
return 0;
}
]]>
#include<string.h>
/*緇撴瀯浣撹妭鐐?/
typedef struct _node{
int key;
struct _node *left;
struct _node *right;
struct _node *parent;
}node;
/*鎻掑叆鑺傜偣*/
void insert(node *root,node *toInsert)
{
node *p,*q;
p=root;
q=NULL;
if(toInsert==NULL || root==NULL)
{
return;
}
while(p!=NULL)
{
q=p;/*璁板綍鐖惰妭鐐?/
if(toInsert->key<p->key)
{
p=p->left;
}else{
p=p->right;
}
}
if(toInsert->key<q->key)
{
q->left=toInsert;
}else{
q->right=toInsert;
}
toInsert->parent=q;
toInsert->left=NULL;
toInsert->right=NULL;
}
/*閫掑綊涓簭閬嶅巻鏍硅妭鐐?/
void middleSearch(node *root)
{
if(root!=NULL)
{
middleSearch(root->left);
printf("%d\t",root->key);
middleSearch(root->right);
}
}
/*鍏堝簭閬嶅巻*/
void preSearch(node *root)
{
if(root!=NULL)
{
printf("%d\t",root->key);
preSearch(root->left);
preSearch(root->right);
}
}
/*鏌ユ壘榪斿洖鑺傜偣鍏抽敭瀛椾負key鐨勮妭鐐?/
node* search(node *root,int key)
{
node *p=root;
while(p!=NULL)
{
if(key<p->key)
{
p=p->left;
}else if(key>p->key)
{
p=p->right;
}else
break;
}
return p;
}
/*鏌ユ壘浜屽弶鏍戞渶灝忓?/
node *minimun(node *root)
{
node *p=root;
if(p==NULL)
return p;
while(p->left!=NULL)
p=p->left;
printf("min %d\n",p->key);
return p;
}
/*鏌ユ壘浜屽弶鏍戞渶澶у?/
node *maximun(node *root)
{
node *p=root;
if(p==NULL)
return;
while(p->right!=NULL)
p=p->right;
printf("max %d\n",p->key);
return p;
}
/*鎵捐妭鐐瑰悗緇?/
node* successor(node *x)
{
node *p;
if(NULL==x)
return x;
if(x->right!=NULL)
return minimun(x->right);
p=x->parent;
while(p!=NULL && p->right==x)
{
x=p;
p=p->parent;
}
return p;
}
/*鍒犻櫎鑺傜偣*/
void delete(node *root,node *toDelete)
{
node *p,*q;
int key;
if(root==NULL || toDelete==NULL)
return ;
p=toDelete->parent;
/*絎竴縐嶆儏鍐碉紝瑕佸垹闄ょ殑鑺傜偣鐨勫乏鍙沖瓙鏍戦兘涓虹┖*/
if(toDelete->left ==NULL && toDelete->right ==NULL)
{
if(p==NULL)/*瑕佸垹闄ょ殑鏄牴鑺傜偣*/
{
free(toDelete);
return;
}
if(p->left==toDelete)
{
p->left=NULL;
}else
p->right=NULL;
free(toDelete);
}
/*絎簩縐嶆儏鍐碉紝瑕佸垹闄ょ殑宸﹀瓙鏍戜負絀猴紝鍙沖瓙鏍戜笉涓虹┖*/
else if(toDelete->left==NULL)
{
if(p==NULL)
{
q=root->right;
root->key=q->key;
root->left=q->left;
root->right=q->right;
free(q);
return;
}
else if(p->left==toDelete)
{
p->left=toDelete->right;
}else
p->right=toDelete->right;
toDelete->right->parent=p;
free(toDelete);
}
/* 絎笁縐嶆儏鍐碉紝瑕佸垹闄ょ殑鍙沖瓙鏍戜負絀猴紝宸﹀瓙鏍戜笉涓虹┖*/
else if(toDelete->right==NULL)
{
if(p==NULL)
{
q=root->left;
root->key=q->key;
root->left=q->left;
root->right=q->right;
root->parent=NULL;
free(q);
return;
}
else if(p->left==toDelete)
{
p->left=toDelete->left;
}else
p->right=toDelete->right;
toDelete->parent=p;
free(toDelete);
}
/* 絎洓縐嶆儏鍐碉紝瑕佸垹闄ょ殑宸﹀彸瀛愭爲閮戒笉涓虹┖*/
else{
q=successor(toDelete);
key=q->key;
delete(root,q);
toDelete->key=key;
}
}
int main()
{
node *root;
int a[12]={15,5,3,12,10,13,6,7,16,20,18,23};
node *toInsert;
node *x,*y;
int i;
/*鍒涘緩澶磋妭鐐?/
if((root=(node*)malloc(sizeof(node)))==NULL)
{
perror("malloc error");
exit(1);
}
root->key=a[0];
/*鎻掑叆鑺傜偣*/
for(i=1;i<12;i++)
{
if((toInsert=(node*)malloc(sizeof(node)))==NULL)
{
perror("malloc error");
exit(1);
}
toInsert->key=a[i];
insert(root,toInsert);
}
/*涓簭閬嶅巻*/
middleSearch(root);
printf("\n");
/*鍏堝簭閬嶅巻*/
preSearch(root);
printf("\n");
/*鏈澶у?/
maximun(root);
/*鏈灝忓?/
minimun(root);
/*鏌ユ壘鍏抽敭瀛楄妭鐐瑰強鍏跺墠椹?/
x=search(root,6);
y=successor(x);
if(y!=NULL)
printf("鑺傜偣 6 鍚庨┍ %d\n",y->key);
x=search(root,3);
y=successor(x);
if(y!=NULL)
printf("鑺傜偣 3 鍚庨┍ %d\n",y->key);
x=search(root,13);
y=successor(x);
if(y!=NULL)
printf("鑺傜偣 13 鍚庨┍ %d\n",y->key);
x=search(root,23);
y=successor(x);
if(y!=NULL)
printf("鑺傜偣 23 鍚庨┍ %d\n",y->key);
/*鍒犻櫎鑺傜偣*/
printf("before delete the node\n");
x=search(root,13);
delete(root,x);
printf("\nafter delete the node\n");
printf("涓簭閬嶅巻\n");
middleSearch(root);
printf("\n鍏堝簭閬嶅巻\n");
preSearch(root);
if((toInsert=(node*)malloc(sizeof(node)))==NULL)
{
perror("malloc error");
exit(1);
}
toInsert->key=13;
insert(root,toInsert);
printf("\n涓簭閬嶅巻\n");
middleSearch(root);
printf("\n鍏堝簭閬嶅巻\n");
preSearch(root);
printf("\nbefore delete the node\n");
x=search(root,16);
delete(root,x);
printf("\nafter delete the node\n");
printf("涓簭閬嶅巻\n");
middleSearch(root);
printf("\n鍏堝簭閬嶅巻\n");
preSearch(root);
if((toInsert=(node*)malloc(sizeof(node)))==NULL)
{
perror("malloc error");
exit(1);
}
toInsert->key=16;
insert(root,toInsert);
printf("\n涓簭閬嶅巻\n");
middleSearch(root);
printf("\n鍏堝簭閬嶅巻\n");
preSearch(root);
printf("\nbefore delete the node\n");
x=search(root,5);
delete(root,x);
printf("\nafter delete the node\n");
printf("涓簭閬嶅巻\n");
middleSearch(root);
printf("\n鍏堝簭閬嶅巻\n");
preSearch(root);
if((toInsert=(node*)malloc(sizeof(node)))==NULL)
{
perror("malloc error");
exit(1);
}
toInsert->key=5;
insert(root,toInsert);
printf("\n涓簭閬嶅巻\n");
middleSearch(root);
printf("\n鍏堝簭閬嶅巻\n");
preSearch(root);
printf("\nbefore delete the node\n");
x=search(root,15);
delete(root,x);
printf("\nafter delete the node\n");
printf("涓簭閬嶅巻\n");
middleSearch(root);
printf("\n鍏堝簭閬嶅巻\n");
preSearch(root);
printf("\n");
printf("before delete the node\n");
x=search(root,16);
delete(root,x);
printf("\nafter delete the node\n");
printf("涓簭閬嶅巻\n");
middleSearch(root);
printf("\n鍏堝簭閬嶅巻\n");
preSearch(root);
printf("\n");
printf("before delete the node\n");
x=search(root,18);
delete(root,x);
printf("\nafter delete the node\n");
printf("涓簭閬嶅巻\n");
middleSearch(root);
printf("\n鍏堝簭閬嶅巻\n");
preSearch(root);
printf("\n");
printf("before delete the node\n");
x=search(root,20);
delete(root,x);
printf("\nafter delete the node\n");
printf("涓簭閬嶅巻\n");
middleSearch(root);
printf("\n鍏堝簭閬嶅巻\n");
preSearch(root);
printf("\n");
printf("before delete the node\n");
x=search(root,23);
delete(root,x);
printf("\nafter delete the node\n");
printf("涓簭閬嶅巻\n");
middleSearch(root);
printf("\n鍏堝簭閬嶅巻\n");
preSearch(root);
printf("\n");
}
]]>
#include<stdio.h>
#include<stdlib.h>
void print(int A[],int len)
{
int i;
for(i=0;i<len;i++)
{
printf("%d ",A[i]);
}
printf("\n");
}
int quickPart(int A[],int begin,int end)
{
int key,i,j,temp;
key=A[end-1];
i=begin-1;
for(j=begin;j<end-1;j++)
{
if(A[j]<key)
{
i++;
temp=A[i];
A[i]=A[j];
A[j]=temp;
}
}
i++;
temp=A[i];
A[i]=key;
A[end-1]=temp;
return (i);
}
void quickSort(int A[],int begin,int end)
{
int q;
if(end>begin)
{
q=quickPart(A,begin,end);
quickSort(A,begin,q);
quickSort(A,q+1,end);
}
}
int main()
{
int A[8]={2,8,7,1,3,5,6,4};
quickSort(A,0,8);
printf("after sort\n");
print(A,8);
}
]]>
#include<stdlib.h>
/*鍋囪鑺傜偣i鐨勫乏鍙沖瓙鏍戦兘鏄渶澶у爢錛屾搷浣滀嬌鑺傜偣i鐨勫瓙鏍戝彉鎴愭渶澶у爢*/
void maxHeap(int A[],int len,int i)
{
int l,r,large,temp;
l=2*i;
r=2*i+1;
large=i;
if(l<len)
{
if(A[l]>A[i])
{
large=l;
}
}
if(r<len)
{
if(A[r]>A[large])
{
large=r;
}
}
if(large!=i)
{
temp=A[large];
A[large]=A[i];
A[i]=temp;
maxHeap(A,len,large);
}
}
/*寤虹珛澶ф牴鍫?/
void buildMaxHeap(int A[],int len)
{
int i;
for(i=len/2-1;i>=0;i--)
maxHeap(A,len,i);
}
/*鍫嗘帓搴?/
void maxHeapSort(int A[],int len)
{
int i,temp;
buildMaxHeap(A,len);
printf("寤虹珛澶ц窡鍫哱n");
for(i=0;i<len;i++)
printf("%d ",A[i]);
printf("\n");
for(i=len;i>1;i--)
{
temp=A[0];
A[0]=A[i-1];
A[i-1]=temp;
printf("%d ",A[i-1]);
buildMaxHeap(A,i-1);
}
printf("\n");
}
/*嫻嬭瘯鍫嗘帓搴?/
int main()
{
int i;
int A[11]={4,1,3,2,16,9,10,14,8,7,6};
maxHeapSort(A,11);
for(i=0;i<11;i++)
{
printf("%d ",A[i]);
}
printf("\n");
}
]]>
#include<stdlib.h>
#define MAX 65536 /*鐢ㄤ簬褰掑茍鎺掑簭涓殑鍝ㄥ叺*/
/*綆鍗曟彃鍏ユ帓搴?鏃墮棿澶嶆潅搴︿負o(n2),紼沖畾鎺掑簭錛岄傚悎宸茬粡鎺掑ソ搴忕殑*/
void insertSort(int arr[],int len)
{
int i,j;
int key;
for(i=1;i<len;i++)
{
key=arr[i];
for(j=i-1;j>=0;j--)
{
if(arr[j]>key)
arr[j+1]=arr[j];
else
break;
}
arr[j+1]=key;
}
}
/*閫夋嫨鎺掑簭錛屾椂闂村鏉傚害涓簅(n2),涓嶇ǔ瀹氭帓搴忥紝閫傚悎瑙勬ā姣旇緝灝忕殑*/
void selectSort(int arr[],int len)
{
int i,j,temp;
for(i=0;i<len;i++)
{
for(j=i+1;j<len;j++)
{
if(arr[i]>arr[j])
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
}
/*鍐掓場鎺掑簭錛屾椂闂村鏉傚害涓簅(n2),紼沖畾鎺掑簭錛岄傚悎瑙勬ā姣旇緝灝忕殑*/
void bubbleSort(int arr[],int len)
{
int i,j,temp;
for(i=0;i<len;i++)
{
for(j=0;j<len-i-1;j++)
{
if(arr[j]>arr[j+1])
{
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
}
/*鍚堝茍錛堝綊騫訛級鎺掑簭,鏃墮棿澶嶆潅搴︿負o(nlogn),紼沖畾鎺掑簭錛岄傚悎瑙勬ā姣旇緝澶х殑鎺掑簭*/
void merge(int arr[],int p,int q,int r)
{
int *A,*B;
int n1,n2,i,j,k;
n1=q-p+1;
n2=r-q;
if((A=(int*)malloc((n1+1)*sizeof(int)))==NULL)
{
perror("malloc error");
exit(1);
}
if((B=(int*)malloc((n2+1)*sizeof(int)))==NULL)
{
perror("malloc error");
exit(1);
}
for(i=0;i<n1;i++)
{
A[i]=arr[p+i];
}
A[i]=MAX;
for(i=0;i<n2;i++)
{
B[i]=arr[q+i+1];
}
B[i]=MAX;
i=0;j=0;
for(k=p;k<=r;k++)
{
if(A[i]>B[j])
{
arr[k]=B[j];
j++;
}else{
arr[k]=A[i];
i++;
}
}
}
void mergeSort(int arr[],int begin,int end)
{
int mid;
if(begin<end)
{
mid=(begin+end)/2;
mergeSort(arr,begin,mid);
mergeSort(arr,mid+1,end);
merge(arr,begin,mid,end);
}
}
int main()
{
int a[8]={5,2,4,7,1,3,2,6};
int b[8]={5,2,4,7,1,3,2,6};
int c[8]={5,2,4,7,1,3,2,6};
int d[8]={5,2,4,7,1,3,2,6};
int i;
/*嫻嬭瘯鎻掑叆鎺掑簭*/
insertSort(a,8);
printf("after 鎻掑叆鎺掑簭\n");
for(i=0;i<8;i++)
{
printf("%d ",a[i]);
}
printf("\n");
/*嫻嬭瘯閫夋嫨鎺掑簭*/
selectSort(b,8);
printf("after 閫夋嫨鎺掑簭\n");
for(i=0;i<8;i++)
{
printf("%d ",b[i]);
}
printf("\n");
/*嫻嬭瘯鍐掓場鎺掑簭*/
bubbleSort(c,8);
printf("after 鍐掓場鎺掑簭\n");
for(i=0;i<8;i++)
{
printf("%d ",c[i]);
}
printf("\n");
/*嫻嬭瘯褰掑茍鎺掑簭*/
mergeSort(d,0,7);
printf("after 褰掑茍鎺掑簭\n");
for(i=0;i<8;i++)
{
printf("%d ",d[i]);
}
printf("\n");
}
]]>
#include<stdlib.h>
#include<string.h>
/*naive string-matching algorithm,T涓哄師濮嬪瓧絎︿覆錛孭涓洪渶瑕佸尮閰嶇殑瀛楃涓?/
void naiveMatch(char *T,char *P)
{
int lenT,lenP,i,j;
lenT=strlen(T);
lenP=strlen(P);
if(lenT<lenP)/*闇瑕佸尮閰嶇殑瀛楃涓叉瘮鍘熷瀛楃涓茶繕瑕侀暱鍑洪敊*/
{
perror("input error");
return ;
}
for(i=0;i<(lenT-lenP+1);i++)
{
for(j=0;j<lenP;j++)
{
if(T[i+j]!=P[j])
break;
}
if(j==lenP)
printf("string match at place %d\n",i);
}
}
/*kmp棰勫鐞嗛渶瑕佺殑鍖歸厤涓?/
void getNext(char *p,int next[])
{
int len,i,j;
len=strlen(p);
next[0]=0;
for(i=1;i<len;i++)
{
j=next[i-1];
while((p[i-1]!=p[j-1])&&(j!=0))
{
j=next[j-1];
// printf("j= %d\n",j);
}
next[i]=j+1;
printf("next[i]=%d\n",next[i]);
}
}
/*kmp瀛楃涓插尮閰嶇畻娉?/
void kmp(char *t,char *p,int next[])
{
int lent,lenp,i,j;
lent=strlen(t);
lenp=strlen(p);
i=0;
j=0;
while(i<(lent))
{
if((j==-1)||(t[i]==p[j]))
{
i++;
j++;
// printf("i=%d,j=%d\n",i,j);
}
else
{
// printf("in else i=%d,j=%d\n",i,j);
j=next[j]-1;
}
if(j==(lenp))
{
printf("match at place %d\n",i-lenp);
}
}
}
int main()
{
char p[10]="0001";
char t[20]="000010001010001";
naiveMatch(t,p);/*嫻嬭瘯鏅氬瓧絎︿覆鍖歸厤綆楁硶*/
char p1[10]="abaabcac";
char t1[20]="acabaabaabcacaabc";
int len=strlen(p1);
int *next;
next=(int*)malloc(sizeof(int)*len);
getNext(p1,next);
kmp(t1,p1,next);/*嫻嬭瘯kmp綆楁硶*/
getNext(p,next);
kmp(t,p,next);/*嫻嬭瘯kmp綆楁硶*/
}
]]>