इंसर्शन सॉर्ट बाईं ओर की सजी हुई सूची में एक बार में एक मान जोड़ता है। बड़े मानों को एक जगह दाईं ओर खिसकाएँ और खाली जगह पर अगला मान रखें। सूची पहले से सजी हो तो समय Θ(n) होता है। बेतरतीब क्रम में औसत समय Θ(n²) होता है। उलटा क्रम सबसे ज़्यादा समय लेता है और उसका समय भी सूची की लंबाई के वर्ग के हिसाब से बढ़ता है।
एक आसान उदाहरण
1 को 3 से पहले रखें। फिर 2 के लिए जगह बनाने को 3 को दोबारा खिसकाएँ। क्रम में लगी संख्याएँ दिखाएँ।
शुरुआत में3 1 2
नतीजा1 2 3
lesson.c
#include <stdbool.h>
#include <stdio.h>
int main(void) {
int values[] = {3, 1, 2};
int i = 1;
while (i < 3) {
int key = values[i];
int 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) {
printf("%d\n", 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²) बार खिसकाना और तुलना करना पड़ता है।
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] है।
प्रत्येक शेष मूल्य के लिए, इसे डालने से पहले केवल बड़े मूल्यों को स्थानांतरित करें। समान मूल्य उनके मूल आदेश को बनाए रखते हैं।
C में इंसर्शन सॉर्ट को कदम-दर-कदम समझें
C का सोर्स कोड
#include<stdio.h>
void insertionSort(int arr[], size_t size) {
// Un-sorted array from position 1 to size -1
for(size_t i = 1; i < size; i++) {
// Take the left most item in an un-sorted array to temp variable
int temp = arr[i];
// Mark the left most position as empty or hole
int 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;
}
}
void printArray(const int arr[], size_t size) {
// Loop the item and print each item in the console with white space seperated
for(size_t i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}
int main(int argc, const char * argv[]) {
int arr[] = { 15, 3, 12, 6, -9, 9, 0 };
const size_t SIZE = sizeof(arr) / sizeof(*arr);
printf("Before Sorting: ");
printArray(arr, SIZE);
insertionSort(arr, SIZE);
printf("After Sorting: ");
printArray(arr, SIZE);
return 0;
}
आउटपुट
Before Sorting: 15 3 12 6 -9 9 0
After Sorting: -9 0 3 6 9 12 15