ഇൻസർഷൻ സോർട്ടിൽ ഇടതുവശത്ത് ഇതിനകം ക്രമത്തിലാക്കിയ ഭാഗത്തേക്ക് ഓരോ മൂല്യമായി ചേർക്കും. വലിയ മൂല്യങ്ങൾ ഓരോ സ്ഥാനം വലത്തേക്ക് നീക്കി, ഒഴിവായ സ്ഥാനത്ത് അടുത്ത മൂല്യം വയ്ക്കും. പട്ടിക നേരത്തേ ക്രമത്തിലാണെങ്കിൽ സമയം Θ(n). മൂല്യങ്ങൾ ക്രമമില്ലാതെ വരുമ്പോൾ ശരാശരി സമയം Θ(n²). വിപരീതക്രമത്തിലാണ് ഏറ്റവും കൂടുതൽ സമയം വേണ്ടത്. അപ്പോഴും സമയം പട്ടികയുടെ നീളത്തിന്റെ വർഗത്തിന് അനുസരിച്ച് കൂടും.
ഒരു ലളിതമായ ഉദാഹരണം
3ന് മുമ്പിൽ 1 ചേർക്കുക. തുടർന്ന് 2 ചേർക്കാനായി 3 വീണ്ടും വശത്തേക്ക് നീക്കുക. ക്രമത്തിലായ സംഖ്യകൾ സ്ക്രീനിൽ കാണിക്കുക.
തുടക്കത്തിൽ3 1 2
ഫലം1 2 3
main.go
package main
import "fmt"
func main() {
values := []int{3, 1, 2}
i := 1
for i < 3 {
key := values[i]
j := i - 1
for (j >= 0) && (values[j] > key) {
values[j + 1] = values[j]
j -= 1
}
values[j + 1] = key
i += 1
}
i = 0
for i < 3 {
fmt.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²)
Go ഭാഷയിൽ ഇൻസർഷൻ സോർട്ട്
Go സോഴ്സ് കോഡ്
package main
import (
"fmt"
"strconv"
)
func insertionSort(arr []int) {
for i := 0; i < len(arr); i++ {
var temp = arr[i]
var holePosition = i
for holePosition > 0 && arr[holePosition - 1] > temp {
arr[holePosition] = arr[holePosition - 1]
holePosition--
}
arr[holePosition] = temp
}
}
func printArray(arr []int) {
for i := 0; i < len(arr); i++ {
fmt.Print(strconv.Itoa(arr[i]) + " ")
}
fmt.Println("")
}
func main() {
var arr = []int { 15, 3, 12, 6, -9, 9, 0 }
fmt.Print("Before Sorting: ")
printArray(arr)
insertionSort(arr)
fmt.Print("After Sorting: ")
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] ആണ്.
ബാക്കിയുളള ഓരോ മൂല്ല്യത്തിനും, അത് ഇടുന്നതിനു് മുമ്പുതന്നെ വലിയ വിലകള് മാറ്റുക. തുല്യ മൂല്യങ്ങൾ അവരുടെ യഥാര്ത്ഥ ക്രമം പാലിക്കുക.
Go ഭാഷയിൽ ഇൻസർഷൻ സോർട്ട് ഘട്ടം ഘട്ടമായി
Go സോഴ്സ് കോഡ്
package main
import (
"fmt"
"strconv"
)
func insertionSort(arr []int) {
// Un-sorted array from position 1 to length -1
for i := 1; i < len(arr); i++ {
// Take the left most item in an un-sorted array to temp variable
var temp = arr[i]
// Mark the left most position as empty or hole
var 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
for 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
}
}
func printArray(arr []int) {
// Loop the item and print each item in the console with white space seperated
for i := 0; i < len(arr); i++ {
fmt.Print(strconv.Itoa(arr[i]) + " ")
}
fmt.Println("")
}
func main() {
var arr = []int { 15, 3, 12, 6, -9, 9, 0 }
fmt.Print("Before Sorting: ")
printArray(arr)
insertionSort(arr)
fmt.Print("After Sorting: ")
printArray(arr)
}
ഔട്ട്പുട്ട്
Before Sorting: 15 3 12 6 -9 9 0
After Sorting: -9 0 3 6 9 12 15