JAVA实现一个快速排序?
排序的方法有:插入排序(直接插入排序、希尔排序),交换排序(冒泡排序、快速排序),选择排序(直接选择排序、堆排序),归并排序,分配排序(箱排序、基数排序)
//冒泡排序
int[] a = new int[] { 1, 3, 9, 2, 0, 6, 8, 7, 5 };
int b = 0;
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a.length - i - 1; j++) {
if (a[j] > a[j + 1]) {
b = a[j];
a[j] = a[j + 1];
a[j + 1] = b;
}
}
}
//插入排序
for(int i=1;i<arr.length;i++){
int temp=arr[i];
int j=i;
for(;j>0 && arr[j-1]>temp;j--){
arr[j]=arr[j-1];
}
arr[j]=temp;
}
///
/**
* 测试类
* @param args
*/
public static void a()
{
int[] aa = new int[] {2, 8, 9, 2, 1, 4, 6, 5};
Arrays.sort(aa);
get(aa, 9);
}
/**
* 使用二分查找法查找数组中的某个数
* @param index 数组集合
* @param num 要查找的数
*/
public static void get(int[] index, int num)
{
int leng = index.length;
if (leng > 0)
{
if (leng == 1)
{
if (index[0] == num)
{
System.out.println("result: " + num); //$NON-NLS-1$
}
return;
}
int l = leng / 2;
if (index[l] > num)
{
int[] a = new int[l];
System.arraycopy(index, 0, a, 0, a.length);
get(a, num);
}
else if (index[l] < num)
{
int[] a = new int[leng - l - 1];
System.arraycopy(index, l + 1, a, 0, a.length);
get(a, num);
}
else if (index[l] == num)
{
System.out.println("result: " + num); //$NON-NLS-1$
}
}
}
//二分查找法
int[] index = new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9};
Arrays.sort(index);
int begin = 0;
int end = index.length;
while (end - begin >= 0)
{
int center = (begin + end) / 2;
if (index[center] == a)
{
System.out.println("OK" + index[center]);
break;
}
else if (index[center] > a)
{
end = center - 1;
}
else if (index[center] < a)
{
begin = center + 1;
}
}
排序的方法有:插入排序(直接插入排序、希尔排序),交换排序(冒泡排序、快速排序),选择排序(直接选择排序、堆排序),归并排序,分配排序(箱排序、基数排序)
//冒泡排序
int[] a = new int[] { 1, 3, 9, 2, 0, 6, 8, 7, 5 };
int b = 0;
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a.length - i - 1; j++) {
if (a[j] > a[j + 1]) {
b = a[j];
a[j] = a[j + 1];
a[j + 1] = b;
}
}
}
//插入排序
for(int i=1;i<arr.length;i++){
int temp=arr[i];
int j=i;
for(;j>0 && arr[j-1]>temp;j--){
arr[j]=arr[j-1];
}
arr[j]=temp;
}
///
/**
* 测试类
* @param args
*/
public static void a()
{
int[] aa = new int[] {2, 8, 9, 2, 1, 4, 6, 5};
Arrays.sort(aa);
get(aa, 9);
}
/**
* 使用二分查找法查找数组中的某个数
* @param index 数组集合
* @param num 要查找的数
*/
public static void get(int[] index, int num)
{
int leng = index.length;
if (leng > 0)
{
if (leng == 1)
{
if (index[0] == num)
{
System.out.println("result: " + num); //$NON-NLS-1$
}
return;
}
int l = leng / 2;
if (index[l] > num)
{
int[] a = new int[l];
System.arraycopy(index, 0, a, 0, a.length);
get(a, num);
}
else if (index[l] < num)
{
int[] a = new int[leng - l - 1];
System.arraycopy(index, l + 1, a, 0, a.length);
get(a, num);
}
else if (index[l] == num)
{
System.out.println("result: " + num); //$NON-NLS-1$
}
}
}
//二分查找法
int[] index = new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9};
Arrays.sort(index);
int begin = 0;
int end = index.length;
while (end - begin >= 0)
{
int center = (begin + end) / 2;
if (index[center] == a)
{
System.out.println("OK" + index[center]);
break;
}
else if (index[center] > a)
{
end = center - 1;
}
else if (index[center] < a)
{
begin = center + 1;
}
}