#Quantitatively Determining The Time Complexity Of A Sorting Algorithm

8 messages · Page 1 of 1 (latest)

unborn vault
#

Before asking the question, I wanted to give some background. Although time complexity can be empirically determined, I am trying to determine it quantitatively by using a variable to count the complexity. From there the size of the experimental data imposed in the algorithm will act as the x axis and the number of iterations / conditionals within the algorithm that increment the complexity count variable should reflect the y axis of the cartesian plane. This is what would generate the best fit curve (the regression analyis) that provides the growth function. With that growth function you can determine dominance to get your Big O.

My question is about where I should add the variable to count the complexity that will satisfy my use case.

In the below examples, complexityCount is counting the complexity.

proud sapphireBOT
#

Hey, @unborn vault!
Please remember to /close this post once your question has been answered!

unborn vault
#

Option one is to count like this: ```java
@Override
public <T extends Comparable<T>> int sort(List<T> arr) {
int complexityCount = 0;
n = arr.size();
T temp;

// Sorting strings using bubble sort
for (int i = 0; i < n - 1; i++) {
    complexityCount++;
    for (int j = 0; j < n - i - 1; j++) {
        complexityCount++;
        if (arr.get(j).compareTo(arr.get(j + 1)) > 0) {
            complexityCount++;
            temp = arr.get(j);
            arr.set(j, arr.get(j + 1));
            arr.set(j + 1, temp);
        }
    }
}
return complexityCount;

}```

#

Option two is to count like this:

#
@Override
public <T extends Comparable<T>> int sort(List<T> arr) {
    int complexityCount = 0;
    n = arr.size();
    T temp;

    // Sorting strings using bubble sort
    for (int i = 0; i < n - 1; i++) {     
        for (int j = 0; j < n - i - 1; j++) {
            complexityCount++;
            if (arr.get(j).compareTo(arr.get(j + 1)) > 0) {
                temp = arr.get(j);
                arr.set(j, arr.get(j + 1));
                arr.set(j + 1, temp);
            }
        }
    }
    return complexityCount;
}```
#

Option three is to count like this:

#
@Override
public <T extends Comparable<T>> int sort(List<T> arr) {
    int complexityCount = 0;
    n = arr.size();
    T temp;

    // Sorting strings using bubble sort
    for (int i = 0; i < n - 1; i++) {     
        for (int j = 0; j < n - i - 1; j++) {
            if (arr.get(j).compareTo(arr.get(j + 1)) > 0) {
                complexityCount++;
                temp = arr.get(j);
                arr.set(j, arr.get(j + 1));
                arr.set(j + 1, temp);
            }
        }
    }
    return complexityCount;
}```