當前位置:首頁 » 編程軟體 » 哨兵編程技巧

哨兵編程技巧

發布時間: 2023-05-15 19:13:48

編程里「哨兵」是什麼意思

就是 sentinel ,就是用來指定一個位置的特殊元素,比如 快速排序 裡面,需要選一個變數作為中間值,這個值就是一個 sentinel,又比如用來指示一個隊列尾部位置的變數

② 編程C:使用「監視哨」方法實現順序表查找,並說明監視哨的作用

你說的「監視哨」就是「哨兵」吧,順序表查找似乎不需要,「哨兵」多用在排序中,下面給出一個實例帶詳注。我發的寫的程序都是經過驗證無誤的。本程序在win-tc和Dev-c++下運行通過。
/* 插入排序 */
#include <stdio.h>
#include <conio.h>
#define MAX 255
int R[MAX];
void Insert_Sort(int n)
{ /* 對數組R中的記錄R[1..n]按遞增序進行插入排序 */
int i,j;
for(i=2;i<=n;i++) /* 依次插入R[2],…,R[n] */
if(R[i]<R[i-1])
{/* 若R[i]大於等於有序區中所有的R,則R[i] */
/* 應在原有位置上 */
R[0]=R[i];j=i-1; /* R[0]是哨兵,且是R[i]的副本 */
do{ /* 從右向左在有序區R[1..i-1]中查找R[i]的插入位置 */
R[j+1]=R[j]; /* 將關鍵字大於R[i]的記錄後移 */
j--;
}while(R[0]<R[j]); /* 當R[i]≥R[j]時終止 */
R[j+1]=R[0]; /* R[i]插入到正確的位置上 */
}
}

main()
{
int i,n;
clrscr();
puts("Please input total element number of the sequence:");
scanf("%d",&n);
if(n<=0||n>MAX)
{
printf("n must more than 0 and less than %d.\n",MAX);
exit(0);
}
puts("Please input the elements one by one:");
for(i=1;i<=n;i++)
scanf("%d",&R[i]);
puts("The sequence you input is:");
for(i=1;i<=n;i++)
printf("%4d",R[i]);
Insert_Sort(n);
puts("\nThe sequence after insert_sort is:");
for(i=1;i<=n;i++)
printf("%4d",R[i]);
puts("\n Press any key to quit...");
getch();
}

熱點內容
叉叉助手刪除腳本 發布:2025-09-18 03:21:24 瀏覽:847
深圳ug五軸編程培訓 發布:2025-09-18 03:13:35 瀏覽:193
安卓軟體殘留怎麼清理 發布:2025-09-18 03:02:02 瀏覽:339
centos7apachephp7 發布:2025-09-18 03:01:47 瀏覽:651
安卓如何實現點擊彈出列表 發布:2025-09-18 02:47:25 瀏覽:52
python文件函數 發布:2025-09-18 02:47:23 瀏覽:568
pythonwrap 發布:2025-09-18 02:46:32 瀏覽:326
伺服器與計算機有什麼區別 發布:2025-09-18 02:07:26 瀏覽:931
python不支持的數據類型有 發布:2025-09-18 01:50:23 瀏覽:643
長江存儲科技招聘 發布:2025-09-18 01:44:48 瀏覽:767