इटरेटिव क्विक सॉर्ट के लिए जावा प्रोग्राम निम्नलिखित है:
उदाहरण
public class Demo{
void swap_vals(int arr[], int i, int j){
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
int partition(int arr[], int l, int h){
int x = arr[h];
int i = (l - 1);
for (int j = l; j <= h - 1; j++){
if (arr[j] <= x){
i++;
swap_vals(arr, i, j);
}
}
swap_vals(arr, i + 1, h);
return (i + 1);
}
void quick_sort(int arr[], int l, int h){
int my_list[] = new int[h - l + 1];
int top = -1;
my_list[++top] = l;
my_list[++top] = h;
while (top >= 0){
h = my_list[top--];
l = my_list[top--];
int p = partition(arr, l, h);
if (p - 1 > l){
my_list[++top] = l;
my_list[++top] = p - 1;
}
if (p + 1 < h){
my_list[++top] = p + 1;
my_list[++top] = h;
}
}
}
public static void main(String args[]){
Demo my_ob = new Demo();
int my_arr[] = { 34, 76, 41, 32, 11, 0 , 91, 102, -11};
my_ob.quick_sort(my_arr, 0, my_arr.length - 1);
int i;
System.out.println("इटरेटिवली क्विक सॉर्ट करने के बाद, ऐरे है ");
for (i = 0; i < my_arr.length; ++i)
System.out.print(my_arr[i] + " ");
}
}
आउटपुट
इटरेटिवली क्विक सॉर्ट करने के बाद, ऐरे है -11 0 11 32 34 41 76 91 102
Demo नामक एक क्लास में 3 फंक्शन्स होते हैं, swap_vals जिसका उपयोग एक टेम्पररी वेरिएबल का उपयोग करके मानों को स्वैप करने के लिए किया जाता है, एक partition फंक्शन जो एक पिवट मान के आधार पर ऐरे को दो हिस्सों में विभाजित करता है, और quick_sort फंक्शन जो एक पिवट मान का उपयोग करता है और इस मान के आधार पर, ऐरे में मानों को सॉर्ट करता है।
मुख्य फंक्शन में, Demo क्लास का एक इंस्टेंस एक ऐरे के साथ बनाया जाता है। इस ऐरे पर quick_sort फंक्शन को कॉल किया जाता है और आउटपुट कंसोल पर प्रदर्शित किया जाता है।