A-Levels Computer Science Practical Codes

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

linear search:

#to find index at which value is stored


def linearsearch(array,len,value):
for i in range(len):
if array[i]==value:
return i
return -1
binary search:

#to find index at which value is stored


def binarysearch(array,value,first,last):
while first<=last:
mid=(first+last)//2
if array[mid]==value:
return mid
elif array[mid]<value:
first=mid+1
elif array[mid]>value:
last=mid-1
return -1
insertion sort:

#ascending sort
def insertionsort(array):
for i in range(1,len(array)):
value=array[i]
current=i-1
while array[current]>value and current>-1:
array[current+1]=array[current]
current=current-1
array[current+1]=value

#descending sort
def insertionsort(array):
for i in range(1,len(array)):
value=array[i]
current=i-1
while array[current]<value and current>-1:
array[current+1]=array[current]
current=current-1
array[current+1]=value
bubble sort:

#ascending order
def bubblesort(array):
for passes in range(len(array)):
for swap in range(len(array)-1):
if array[swap]>array[swap+1]:
temp=array[swap]
array[swap]=array[swap+1]
array[swap+1]=temp

#descending order
def bubblesort(array):
for passes in range(len(array)):
for swap in range(len(array)-1):
if array[swap]<array[swap+1]:
temp=array[swap]
array[swap]=array[swap+1]
array[swap+1]=temp
linked list:

#create new linked list


class node:
def __init__(self,dataN,point):
self.data=dataN
self.pointer=point

def initialiseList():
list=[node(0,0)]*len
startPointer=-1
emptyPointer=0 #or index where free list starts
for i in range(len(list)):
list[i].pointer=i+1
list[len].pointer=-1

#insert new node into ordered linked list


def InsertNode(newValue):
if emptyPointer!=-1:
newPointer=emptyPointer
list[newPointer].data=newValue
emptyPointer=list[emptyPointer].pointer
currentPointer=startPointer
previousPointer=-1
while currentPointer!=-1: and list[currentPointer].data<newValue:
previousPointer=currentPointer
currentPointer=list[currentPointer].pointer
if previousPointer==startPointer:
list[newPointer].pointer=startPointer
startPointer=newPointer
else:
list[newPointer].pointer=list[previousPointer].pointer
list[previousPointer].pointer=newPointer

#search value in ordered linked list


def findNode(search):
currentPointer=startPointer
while currentPointer!=-1 and list[currentPointer].data!=search:
currentPointer=list[currentPointer].pointer
return currentPointer

#delete node from ordered linked list


def deleteNode(value):
currentPointer=startPointer
while currentPointer!=-1 and list[currentPointer].data!=value:
previousPointer=currentPointer
currentPointer=list[currentPointer].pointer
if currentPointer!=-1:
if currentPointer==startPointer:
startPointer=list[startPointer].pointer
else:
list[previousPointer].pointer=list[currentPointer].pointer
list[currentPointer].pointer=emptyPointer
emptyPointer=currentPointer

#print linked list


def printList(list):
currentPointer=startPointer
while currentPointer!=-1:
print(list[currentPointer].data)
currentPointer=list[currentPointer].pointer
binary tree:
stack:

emptystring=””
nullpointer=-1
maxsize=8 #or whatever length asked
array=[“”]*maxsize
base=0
top=nullpointer

#push item onto stack


def push(value):
global top
if top<maxsize-1:
top=top+1
array[top]=value
else:
print(“stack full”)

#pop item from stack


def pop():
value=emptystring
global top
if top>nullpointer:
array[top]=value
top=top-1
else:
print(“stack empty”)
return value
queue:

emptystring=””
nullpointer=-1
maxsize=8 #or whatever length asked
queue=[“”]*maxsize

def initialize():
global frontPointer
global endPointer
global numQueue
frontPointer=0
endPointer=nullpointer
numQueue=0

#add item to queue


def enqueue(value):
global numQueue
global endPointer
if numQueue<maxsize:
endPointer=endPointer+1
if endPointer>maxsize-1:
endpointer=0
queue[endPointer]=value
numQueue=numQueue+1

#remove item from queue


def dequeue():
global numQueue
global frontPointer
global temp
value=emptystring
if numQueue>0:
temp=queue[frontPointer]
queue[frontPointer]=value
numQueue=numQueue-1
if numQueue==0:
initialize()
else:
frontPointer=frontPointer+1
if frontPointer>maxsize-1
frontPointer=0
return value
file handling:

#write to file (starts anew)


file=open(“example.txt”,”w”)
for i in range(5):
first=input(“enter word: “)
file.write(first)
file.close()

#read file
file=open(“example.txt”,”r”)
eof=(file.readline()).strip() #.strip() gets rid of spaces after line
while eof!=””:
line=(file.readline()).strip()
file.close()

#append to end of file


file=open(“example.txt”,”a”)
for i in range(5):
first=input(“enter word: “)
file.write(first)
file.close()
oop:

You might also like