,,Lg1% 1:55 AM Assignment 3 The bag daia sructure is a data strunture for starin
ID: 3729340 • Letter: #
Question
,,Lg1% 1:55 AM Assignment 3 The bag daia sructure is a data strunture for staring the data in an unordered lsore bag data slrl, lhe inser Turiclion laku:s 01) and the remove tunction takes 01) However thesearch functan for a large bag a lot cf different alucs is 0Irnil Dcsi u suund ta dlucluc with the same s first bag data structure that has a search time of Og n) but the Inseri rdrewe lim ul C(ri. This special ba ata structure might be very useful in the real Ife. Suppose there is a lst of Items for sale. Ihe pecitication ct the again and again For multiple times) Hawever ramoval and insertion to the ltem list may happen very irilrecuenty This ussigi exurnplol a data structure with the sa e capahilities nf the othebu different performance hat is cdeslgned for aExplanation / Answer
To search the element in o(log n) we will keep the list sorted and bec of this new insertion will take O(n) to the find the new index and deletion ill take O(n) to shift the index of all the elemnents after deleted elemnts by -1.
Code in Python
# bag to store words
s = []
def insert(s,element):
i=0
while i<len(s):
if element<=s[i]:
break
s.insert(i,element)
def search(s,element):
def delete(s,element):
i=search(s,element)
del s[i]
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.