how to calculate Bubble sort Time Complexity

前端 未结 7 1254
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-05 12:29

I was trying to understand the Data Structure and different algorithm, then i got confused to measure the Bubble sort time complexity.

for (c = 0; c < ( n         


        
7条回答
  •  情歌与酒
    2020-12-05 12:37

    it does comparison between two elements. so in 1st Phase - n-1 comparison. i.e, 6 2nd phase - n-2 comparison. i.e, 5 and so on till 1. and thus, sum = n(n-1)/2 i.e O(n^2).

    if there is any error you can correct.....

提交回复
热议问题