క్విక్సార్ట్ ఒక విలువను ఆధారంగా ఎంచుకుంటుంది. అదే పివట్. దాని ప్రకారం జాబితాను విడదీసి రెండు భాగాల్లోనూ ఇదే పని మళ్లీ చేస్తుంది. ఇక్కడ మొదటి విలువనే ఎప్పుడూ పివట్గా తీసుకుంటాం. భాగాలు దాదాపు సమంగా ఉంటే సమయం Θ(n log n). ప్రతిసారీ అతి చిన్న లేదా అతి పెద్ద విలువే పివట్ అయితే సమయం Θ(n²) అవుతుంది. ఈ నియమంతో ముందే క్రమంలో ఉన్న జాబితాకూ అదే జరుగుతుంది.
ఒక చిన్న ఉదాహరణ
పోలిక కోసం 2ను ఎంచుకోండి. దాని కంటే చిన్నదైన 1ను ఎడమవైపు, పెద్దదైన 3ను కుడివైపు పెట్టండి. ఇప్పుడు రెండు వైపులా ఒక్కో సంఖ్య ఉంది.
మొదట3 1 2
ఫలితం1 2 3
Main.java
public class Main {
public static void main(String[] args) {
int[] values = {3, 1, 2};
int pivot = values[2];
int boundary = 0;
int i = 0;
while (i < 2) {
if (values[i] < pivot) {
int saved = values[i];
values[i] = values[boundary];
values[boundary] = saved;
boundary += 1;
}
i += 1;
}
values[2] = values[boundary];
values[boundary] = pivot;
i = 0;
while (i < 3) {
System.out.println(values[i]);
i += 1;
}
}
}
అవుట్పుట్
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²)
Javaలో క్విక్ సార్ట్
Java సోర్స్ కోడ్
class QuickSortExample {
public void quickSort(int[] arr, int low, int high) {
if(low < high) {
int pivot = partition(arr, low, high);
quickSort(arr, low, pivot - 1);
quickSort(arr, pivot + 1, high);
}
}
private int partition(int[] arr, int low, int high) {
int pivot = arr[low];
int i = low;
int j = high;
while(i < j) {
while(arr[i] <= pivot && i < high) {
i++;
}
while(arr[j] > pivot && j > low) {
j--;
}
if(i < j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
arr[low] = arr[j];
arr[j] = pivot;
return j;
}
public void printArray(int[] arr) {
for(int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
System.out.println("");
}
public static void main(String[] args) {
QuickSortExample obj = new QuickSortExample();
int[] arr = { 15, 3, 12, 6, -9, 9, 0 };
System.out.print("Before Sorting: ");
obj.printArray(arr);
obj.quickSort(arr, 0, arr.length - 1);
System.out.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వ దశగా ఉపయోగించండి. రెండు మూలల నుండి అంతర్గత స్కాన్.
ఎడమ సూచిక పై హద్దు కంటే తక్కువగా ఉండి, అక్కడి విలువ పివట్ కంటే తక్కువగా లేదా సమానంగా ఉన్నంతసేపు దాన్ని కుడికి జరపండి. కుడి సూచిక దగ్గర విలువ పివట్ కంటే ఎక్కువగా ఉన్నంతసేపు ఆ సూచికను ఎడమకు జరపండి.
రెండు సూచికలు ఇంకా కలవకపోతే, తప్పు వైపున ఉన్న ఆ రెండు విలువల స్థానాలు మార్చండి. తర్వాత వెతకడం కొనసాగించండి.
సూచికలు కలిసినప్పుడు లేదా ఒకదానినొకటి దాటినప్పుడు, మొదటి విలువను కుడి సూచిక దగ్గర ఉన్న విలువతో మార్చండి. ఇప్పుడు పివట్ తన చివరి స్థానంలో ఉంటుంది.
ఆ స్థానానికి ముందు, తర్వాత ఉన్న భాగాలపై ఇదే వర్గీకరణ పద్ధతిని మళ్లీ వాడండి. రెండు కన్నా తక్కువ అంశాలున్న భాగం అప్పటికే క్రమంలో ఉంటుంది.
Javaలో క్విక్ సార్ట్ దశలవారీ వివరణ
Java సోర్స్ కోడ్
class QuickSortExample {
public void quickSort(int[] arr, int low, int high) {
if(low < high) {
// Find the pivot index of the lower bound of an array
int 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);
}
}
private int partition(int[] arr, int low, int high) {
// Pick a lowest bound element as an pivot value
int pivot = arr[low];
int i = low;
int 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] <= pivot && i < high) {
i++;
}
// Decrement the index value of "j"
// till the right most values should be greater than to the pivot value
while(arr[j] > pivot && j > low) {
j--;
}
// Interchange the values of present
// in the index i and j of an array only if i is less than j
if(i < j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
// 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;
}
public void printArray(int[] arr) {
// Loop the item and print each item in the console with white space seperated
for(int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
System.out.println("");
}
public static void main(String[] args) {
QuickSortExample obj = new QuickSortExample();
int[] arr = { 15, 3, 12, 6, -9, 9, 0 };
System.out.print("Before Sorting: ");
obj.printArray(arr);
obj.quickSort(arr, 0, arr.length - 1);
System.out.print("After Sorting: ");
obj.printArray(arr);
}
}
అవుట్పుట్
Before Sorting: 15 3 12 6 -9 9 0
After Sorting: -9 0 3 6 9 12 15