इन्सर्शन सॉर्ट डावीकडच्या क्रमाने लावलेल्या भागात एका वेळी एक मूल्य जोडतो. मोठी मूल्यं एक जागा उजवीकडे सरकवतो आणि मोकळ्या जागी पुढचं मूल्य ठेवतो. यादी आधीच क्रमाने असेल तर वेळ Θ(n) असतो. मूल्यं कोणत्याही क्रमाने आली तर सरासरी वेळ Θ(n²) असतो. उलट्या क्रमात सर्वाधिक वेळ लागतो आणि तोही यादीच्या लांबीच्या वर्गाप्रमाणे वाढतो.
एक सोपे उदाहरण
3 च्या आधी 1 घाला. मग 2 साठी जागा करायला 3 पुन्हा बाजूला सरकवा. क्रमाने लागलेल्या संख्या स्क्रीनवर दाखवा.
सुरुवातीला3 1 2
निकाल1 2 3
lesson.py
values = [3, 1, 2]
i = 1
while i < 3:
key = values[i]
j = i - 1
while j >= 0 and values[j] > key:
values[j + 1] = values[j]
j -= 1
values[j + 1] = key
i += 1
i = 0
while i < 3:
print(values[i])
i += 1
आउटपुट
1
2
3
रोजच्या आयुष्यातील उदाहरणे
नवं तिकीट योग्य जागी घाला
एक कारकून तिकिटे 2,5 आणि 8 क्रमाने ठेवतो. तिकीट 6 कुठे जावे?
सुरुवातीला2 5 8 +6
निकाल2 5 6 8
8 उजवीकडे सरकवा. 5 नंतरचे उघडे ठिकाण म्हणजे समाविष्ट करण्याची स्थिती.
सकाळची भेट नोंदवा
तुमची डायरी 10 आणि 12 वाजता सुरू होते. 9 वाजता भेटीची वेळ जोडा.
सुरुवातीला10 12 +9
निकाल9 10 12
नंतरच्या दोन्ही भेटी जागा बनवण्यासाठी एक स्थान बदलतात.
उशिरा आलेलं पेमेंट शेवटी जोडा
क्रमवारी लावलेली खातावही 4 आणि 6 दिवसांसह संपते. 7व्या दिवसाचे पैसे येतात. काय बदल?
सुरुवातीला4 6 +7
निकाल4 6 7
शेवटचा विद्यमान दिवस आधीच 7 च्या आधी आहे. इतर नोंदी न बदलता ती जोडा.
लागणारे काम आणि मर्यादा
छोटी किंवा जवळपास क्रमात असलेली यादी लावण्यासाठी इन्सर्शन सॉर्ट चांगला चालतो. यादी उलट्या क्रमात असेल तर O(n²) वेळा घटक सरकवावे आणि त्यांची तुलना करावी लागते.
वेळेची गुंतागुंत
सर्वांत कमी वेळ लागणारी परिस्थिती: Θ(n)
सरासरी परिस्थिती: Θ(n²)
सर्वांत जास्त वेळ लागणारी परिस्थिती: Θ(n²)
Python मध्ये इन्सर्शन सॉर्ट
Python सोर्स कोड
class InsertionSortExample:
def __init__(self):
pass
def insertionSort(self, arr):
for i in range(len(arr)):
temp = arr[i]
holePosition = i
while(holePosition > 0 and arr[holePosition - 1] > temp):
arr[holePosition] = arr[holePosition - 1]
holePosition -= 1
arr[holePosition] = temp
return arr
def printArray(self, arr):
string = " ".join([str(item) for item in arr]);
print(string)
obj = InsertionSortExample()
arr = [15, 3, 12, 6, -9, 9, 0]
print("Before Sorting: ")
obj.printArray(arr)
result = obj.insertionSort(arr)
print("After Sorting: ")
obj.printArray(result)
आउटपुट
Before Sorting: 15 3 12 6 -9 9 0
After Sorting: -9 0 3 6 9 12 15
जाहिराती
उदाहरण टप्प्याटप्प्याने पाहा
[15, 3, 12, 6, -9, 9, 0] पासून सुरुवात करा.
सुरुवातीला क्रमात असलेल्या भागात फक्त 15 ठेवा. पुढचं 3 हे मूल्य बाजूला राखून ठेवा.
15 एक जागा उजवीकडे सरकवा आणि सुरुवातीला 3 ठेवा. आता यादीचा सुरुवातीचा भाग [3, 15] आहे.
बाकीची मूल्यं एकेक करून घाला. प्रत्येकाला जागा करताना त्याच्यापेक्षा मोठी मूल्यंच सरकवा. सारखी मूल्यं आपला आधीचा क्रम राखतात.
Python मध्ये इन्सर्शन सॉर्ट टप्प्याटप्प्याने समजून घ्या
Python सोर्स कोड
class InsertionSortExample:
def __init__(self):
pass
def insertionSort(self, arr):
# Un-sorted array from position 1 to length -1
for i in range(len(arr)):
# Take the left most item in an un-sorted array to temp variable
temp = arr[i]
# Mark the left most position as empty or hole
holePosition = i
# Traverse the sorted array from right to left direction
# Move the item from the sorted array to the empty position (i.e. holePosition) only if the temp variable is less then
# Mark the current item's position as empty by decrementing the value by one
while(holePosition > 0 and arr[holePosition - 1] > temp):
arr[holePosition] = arr[holePosition - 1]
holePosition -= 1
# Once the comparison done, move the value in temp variable to the lastly marked empty position
arr[holePosition] = temp
# Return the sorted array
return arr
def printArray(self, arr):
# Loop the item and print each item in the console with white space seperated
string = " ".join([str(item) for item in arr]);
print(string)
obj = InsertionSortExample()
arr = [15, 3, 12, 6, -9, 9, 0]
print("Before Sorting: ")
obj.printArray(arr)
result = obj.insertionSort(arr)
print("After Sorting: ")
obj.printArray(result)
आउटपुट
Before Sorting: 15 3 12 6 -9 9 0
After Sorting: -9 0 3 6 9 12 15