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$

Bubble Sort from Cormen

Insertion Sort from Cormen

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…

enter image description here

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.


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?

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?

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; }