.[題目分析] 建立遞增有序的順序表對每個輸入數據應首先查找該數據在順序表中的位置若表中沒有該元素則插入之如已有該元素則不再插入為此采用折半查找方法
FUNC BinSearch(VAR asqlisttp;xinteger)integer;
∥在順序表a中查找值為x的元素如查找成功返回值如x不在a中則返回查找失敗時的較大下標值
low:=;high:=alast;found:=false;
WHILE(low<=high)AND NOT found DO
[mid:=(low+high)DIV ;
IF aelem[mid]=x THEN found:=true
ELSE IF aelem[mid]>x THEN high:=mid ELSE low:=mid+;
]
IF found=true THEN return()
ELSE return(low);∥當查找失敗時low=high+
ENDF;∥結束對分查找函數
PROC create(VAR Lsqlisttp)∥本過程生成順序表L
Llast:=;∥順序表L初始化
read(x);
WHILE x<> DO∥設x=時退出輸入
[k:=binsearch(Lx);∥去查找x元素
IF k<>∥不同元素才插入
THEN [FOR i:=Llast DOWNTO k DO Lelem[i+]:=Lelem[i];
Lelem[k]=x;Llast:= Llast+;∥插入元素x線性表長度增
]
read(x);
]
ENDP;∥結束過程creat
[] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] []
From:http://tw.wingwit.com/Article/program/sjjg/201311/23324.html