33,007
社区成员
发帖
与我相关
我的任务
分享
import java.util.*;
public class MySort {
public static MySort getInstance()
{
if(instance==null)
{
instance = new MySort();
}
return instance;
}
private static MySort instance = null;
/*
*
*/
private int partition(int[] arr, int start, int end)
{
// TODO: argument check
int key = arr[start];
while(start<end)
{
while(arr[end]>key) end--;
while(arr[start]<key)start++;
int temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
}
return start;
}
public void QSort(int[] arr, int start, int end)
{
// TODO: argument check
if(start<end)
{
int partition = partition(arr, start, end);
QSort(arr, start, partition - 1);
QSort(arr, partition + 1, end);
}
}
public void QSortTest1()
{
int len = 10;
int arr[] = new int[len];
Random r = new Random(System.currentTimeMillis());
for(int i=0; i<len;i++)
{
arr[i] = r.nextInt(100);
}
System.out.println("Before sort:");
DisplayIntArray(arr);
QSort(arr, 0, arr.length - 1);
System.out.println("After sort:");
DisplayIntArray(arr);
}
public void QSortDebug1()
{
int arr[] = new int[]{3, 23, 82, 82, 5, 15, 13, 45, 38, 8 };
System.out.println("Before sort:");
DisplayIntArray(arr);
QSort(arr, 0, arr.length - 1);
System.out.println("After sort:");
DisplayIntArray(arr);
}
private void DisplayIntArray(int[] arr)
{
for(int i=0;i<arr.length;i++)
{
System.out.print(arr[i]);
System.out.print(" ");
}
System.out.println();
}
}