publicstaticintlinearSearch(int item){ for(int i=0;i<mylist1.length;++i){//从头开始一个一个看数组里的`element`是否是正在找的`item` if(item==mylist1[i]){//如果相等 return i;//返回当前index } } System.out.println("item not found");//如果找到最后了都没找到 则打印"item not found" return -1;//终止程序并返回一个不是index的数字 }
1 2 3 4 5 6 7 8 9 10 11
FUNCTION LinearSearch(item:INTEGER, arr: ARRAY[1:10] OF INTEGER) RETURNS INTEGER FOR i <- 1 TO 10 IF arr[i] = item THEN OUTPUT "Item found" RETURN i ENDIF NEXT i OUTPUT "Item not found" RETURN -1 ENDFUNCTION
publicstaticintbinarySearch(int item){//the item to be searched int lwb=0,upb=7;//lowerbound和upperbound的初始化 int mid; Boolean found=false;//初始化 while(!found && lwb<=upb){//还没找到且范围正常 mid=(lwb+upb)/2;//算出区间中值 然后对比区间中间的element和寻找的element的大小 if(mylist2[mid]==item){//如果刚好找到 则fouund=true, 终止循环 found=true; } elseif(mylist2[mid]<item){//如果要找的item比区间中值大 则lwb变成mid+1 lwb=mid+1; } else{ upb=mid-1; } } if(found){ return mid; } else{ System.out.println("item not found"); return -1; } }
publicstaticintrecursiveBinarySearch(int[] bsarr, int lwb, int upb, int snum){//snum: the number to be searched; bsarr: array for binary search int mid=(upb-lwb)/2+lwb; if(lwb>upb){ System.out.println("not found"); return -1; } if(bsarr[mid]==snum){ return mid; } elseif(bsarr[mid]<snum){ return recursiveBinarySearch(bsarr, mid+1, upb, snum); } else{ return recursiveBinarySearch(bsarr, lwb, mid-1, snum); } }
FUNCTION binarySearch(item:INTEGER, arr: ARRAY[1:10] OF INTEGER) DECLARE upb: INTEGER DECLARE lwb: INTEGER DECLARE mid: INTEGER upb <- 10 lwb <- 1 WHILE lwb<upb mid<-(upb+lwb)/2 // mid <- (upb-lwb)/2+lwb IF arr[mid]=item THEN OUTPUT "found" RETURN mid ELSE IF arr[mid]<item THEN lwb=mid+1 ELSE upb=mid-1 ENDIF ENDIF ENDWHILE OUTPUT "not found" RETURN -1
Running:
1 2 3 4 5 6 7 8
System.out.println("binary search:"); System.out.println("Position of 7 in mylist2: "+ binarySearch(7)); System.out.println("Position of -2 in mylist2: "); binarySearch(-2); System.out.println("linear search:"); System.out.println("Position of 22 in mylist1: "+linearSearch(22)); System.out.println("Position of -2 in mylist1: "); linearSearch(-2);
Result:
1 2 3 4 5 6 7 8
binary search: Position of 7 in mylist2: 0 Position of -2 in mylist2: item not found linear search: Position of 22 in mylist1: 3 Position of -2 in mylist1: item not found
Sort
Factors that may affect the performance of a sorting algorithms:
FUNCTION BubbleSort(arr : ARRAY [1:10] OF INTEGER) RETURNS ARRAY [1:10] OF INTEGER DECLARE top : INTEGER DECLARE tmp : INTEGER DECLARE swap : BOOLEAN top=9 REPEAT swap=FALSE FOR i <- 1 TO top IF arr[i]>arr[i+1] THEN tmp<-arr[i] arr[i]=arr[i+1] arr[i+1]=tmp swap<-TRUE ENDIF NEXT i top <- top -1 UNTIL NOT swap AND top<1 RETURN arr ENDFUNCTION
Insertion Sort
Describe how an insertion sort will sort the data
Set the first element to be the sorted list
Store the next element in a temporary variable // store the value to be sorted in a temporary variable
… Compare this next element to each element in the sorted list
Move the elements that are greater than it one space to the right and insert the temporary variable// swap the element down until in the correct position
publicstaticint[] insertionsort1(int[] arr){ int lwb=0; int upb=arr.length-1; for(int i=lwb+1;i<=upb;++i){ int key=arr[i]; int place=i-1; if(arr[place]>key){ while(place>=lwb && arr[place]>arr[place+1]){ int tmp=arr[place+1]; arr[place+1]=arr[place]; arr[place]=tmp; place--; } } } return arr; } publicstaticint[] insertionsort2(int[] arr){// this is more preferrable and more efficienct compared to insertionsort1 int lwb=0; int upb=arr.length-1; for(int i=lwb+1;i<=upb;++i){ intkey= arr[i]; intj= i-1; while(arr[j]>key && j>=lwb){ arr[j+1]=arr[j]; j--; } arr[j+1]=key; } return arr; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14
FUNCTION insertionSort (arr : ARRAY[1:10] OF INTEGER) RETURNS ARRAY[1:10] OF INTEGER DECLARE key : INTEGER DECLARE place:INTEGER FOR i < 2 TO 10 key <- arr[i] place <- i-1 WHILE arr[place-1] > arr[place] && place>1 arr[place]=arr[place-1] place <- place -1 ENDWHILE arr[place+1]<-key NEXT i RETURN arr ENDFUNCTION
classqueue{ int[] que= newint[10]; int front=0; int rear=-1; int queueFull=10;//store the maximum size of the queue int queueLength=0;// store the current size of the queue publicvoidenqueue(int item){ if(queueLength<queueFull){ if(rear<queueFull-1){ rear++; } else{ rear=0; } queueLength=queueLength+1; que[rear]=item; } else{ System.out.println("queue is full, cannot enqueue"); } } publicintdequeue(){ if(queueLength==0){ System.out.println("Queue is empty, cannot dequeue"); return -1; } else{ int item=que[front]; if(front==queueFull-1){ front=0; } else{ front=front+1; } queueLength--; return item; } } }
Linked List
Insert
Delete
Find
Initialization
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
CONSTANT nullPointer=-1 TYPE node DECLARE data:STRING DECLARE nextPointer:INTEGER ENDTYPE DECLARE startPointer,freePointer:INTEGER DECLARE list : ARRAY[0:N] OF node PROCEDURE Initialization startPointer <- -1 freePointer <- 0 FOR i <- 0 TO N-1 list[i].nextPointer<-i+1 NEXT i list[N].nextPointer<-nullPointer ENDPROCEDURE
PROCEDURE delete(item: STRING) DECLARE prevPointer, thisPointer:INTEGER prevPointer<-nullPointer thisPointer<-startPointer WHILE list[thisPointer]<>null && list[thisPointer].data<>item prevPointer<-thisPointer thisPointer<-list[thisPointer].nextPointer ENDWHILE IF thisPointer<>nullPointer THEN IF thisPointer=startPointer THEN startPointer<-list[startPointer].nextPointer ELSE list[prevPointer].nextPointer<-list[thisPointer].nextPointer ENDIF list[thisPointer].nextPointer<-freePointer freePointer<-thisPointer ELSE OUTPUT "not found the item to be deleted" ENDIF ENDPROCEDURE
PROCEDURE insert DECLARE newPointer,thisPointer,prevPointer:INTEGER DECLARE turnedLeft : BOOLEAN IF freePointer<>-1 THEN newPointer<-freePointer freePointer<-tree[freePointer].leftPointer INPUT data tree[newPointer].data <- data tree[newPointer].leftPointer<- nullPointer tree[newPointer].rightPointer <- nullPointer IF rootPointer = nullPointer THEN rootPointer<- newPointer ELSE thisPointer<-rootPointer prevPointer<-nullPointer WHILE thisPointer<>-1 prevPointer<-thisPointer IF tree[thisPointer].data < data THEN turnedLeft<-FALSE thisPointer<-tree[thisPointer].rightPointer ELSE turnedLeft<-TRUE thisPointer<-tree[thisPointer].leftPointer ENDIF ENDWHILE IF turnedLeft THEN tree[prevPointer].leftPointer<-newPointer ELSE tree[prevPointer].rightPointer<-newPointer ENDIF ENDIF ENDIF ENDPROCEDURE
Search🔍
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
publicstaticvoidsearch(int element){ int nownode=RootPointer; while(nownode!=-1){ if(ArrayNodes[nownode].data==element){ System.out.println("found, the position is:"+ nownode); return; } elseif(ArrayNodes[nownode].data>element){ nownode=ArrayNodes[nownode].leftPointer; } else{ nownode=ArrayNodes[nownode].rightPointer; } } System.out.println("not found"); }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
FUNCTION search(item : STRING) RETURNS INTEGER DECLARE thisPointer : INTEGER thisPointer<-rootPointer WHILE thisPointer<>nullPointer IF tree[thisPointer].data=item THEN RETURN thisPointer ELSE IF tree[thisPointer].data>item THEN thisPointer<-tree[thisPointer].leftPointer ELSE thisPointer<-tree[thisPointer].rightPointer ENDIF ENDIF ENDWHILE ENDFUNCTION
class Main{
static node[] ArrayNodes = new node[20];
static int RootPointer=-1;
static int FreeNode=0;
public static void main(String[] args){
//Adding: 23 5 8 100 9 88
System.out.println("Adding Nodes");
AddNode();
AddNode();
AddNode();
AddNode();
AddNode();
AddNode();
System.out.println("Printing Tree Content");
printContent();
System.out.println("Inorder Traverse");
inorderTraverse(0);
search(5);
}
public static void AddNode(){
Scanner sca = new Scanner(System.in);
System.out.println("input the data to be Added");
int data = sca.nextInt();
if(FreeNode<=19){//如果
int newPtr=FreeNode;
ArrayNodes[newPtr]=new node(-1,data,-1);
if(RootPointer==-1){//如果当前为空
RootPointer=newPtr;
}
else{
Boolean placed=false;
int CurrentNode=RootPointer;
while(placed==false){
if(dataelement){
nownode=ArrayNodes[nownode].leftPointer;
}
else{
nownode=ArrayNodes[nownode].rightPointer;
}
}
System.out.println("not found");
}
static class node{
int leftPointer;
int data;
int rightPointer;
public node(int lp,int da, int rp){
this.leftPointer=lp;
this.rightPointer=rp;
this.data=da;
}
}
}
Past paper questions
Describe ways in which a queue differs from a stack:
In a stack the last item in is the first out/LIFO and in a queue the first item in is the first out/FIFO
Queue can be circular, but a stack is linear
Stack only needs a pointer to the top (and can have a base pointer, and a queue needs a pointer to the front and the rear