इंसर्शन सॉर्ट बाईं ओर की सजी हुई सूची में एक बार में एक मान जोड़ता है। बड़े मानों को एक जगह दाईं ओर खिसकाएँ और खाली जगह पर अगला मान रखें। सूची पहले से सजी हो तो समय Θ(n) होता है। बेतरतीब क्रम में औसत समय Θ(n²) होता है। उलटा क्रम सबसे ज़्यादा समय लेता है और उसका समय भी सूची की लंबाई के वर्ग के हिसाब से बढ़ता है।
एक आसान उदाहरण
1 को 3 से पहले रखें। फिर 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