इन्सर्शन सॉर्ट डावीकडच्या क्रमाने लावलेल्या भागात एका वेळी एक मूल्य जोडतो. मोठी मूल्यं एक जागा उजवीकडे सरकवतो आणि मोकळ्या जागी पुढचं मूल्य ठेवतो. यादी आधीच क्रमाने असेल तर वेळ Θ(n) असतो. मूल्यं कोणत्याही क्रमाने आली तर सरासरी वेळ Θ(n²) असतो. उलट्या क्रमात सर्वाधिक वेळ लागतो आणि तोही यादीच्या लांबीच्या वर्गाप्रमाणे वाढतो.
एक सोपे उदाहरण
3 च्या आधी 1 घाला. मग 2 साठी जागा करायला 3 पुन्हा बाजूला सरकवा. क्रमाने लागलेल्या संख्या स्क्रीनवर दाखवा.
सुरुवातीला3 1 2
निकाल1 2 3
Lesson.groovy
def values = [3, 1, 2]
def i = 1
while (i < 3) {
def key = values[i]
def j = i - 1
while ((j >= 0) && (values[j] > key)) {
values[j + 1] = values[j]
j -= 1
}
values[j + 1] = key
i += 1
}
i = 0
while (i < 3) {
println(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²)
Groovy मध्ये इन्सर्शन सॉर्ट
Groovy सोर्स कोड
class InsertionSortExample {
public void insertionSort(arr) {
def lastIndex = arr.length - 1
for (i in 0..lastIndex) {
def temp = arr[i]
def holePosition = i
while(holePosition > 0 && arr[holePosition - 1] > temp) {
arr[holePosition] = arr[holePosition - 1]
holePosition--
}
arr[holePosition] = temp
}
}
public void printArray(arr) {
def lastIndex = arr.length - 1
for (i in 0..lastIndex) {
def num = arr[i]
print(num + " ")
}
println("")
}
public static void main(String[] args) {
def obj = new InsertionSortExample()
def arr = [15, 3, 12, 6, -9, 9, 0].toArray()
print("Before Sorting: ")
obj.printArray(arr)
obj.insertionSort(arr)
print("After Sorting: ")
obj.printArray(arr)
}
}
आउटपुट
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] आहे.
बाकीची मूल्यं एकेक करून घाला. प्रत्येकाला जागा करताना त्याच्यापेक्षा मोठी मूल्यंच सरकवा. सारखी मूल्यं आपला आधीचा क्रम राखतात.
Groovy मध्ये इन्सर्शन सॉर्ट टप्प्याटप्प्याने समजून घ्या
Groovy सोर्स कोड
class InsertionSortExample {
public void insertionSort(arr) {
def lastIndex = arr.length - 1
// Un-sorted array from position 1 to length -1
for (i in 0..lastIndex) {
// Take the left most item in an un-sorted array to temp variable
def temp = arr[i]
// Mark the left most position as empty or hole
def 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 && arr[holePosition - 1] > temp) {
arr[holePosition] = arr[holePosition - 1]
holePosition--
}
// Once the comparison done, move the value in temp variable to the lastly marked empty position
arr[holePosition] = temp
}
}
public void printArray(arr) {
def lastIndex = arr.length - 1
// Loop the item and print each item in the console with white space seperated
for (i in 0..lastIndex) {
def num = arr[i]
print(num + " ")
}
println("")
}
public static void main(String[] args) {
def obj = new InsertionSortExample()
def arr = [15, 3, 12, 6, -9, 9, 0].toArray()
print("Before Sorting: ")
obj.printArray(arr)
obj.insertionSort(arr)
print("After Sorting: ")
obj.printArray(arr)
}
}
आउटपुट
Before Sorting: 15 3 12 6 -9 9 0
After Sorting: -9 0 3 6 9 12 15