Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions Sprint-2/implement_skip_list/skip_list.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
class SkipList:
def __init__(self):
self.items = []

def insert(self, value):
left = 0
right = len(self.items)

while left < right:
mid = (left + right) // 2

if self.items[mid] < value:
left = mid + 1
else:
right = mid

self.items.insert(left, value)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The complexity of Python's list.insert() method is $O(n)$ in the worst case.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The complexity of Python's list.insert() method is O ( n ) in the worst case.

YES, You're right. I used binary search to find the insertion position, but I overlooked the fact that Python's list.insert() still has O(n) complexity because elements may need to be shifted. This means my implementation does not achieve the insertion complexity expected from a true Skip List.


def __contains__(self, value):
left = 0
right = len(self.items) - 1

while left <= right:
mid = (left + right) // 2

if self.items[mid] == value:
return True

if self.items[mid] < value:
left = mid + 1
else:
right = mid - 1

return False

def to_list(self):
return list(self.items)
Loading