private static void quickSort(int[] data, int i, int j) {
int pivotIndex = (i + j) / 2;
// swap
SortTest.swap(data, pivotIndex, j);
int k = partition(data, i - 1, j, data[j]);
SortTest.swap(data, k, j);
if ((k - i) > 1)
quickSort(data, i, k - 1);
if ((j - k) > 1)
quickSort(data, k + 1, j);
}
作者: 酱爆 时间: 2013-9-21 17:05
快排(用递归实现)
public class MySort
{
public static void main(String[] args)
{
int[] arr = {1,6,7,5,3,5,8,9,7,56};
QuickSort.Sort(arr, 0, arr.length-1);
for (int i = 0; i < arr.length; ++i)
{
System.out.println(arr[i]);
}
}
}
class QuickSort
{
public static void Sort(int arr[], int low, int high)
{
int pos = 0;
if (low < high)
{
pos = FindPos(arr, low, high);
Sort(arr, low, pos-1);
Sort(arr, pos+1, high);
}
}
public static int FindPos(int arr[], int low, int high)
{
int val = arr[low];
while (low < high)
{
while (low < high && arr[high] >= val)
--high;
arr[low] = arr[high];
while (low < high && arr[low] <= val)
++low;
arr[high] = arr[low];
}
arr[low] = val;
return low;
}
}
复制代码
作者: 咕咕猫 时间: 2013-9-21 17:19
class PaiXu
{
public static void main(String[]args)
{
int []arr={3,5,8,1,4,2,9,2}
seletsort(arr);
bubblesort(arr);
}
/**
给int数组选择排序
*/
public static void selectsort(int[]arr)
{
for (int x=0;x<arr.length-1; x++)
{
for (int y=x+1;y<arr.length ;y++ )
{
if (arr[x]>arr[y])
{
swap(arr,x,y);
}
}
}
}
/**
给int数组进行冒泡排序
*/
public static void bubblesort(int[]arr)
{
for (intx=0;x<arr.length-1 ;x++ )
{
for (int y=0;y<arr.length-x-1 ;y++ )
{
if (arr[y]>arr[y+1])
{
swap(arr,y,y+1);
}
}
}
}
/**
给数组中的元素进行位置置换
*/
public static void swap(int[]arr,int a,int b)//换位操作
{
int temp =arr[a];
arr[a]=arr[b];
arr[b]=temp;
}
} 作者: 光脚丫的小白兔 时间: 2013-9-21 21:15
public class QuickSort {
private void sort(int[] data, int left, int right) {
int i = left;
int j = right;
int middle = data[(left + right) / 2];
do{
while(data[i] < middle && i < right) i ++;
while(data[j] > middle && j > left) j --;
if(i <= j) {
int itemp = data[i];
data[i] = data[j];
data[j] = itemp;
i ++;
j --;
}
} while(i <= j);