ઇન્સર્શન સૉર્ટ ડાબી બાજુના ગોઠવાયેલા ભાગમાં એક વખતે એક મૂલ્ય ઉમેરે છે. મોટાં મૂલ્યોને એક સ્થાન જમણી બાજુ ખસેડીને ખાલી જગ્યાએ આગળનું મૂલ્ય મૂકે છે. યાદી પહેલેથી ગોઠવાયેલી હોય તો સમય Θ(n) છે. મૂલ્યો આડાઅવળા ક્રમમાં હોય તો સરેરાશ સમય Θ(n²) છે. ઊલટા ક્રમમાં સૌથી વધારે સમય લાગે છે અને તે પણ યાદીની લંબાઈના વર્ગ પ્રમાણે વધે છે.
એક સરળ ઉદાહરણ
3ની પહેલાં 1 મૂકો. પછી 2 મૂકવા માટે 3ને ફરી એક જગ્યા ખસેડો. ગોઠવાયેલી સંખ્યાઓ સ્ક્રીન પર બતાવો.
શરૂઆતમાં3 1 2
પરિણામ1 2 3
lesson.rb
values = [3, 1, 2]
i = 1
while i < 3
key = values[i]
j = i - 1
while (j >= 0) && (values[j] > key)
values[j + 1] = values[j]
j -= 1
end
values[j + 1] = key
i += 1
end
i = 0
while i < 3
puts values[i]
i += 1
end
આઉટપુટ
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
બંને પછીની મુલાકાતો રુમ બનાવવા માટે એક જગ્યા shift કરે છે.
મોડેથી મળેલી ચુકવણી અંતે ઉમેરો
સૉર્ટ કરેલ ખાતાવહી દિવસ 4 અને 6 સાથે સમાપ્ત થાય છે. 7મા દિવસની ચુકવણી આવે છે. કયા ફેરફારો?
શરૂઆતમાં4 6 +7
પરિણામ4 6 7
છેલ્લો વર્તમાન દિવસ પહેલેથી જ 7 પહેલા આવે છે. અન્ય એન્ટ્રીઓને સ્થાનાંતરિત કર્યા વિના તેને ઉમેરો.
કેટલું કામ થાય અને કઈ મર્યાદાઓ છે
ટૂંકી કે લગભગ ગોઠવાયેલી યાદી માટે ઇન્સર્શન સોર્ટ સારું કામ કરે છે. આખી યાદી ઊલટી હોય તો O(n²) ખસેડણીઓ અને સરખામણીઓ કરવી પડે.
સમયની જટિલતા
સૌથી ઓછો સમય લાગે તે કિસ્સો: Θ(n)
સરેરાશ કિસ્સો: Θ(n²)
સૌથી વધુ સમય લાગે તે કિસ્સો: Θ(n²)
Rubyમાં ઇન્સર્શન સૉર્ટ
Ruby સોર્સ કોડ
class InsertionSortExample
def insertionSort(arr)
for i in 1...arr.length do
temp = arr[i]
holePosition = i
while(holePosition > 0 && arr[holePosition - 1] > temp)
arr[holePosition] = arr[holePosition - 1]
holePosition = holePosition - 1
end
arr[holePosition] = temp
end
end
def printArray(arr)
for i in 0...arr.length do
print(arr[i].to_s + " ")
end
puts("")
end
end
obj = InsertionSortExample.new
arr = [15, 3, 12, 6, -9, 9, 0]
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 મૂકો. હવે યાદીનો પહેલો ભાગ [3, 15] છે.
હવે બાકી રહેલાં મૂલ્ય એક પછી એક લો. દરેકને મૂકતાં પહેલાં તેનાથી મોટાં મૂલ્યોને જ ખસેડો. સરખાં મૂલ્યો તેમનો પહેલાનો ક્રમ જાળવે છે.
Rubyમાં ઇન્સર્શન સૉર્ટ પગલે પગલે સમજો
Ruby સોર્સ કોડ
class InsertionSortExample
def insertionSort(arr)
# Un-sorted array from position 1 to length -1
for i in 1...arr.length do
# 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 && arr[holePosition - 1] > temp)
arr[holePosition] = arr[holePosition - 1]
holePosition = holePosition - 1
end
# Once the comparison done, move the value in temp variable to the lastly marked empty position
arr[holePosition] = temp
end
end
def printArray(arr)
# Loop the item and print each item in the console with white space seperated
for i in 0...arr.length do
print(arr[i].to_s + " ")
end
puts("")
end
end
obj = InsertionSortExample.new
arr = [15, 3, 12, 6, -9, 9, 0]
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