Commit c2b2c4d
authored
merge_sort.py
Here after looking the actual code, I found out that the storing the sorted element in the result one by one on comparing two lists, that is left and right, it was using the pop() function for the seek of reducing the line of code. I wanted to alert about the pop function, as pop function in python extract the element on particular valid index from the list, mentioning in argument (by default the last element of the list). But pop off the first element takes O(n) complexity (where n is the length of the list) which leads to extra time complexity, also kind of making worst case for already sorted list.
Also, there is no need of extracting element, instead we just access the element through pointer ( left_index , right_index )1 parent 2cbe01a commit c2b2c4d
1 file changed
Lines changed: 14 additions & 4 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
37 | 37 | | |
38 | 38 | | |
39 | 39 | | |
40 | | - | |
41 | | - | |
42 | | - | |
43 | | - | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
44 | 54 | | |
45 | 55 | | |
46 | 56 | | |
| |||
0 commit comments