Javaでクイックソートのアルゴリズムを使って配列の要素を並び替える

Javaコード:
public class LjsQuickSort {

public int getMiddle(Integer[] list, int low, int high) {
int tmp = list[low];
while (low < high) {
while (low < high && list[high] > tmp) {
high–;
}
list[low] = list[high];
while (low < high && list[low] < tmp) {
low++;
}
list[high] = list[low];
}
list[low] = tmp;
return low;
}
public void kanquickSort(Integer[] list, int low, int high) {
if (low < high) {
int middle = getMiddle(list, low, high);
kanquickSort(list, low, middle – 1);
kanquickSort(list, middle + 1, high);
}
}

public void quick(Integer[] str) {
if (str.length > 0) {
kanquickSort(str, 0, str.length – 1);
}
}

public static void main(String[] args) {

Integer[] list={34,16,27,12,4,8,21,30};
LjsQuickSort qs=new LjsQuickSort();
qs.quick(list);
System.out.println(“quick sort result as follows\n");
for(int i=0;i<list.length;i++){
System.out.print(list[i]+" “);
}
System.out.println();
}

}

Java

Posted by arkgame