క్విక్సార్ట్ ఒక విలువను ఆధారంగా ఎంచుకుంటుంది. అదే పివట్. దాని ప్రకారం జాబితాను విడదీసి రెండు భాగాల్లోనూ ఇదే పని మళ్లీ చేస్తుంది. ఇక్కడ మొదటి విలువనే ఎప్పుడూ పివట్గా తీసుకుంటాం. భాగాలు దాదాపు సమంగా ఉంటే సమయం Θ(n log n). ప్రతిసారీ అతి చిన్న లేదా అతి పెద్ద విలువే పివట్ అయితే సమయం Θ(n²) అవుతుంది. ఈ నియమంతో ముందే క్రమంలో ఉన్న జాబితాకూ అదే జరుగుతుంది.
ఒక చిన్న ఉదాహరణ
పోలిక కోసం 2ను ఎంచుకోండి. దాని కంటే చిన్నదైన 1ను ఎడమవైపు, పెద్దదైన 3ను కుడివైపు పెట్టండి. ఇప్పుడు రెండు వైపులా ఒక్కో సంఖ్య ఉంది.
మొదట3 1 2
ఫలితం1 2 3
lesson.rb
values = [3, 1, 2]
pivot = values[2]
boundary = 0
i = 0
while i < 2
if values[i] < pivot
saved = values[i]
values[i] = values[boundary]
values[boundary] = saved
boundary += 1
end
i += 1
end
values[2] = values[boundary]
values[boundary] = pivot
i = 0
while i < 3
puts values[i]
i += 1
end
అవుట్పుట్
1
2
3
రోజువారీ ఉదాహరణలు
ఒక పార్సిల్ బరువుతో పోల్చి మిగతావాటిని విడగొట్టడం
మూడు పార్సిళ్ల బరువులు వరుసగా 8, 2, 5 కిలోలు. 5 కిలోలను ఆధారంగా తీసుకోండి. ఏ పార్సిల్ తక్కువ బరువు వైపు, ఏది ఎక్కువ బరువు వైపు వెళ్తుంది?
మొదట8 2 5
ఫలితం2 • 5 • 8
రెండు తేలికైనవి మరియు ఎనిమిది బరువుగా ఉంటాయి. ఇరుసు ఇప్పుడు రెండు సమూహాలను వేరు చేస్తుంది.
ధర ఒకటే ఉన్న పుస్తకాలను క్రమంలో పెట్టడం
ప్రతి పుస్తకం ఖర్చులు 4. ఒక మూడు మార్గాల విభజన ఏమి ఉత్పత్తి చేస్తుంది?
మొదట4 4 4
ఫలితం∅ • 4 4 4 • ∅
సమాన సమూహంలో ప్రతి పుస్తకం ఉంటుంది. బయటి సమూహానికి ఎక్కువ పని అవసరం లేదు.
సరిగా విడగొట్టని పివట్ను గుర్తించడం
తేదీలు 1,2,3 మరియు 4 క్రమంలో ఉన్నాయి. ఎల్లప్పుడూ చివరి తేదీని ఎంచుకోండి. మొదటి విభజనలో ఏమి జరుగుతుంది?
మొదట1 2 3 4
ఫలితం1 2 3 • 4 • ∅
ఒక సమూహం ఇప్పటికీ మూడు వస్తువులను కలిగి ఉంది. ఈ నమూనాను పునరావృతం చేయడం వల్ల పోలికలు వృధా అవుతాయి.
పట్టే సమయం మరియు పరిమితులు
ప్రతి సారి దాదాపు సమాన భాగాలుగా విడిపోతే సాధారణంగా O(n log n) పని ఉంటుంది. ప్రతిసారి అతి చిన్న లేదా అతి పెద్ద విలువనే పివట్గా ఎంచుకుంటే O(n²) వరకు పెరుగుతుంది.
సమయ సంక్లిష్టత
అతి తక్కువ సమయం పట్టే సందర్భం: Θ(n log n)
సగటు సందర్భం: Θ(n log n)
అతి ఎక్కువ సమయం పట్టే సందర్భం: Θ(n²)
Rubyలో క్విక్ సార్ట్
Ruby సోర్స్ కోడ్
class QuickSortExample
def quickSort(arr, low, high)
if(low < high)
pivot = partition(arr, low, high)
quickSort(arr, low, pivot - 1)
quickSort(arr, pivot + 1, high)
end
end
def partition(arr, low, high)
pivot = arr[low]
i = low
j = high
while(i < j)
while arr[i].to_i <= pivot.to_i and i < high
i += 1
end
while arr[j].to_i > pivot.to_i and j > low
j -= 1
end
if(i < j)
temp = arr[i]
arr[i] = arr[j]
arr[j] = temp
end
end
arr[low] = arr[j]
arr[j] = pivot
return j
end
def printArray(arr)
for i in 0...arr.length do
print(arr[i].to_s + " ")
end
puts("")
end
end
obj = QuickSortExample.new
arr = [15, 3, 12, 6, -9, 9, 0]
print("Before Sorting: ")
obj.printArray(arr)
obj.quickSort(arr, 0, arr.length - 1)
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వ దశగా ఉపయోగించండి. రెండు మూలల నుండి అంతర్గత స్కాన్.
ఎడమ సూచిక పై హద్దు కంటే తక్కువగా ఉండి, అక్కడి విలువ పివట్ కంటే తక్కువగా లేదా సమానంగా ఉన్నంతసేపు దాన్ని కుడికి జరపండి. కుడి సూచిక దగ్గర విలువ పివట్ కంటే ఎక్కువగా ఉన్నంతసేపు ఆ సూచికను ఎడమకు జరపండి.
రెండు సూచికలు ఇంకా కలవకపోతే, తప్పు వైపున ఉన్న ఆ రెండు విలువల స్థానాలు మార్చండి. తర్వాత వెతకడం కొనసాగించండి.
సూచికలు కలిసినప్పుడు లేదా ఒకదానినొకటి దాటినప్పుడు, మొదటి విలువను కుడి సూచిక దగ్గర ఉన్న విలువతో మార్చండి. ఇప్పుడు పివట్ తన చివరి స్థానంలో ఉంటుంది.
ఆ స్థానానికి ముందు, తర్వాత ఉన్న భాగాలపై ఇదే వర్గీకరణ పద్ధతిని మళ్లీ వాడండి. రెండు కన్నా తక్కువ అంశాలున్న భాగం అప్పటికే క్రమంలో ఉంటుంది.
Rubyలో క్విక్ సార్ట్ దశలవారీ వివరణ
Ruby సోర్స్ కోడ్
class QuickSortExample
def quickSort(arr, low, high)
if(low < high)
# Find the pivot index of the lower bound of an array
pivot = partition(arr, low, high)
# Apply Divide and conquer strategy
# to sort the left side and right side of an array
# respective to the pivot position
# Left hand side array
quickSort(arr, low, pivot - 1)
# Right hand side array
quickSort(arr, pivot + 1, high)
end
end
def partition(arr, low, high)
# Pick a lowest bound element as an pivot value
pivot = arr[low]
i = low
j = high
while(i < j)
# Increment the index value of "i"
# till the left most values should be less than or equal to the pivot value
while arr[i].to_i <= pivot.to_i and i < high
i += 1
end
# Decrement the index value of "j"
# till the right most values should be greater than to the pivot value
while arr[j].to_i > pivot.to_i and j > low
j -= 1
end
# Interchange the values of present
# in the index i and j of an array only if i is less than j
if(i < j)
temp = arr[i]
arr[i] = arr[j]
arr[j] = temp
end
end
# Interchange the element in j's poisition to the lower bound of an array
# and place the Pivot element to the j's position
arr[low] = arr[j]
arr[j] = pivot
# Finally return the appropriate index position of the pivot element
return j
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 = QuickSortExample.new
arr = [15, 3, 12, 6, -9, 9, 0]
print("Before Sorting: ")
obj.printArray(arr)
obj.quickSort(arr, 0, arr.length - 1)
print("After Sorting: ")
obj.printArray(arr)
అవుట్పుట్
Before Sorting: 15 3 12 6 -9 9 0
After Sorting: -9 0 3 6 9 12 15