2019 Q4 (Bubblesort)

As before these may not get 100% due to them being answers that I would give in the exam should it come up.

a) Explain in plain English the Bubblesort algorithm

In Bubblesort you select two elements that are beside eachother and compare them, if the one that is further to the right is smaller than the one further to the left, we swap them round and then repeat till you reach the end of the list. You then repeat until you are able to move through the list without swapping any numbers around.

b) Provide a pseudo-code representation of the Bubblesort algorithm

As before this is closer to C then english but that is the way that I work through things for the most part.

bubbleSort(int set[], int size){
    int temp;
    int check = 1;
    while(check){
        check = 0;
        for(int i = 0, j = 1; j < size; i++, j++){
            if(set[i] > set[j]){
                check = 1;
                temp = set[i];
                set[i] = set[j];
                set[j] = temp;
            }
        }
    }
}

c) Provide an implementation of your Bubblesort algorithm in the C programming language

void bubblesort(int set[], int size){
    int temp;
    int check = 1;
    while(check){
        check = 0;
        for(int i = 0, j = 1; j < size; i++, j++){
            if(set[i] > set[j]){
                check = 1;
                temp = set[i];
                set[i] = set[j];
                set[j] = temp;
            }
        }
    }
}

d) Compare the efficiency of Bubblesort and merge sort for a random array & a sorted array with a single appended element

Bubblesort, in the best case scenario where you only have to wap one or two numbers it can be more efficient but it can be the least efficient if you need to move an awful lot of numbers. To bring this back to the question, Bubblesort can be more efficient with the sorted array with one appended element in a best case scenario but otherwise Mergesort is more efficient.