Using the Bubble Sort Time Complexity

  • (2 Pages)
  • +
  • 1
  • 2

21 Replies - 4754 Views - Last Post: 15 March 2014 - 09:09 PM

#16 shamieh   User is offline

  • D.I.C Head

Reputation: -4
  • View blog
  • Posts: 146
  • Joined: 14-September 13

Re: Using the Bubble Sort Time Complexity

Posted 14 March 2014 - 10:57 AM

Thank you so much! you all are so helpful!!
Was This Post Helpful? 0
  • +
  • -

#17 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7915
  • View blog
  • Posts: 26,425
  • Joined: 05-May 12

Re: Using the Bubble Sort Time Complexity

Posted 15 March 2014 - 08:23 PM

Why aren't the comparisons on lines 3 and 4 counted? They too are comparisons right? Or is there a magic rule when computing complexity that loop condition comparisons don't count?
Was This Post Helpful? 0
  • +
  • -

#18 shamieh   User is offline

  • D.I.C Head

Reputation: -4
  • View blog
  • Posts: 146
  • Joined: 14-September 13

Re: Using the Bubble Sort Time Complexity

Posted 15 March 2014 - 08:26 PM

j - size - 1 - i is in the second condition. But I only need to count after line 4 as far as "comparisons"
Was This Post Helpful? 0
  • +
  • -

#19 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7915
  • View blog
  • Posts: 26,425
  • Joined: 05-May 12

Re: Using the Bubble Sort Time Complexity

Posted 15 March 2014 - 08:36 PM

Why?

So if the code were written as:
int i = 0;
int j = 0;
while (true)
{
    if (items[j] > items[j+1])
        Swap(j, j + 1);
    j++
    if (j >= j - 1 - i)       // does this comparison not count?
    {
        j = 0;
        i++
        if (i >= size - 1)    // does this comparison not count?
            break;
    }
}


Was This Post Helpful? 0
  • +
  • -

#20 shamieh   User is offline

  • D.I.C Head

Reputation: -4
  • View blog
  • Posts: 146
  • Joined: 14-September 13

Re: Using the Bubble Sort Time Complexity

Posted 15 March 2014 - 08:51 PM

So if I had for example a list of these numbers : 5 6 7 8 1 2 4 10 23

AFTER i sorted them with bubble sort I would obtain these results:

New list: 1 2 4 5 6 7 8 10 23

Theoretical Sort Statistics: 45 element comparisons, 9 element movements
Actual Sort Statistics: 36 element comparisons, 12 movements

The Theoretical comparisons would be the average case time complexity of Bubble sort with n being the number of elements in the List.
Bubble Sort Avg TC = (n(n+1)) / 2

Thus: 9 elements so, (9(9+1)) / 2 = 90 / 2 = 45 element comparisons, and 9 movements because we have 9 elements.

In addition, if I used Actual Sort statistics, I would count every single comparison made, thus :
every literal comparison made would be 36 (even ones already in order) and actual movements would be literal SWAPS

This post has been edited by shamieh: 15 March 2014 - 08:54 PM

Was This Post Helpful? 0
  • +
  • -

#21 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7915
  • View blog
  • Posts: 26,425
  • Joined: 05-May 12

Re: Using the Bubble Sort Time Complexity

Posted 15 March 2014 - 08:55 PM

Ah, I see. You are counting "element comparisons", not just any "comparison".

Umm. As a quick aside, you do know that the code you posted is not Bubble Sort, right?

Read about The Real Bubble Sort.
Was This Post Helpful? 1
  • +
  • -

#22 shamieh   User is offline

  • D.I.C Head

Reputation: -4
  • View blog
  • Posts: 146
  • Joined: 14-September 13

Re: Using the Bubble Sort Time Complexity

Posted 15 March 2014 - 09:09 PM

Wow...That is very interesting. Wait until I tell my teacher about this after spring break :online2long:
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2