Constructs/Algorithms Flashcards

1
Q

Code an insertion sort

A

For i = 1 to length(list)

Value = list(i)     

Index = i 

Do while Index > 0             

    If Value < list(Index - 1) Then             

        list(Index) = list(Index - 1)             

        Index = Index - 1         

    Else            

        Exit do         

    End if  

Loop    

list(Index) = value 

Next i

For i = 0 to length(list)

lstdisplay.additem list(i) 

Next i

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Describe an insertion sort

A

This is when the first value is labelled the sorted side and the second the unsorted and the unsorted is either switched or remains once this is determined the value is now sorted and then next unsorted goes through the same process till the whole list is sorted. Much more efficient on longer lists than bubble

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Description of 2-D arrays

A

They are made from a grid/table of data, made from rows and columns, of the same data type and can be accessed uniquely using the array name and two indexes. They are traversed using nested if statements.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How to initialise a 2-D array

A

For row = 0 to 5

For column = 0 to 5        

    data(row, column) = Inputbox(”Please input data”)     

Next column 

Next row

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How to display a 2-D array

A

For row = 0 to 5

For column = 0 to 5         

    PicDisplay.Print Tab(column * 10); data(row,column);     

Next column 

Next row

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Describe a single/double linked list

A

Linked list contain nodes that hold data, their own address and a pointer to the next node address. The first one is the head and the last node points to null. Double linked lists have the address of the previous node stored too making it easier to insert/delete nodes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Describe binary search

A

A binary search algorithm searches by halving the list till the target is found therefore the list must be sorted first. When working with parallel arrays they all must be kept together.

The code begins with a start and end point this helps calculate the middle of the list.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Code a binary search

A

low = 0

high = length(list) - 1

found = false

Do

mid = (low + high)   

If target = list(mid) Then         

    fposition = mid         

    found = true     

End if    

If mid > target Then         

    low = mid + 1     

End if  

If mid < target Then         

    high = mid - 1     

End if 

Loop until found = true Or low>=high

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Describe a bubble sort

A

This is when the first two values are compared and switched if necessary and this is then repeated all the way down the list till the one value is sorted this is then continued with all the other values.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Code a bubble sort

A

n = length(list)

swapped = true

While swapped = true and n>=0 Then

swapped = false 

For i = 0 to n-2                 

    If list(i) > list(i+1) Then             

        temp = list(i)             

        list(i) = list(i+1)             

        list(i+1) = temp             

        swapped = true         

    End if     

Next i 

n = n-1 

End while

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How to add/delete a node from a single/double linked list

A

To insert a node in a linked list first the new node must be made to contain the data and must be given a random unique address this will become the pointer for the previous node and the new nodes pointer will be the next nodes address.

This is the same for a double linked list however the new node will need to know the previous nodes address too.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly