ต่อไปนี้เป็นโปรแกรม Java สำหรับ Iterative Quick Sort -
ตัวอย่าง
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("After iteratively performing quick sort, the array is ");
for (i = 0; i < my_arr.length; ++i)
System.out.print(my_arr[i] + " ");
}
} ผลลัพธ์
After iteratively performing quick sort, the array is -11 0 11 32 34 41 76 91 102
คลาสชื่อ Demo มี 3 ฟังก์ชันคือ 'swap_vals' ที่ใช้ในการสลับค่าโดยใช้ตัวแปรชั่วคราว ฟังก์ชัน 'partition' ที่แบ่งอาร์เรย์ออกเป็นสองส่วนตามค่า pivot และฟังก์ชัน 'quick_sort' ที่ใช้ ค่า pivot และตามค่านี้ จัดเรียงค่าในอาร์เรย์
ในฟังก์ชันหลัก อินสแตนซ์ของคลาสสาธิตจะถูกสร้างขึ้นพร้อมกับอาร์เรย์ ฟังก์ชัน 'quick_sort' ถูกเรียกใช้ในอาร์เรย์นี้ และเอาต์พุตจะแสดงบนคอนโซล