## Bubble Sort: Runtime complexity analysis like Cormen does

I’m trying to analyze Bubble Sort runtime in a method similar to how Cormen does in "Introduction to Algorithms 3rd Ed" for Insertion Sort (shown below). I haven’t found a line by line analysis like Cormen’s analysis of this algorithm online, but only multiplied summations of the outer and inner loops.

For each line of bubblesort(A), I have created the following times run. Appreciate any guidance if this analysis is correct or incorrect. If incorrect, how it should be analyzed. Also, I do not see the best case where $$T(n) = n$$ as it appears the inner loop always runs completely. Maybe this is for "optimized bubble" sort, which is not shown here?

Times for each line with constant run time $$c_n$$, where $$n$$ is the line number:

Line 1: $$c_1 n$$

Line 2: $$c_2 \sum_{j=2}^n j$$

Line 3: $$c_3 \sum_{j=2}^n j – 1$$

Line 4: $$c_4 \sum_{j=2}^n j – 1$$ Worst Case

$$T(n) = c_1 n + c_2 (n(n+1)/2 – 1) + c_3 (n(n-1)/2) + c_4 (n(n-1)/2)$$

$$T(n) = c_1 n + c_2 (n^2/2) + c_2 (n/2) – c2 + c_3 (n^2/2) – c_3 (n/2) + c_4 (n^2/2) – c_4 (n/2)$$

$$T(n) = (c_2/2+c_3/2+c_4/2) n^2 + (c_1 + c_2/2+c_3/2+c_4/2) n – c_2$$

$$T(n) = an^2 + bn – c$$  Posted on Categories proxies

## Will an outsider killed within a planar bubble of its native plane be permanently slain?

If an outsider has the spell Planar Bubble (Spell Compendium, p.158) cast upon it and is then killed, will it be permanently slain as though it had died on its native plane?

Posted on Categories Best Proxies

## Prove by induction that the recurrence form of bubble sort is $\Omega(n^2)$

The recurrence form of bubble sort is $$T(n)=T(n-1)+ n- 1$$

How can I prove by induction that this is $$\Omega(n^2)$$?

I’m stuck with $$T(n+1) \geq cn^2 + n = n(cn+1)$$

Posted on Categories proxies

## How to place bubbles in bubble shooter game?

How are the layouts generated in bubble shooter game? How are those blocks(shape) of same colour generated and where are they placed?

## Bubble sort: how to calculate amount of comparisons and swaps

For a given sequence 1, N ,2 ,N −1 ,3, N −2, … I want to calculate the number of comparisons and swaps for bubble sort. How can I accomplish that using $$\theta ()$$ notation? I would know how to do it for any sequence, but not for a given one.

Posted on Categories proxies

## Creating a “bubble” UI layout like used in the Apple Music app

I’ve been thinking about experimenting with a layout like the one used in the Apple Music app on iOS.

The interface is the “bubble” interface that is used to choose your preferences etc… But I don’t really know where to even start with this. Is there a name for the general idea of creating these sort of semi-physics based layouts?

Would love to know what it is I need to search for in order to learn how these work.

I’d be writing it on iOS in Swift but looking more for the name of this sort of interface so I can learn the principals behind creating it.

Thanks

## Recursive Bubble Sort Complexity

I have this code for a recursive bubble sort:

def bubble_sort_recursive(list_):     global counter     for i in range(len(list_)):         counter += 1         try:             if list_[i + 1] < list_[i]:                 list_[i], list_[i + 1] = list_[i + 1], list_[i]                 bubble_sort_recursive(list_)         except IndexError:             continue     return list_ 

The counter is initialized with zero outside the function, so I can count the number of times the elements were iterated.

I was told that the complexity of bubble sort is O(N^2). But when I run with a reversed list (worst case) with size 3, the counter finishes with the value 12, shouldn’t it be 9?

The same happens for other sizes: 4 -> 28 5 -> 55

What is happening?

Posted on Categories proxies

## Why is the number of swaps done by bubble sort to sort the queue not equal to the number of bribes?

I was going through this challenge on HackerRank. The reason I’m asking this here is that at the heart of it, it’s basically a CS question.

It’s New Year’s Day and everyone’s in line for the Wonderland rollercoaster ride! There are a number of people queued up, and each person wears a sticker indicating their initial position in the queue. Initial positions increment $$1$$ by $$1$$ from at the front of the line to $$n$$ at the back.

Any person in the queue can bribe the person directly in front of them to swap positions. If two people swap positions, they still wear the same sticker denoting their original places in line. One person can bribe at most two others. For example, if $$n=8$$ and $$\text{Person 5}$$ bribes $$\text{Person 4}$$, the queue will look like this: $$1, 2, 3, 5, 4, 6, 7, 8$$.

Fascinated by this chaotic queue, you decide you must know the minimum number of bribes that took place to get the queue into its current state!

Now, my approach to this problem was to first check if the current queue is “chaotic”: we start from the end of the array $$A$$. If $$A_i$$ is not equal to $$i$$, where $$1\leq i \leq n$$, then we know that the last element must have bribed and moved towards the left since it cannot move to the right being the last element. Also, we know that it will be present either in position $$i-1$$ or $$i-2$$. This is because if it is in the position left to, he must have bribed more than 2 people.

If the queue is not chaotic, I applied the bubble sort technique to count the number of swaps required to sort the list. This I thought would equal the minimum number of bribes. However, this approach does not pass all the test cases; any idea why?

## Worst Case Space Complexity of Merge Sort and Bubble Sort

I understand that the worst space complexity of Bubble Sort is constant O(1), since all the space we need is the array where the elements were stored. But why is Merge Sort’s worst space complexity O(n), linear? All space we need is the exact number of the elements, right? Please enlighten me.

## Bubble sort em C inconsistente

Estou tentando criar a estrutura básica para um programa que utiliza o Bubble Sort. Porém, ao testar sua estrutura básica, vejo que o vetor final não está sendo retornado corretamente.

#include <stdio.h> #define TAMANHO 5  int main() {      int vetor[TAMANHO-1],n1,n2,ctr,ctrInt;      for(ctr=0;ctr<TAMANHO;ctr++) scanf("%d",&vetor[ctr]);      for(ctr=0;ctr<TAMANHO;ctr++){         for(ctrInt=0;ctrInt<TAMANHO-ctr;ctrInt++){         n1=vetor[ctrInt];         n2=vetor[ctrInt+1];             if(n1>n2){                 vetor[ctrInt]=n2;                 vetor[ctrInt+1]=n1;             }         }     }      for(ctr=0;ctr<TAMANHO;ctr++) printf("%d",vetor[ctr]);      return 0; }