Hi,

I' studying various sorting techniques...And one thing I'm not clear about the complexity of the techniques...

For e.g the complexity of bubble sort is

Average Case=>O(n*n)

Worst Case=>O(n*n)

Best Case=>O(n*n)

Obviously n is the no. of elements in the array...

But even in the worst case i.e the array having 5 elements in descending order would do only only 4+3+2+1 comparisons

and not O(n*n) which I expect is 5*5=25 comparisons

Can somebody please throw light on it??

himanjim
0
Junior Poster in Training

## Recommended Answers

Jump to PostMay be it helps you to see a little math ( cause, math doesn't lie ;-) ):

The pseudo-code for this algorithm has this structure:

`FOR I = 0 TO N-1 /* It cuts when it compares against N */ FOR J = 0 TO N-I-1 …`

## All 4 Replies

sj87
19
Light Poster

StuXYZ
commented:
Neatly explined.
+1

SRaj
0
Newbie Poster

ArkM
1,090
Postaholic

Freaky_Chris
299
Master Poster

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.