//选择排序
private static int[] selectsort(int a[]) {
for (int i = 0; i < a.length; i++) {
for (int j = i+1; j < a.length; j++) {
if (a[i]>a[j]) {
exch(a, i, j);
}
}
}
return a;
}
//冒泡排序
private static int[] bubblesort(int[] a) {
// TODO Auto-generated method stub
for (int i = 0; i < a.length-1; i++) {
for (int j = 0; j < a.length-1-i; j++) {
if (a[j]>a[j+1]) {
exch(a, j, j+1);
}
}
}
return a;
}
//希尔排序
private static int[] shellsort(int a[]) {
int N=a.length;
int h=1;
while (h<N/3) {h=h*3+1;}
while (h>=1) {
for (int i = h; i < N; i++) {
for (int j = h; j >=h&&a[j]<a[j-h]; j-=h) {
exch(a, j, j-h);
}
h/=3;
}
}
return a;
}
//插入排序
private static int[] insertsort(int[] a) {
// TODO Auto-generated method stub
for (int i = 1; i < a.length; i++) {
for (int j = i; j>0&&a[j]<a[j-1]; j--) {
exch(a, j, j-1);
}
}
return a;
}
//快速排序
public static void quicksort(int[] a,int low,int high){
int start = low;
int end = high;
int key = a[low];
while(end>start){
//从后往前比较
while(end>start&&a[end]>=key) end--;
if(a[end]<=key){
exch(a, start, end);
}
//从前往后比较
while(end>start&&a[start]<=key) start++;
if(a[start]>=key){
exch(a, start, end);
}
//此时第一次循环比较结束,关键值的位置已经确定了。左边的值都比关键值小,右边的值都比关键值大,但是两边的顺序还有可能是不一样的,进行下面的递归调用
}
//递归
if(start-1>low)quicksort(a,low,start-1);//左边序列。第一个索引位置到关键值索引-1
if(end+1<high) quicksort(a,end+1,high);//右边序列。从关键值索引+1到最后一个
}
// 快速排序_哨兵减少交换
private static void quickSort(int[] a, int head, int tail) {
int low = head;
int high = tail;
int pivot = a[low];
if (low < high) {
while (low < high) {
while (low < high && pivot <= a[high]) high--;
a[low] = a[high];
while (low < high && pivot >= a[low]) low++;
a[high] = a[low];
}
a[low] = pivot;
if (low > head + 1) quickSort(a, head, low - 1);
if (high < tail - 1) quickSort(a, high + 1, tail);
}
}
// 非递归快速排序
private static void quickSort(int[] a, int start, int end) {
LinkedList<Integer> stack = new LinkedList<Integer>(); // 用栈模拟
if (start < end) {
stack.push(end);
stack.push(start);
while (!stack.isEmpty()) {
int l = stack.pop();
int r = stack.pop();
int index = partition(a, l, r);
if (l < index - 1) {
stack.push(index - 1);
stack.push(l);
}
if (r > index + 1) {
stack.push(r);
stack.push(index + 1);
}
}
}
}
private static int partition(int[] a, int start, int end) {
int pivot = a[start];
while (start < end) {
while (start < end && a[end] >= pivot)
end--;
a[start] = a[end];
while (start < end && a[start] <= pivot)
start++;
a[end] = a[start];
}
a[start] = pivot;
return start;
}
//归并排序
public static void merge(int[] nums, int low, int mid, int high) {
int[] temp = new int[high - low + 1];
int i = low;// 左指针
int j = mid + 1;// 右指针
int k = 0;
// 把较小的数先移到新数组中
while (i <= mid && j <= high) {
if (nums[i] < nums[j]) {
temp[k++] = nums[i++];
} else {
temp[k++] = nums[j++];
}
}
// 把左边剩余的数移入数组
while (i <= mid) {
temp[k++] = nums[i++];
}
// 把右边边剩余的数移入数组
while (j <= high) {
temp[k++] = nums[j++];
}
// 把新数组中的数覆盖nums数组
for (int k2 = 0; k2 < temp.length; k2++) {
nums[k2 + low] = temp[k2];
}
}
public static int[] sort(int[] nums, int low, int high) {
int mid = (low + high) / 2;
if (low < high) {
// 左边
sort(nums, low, mid);
// 右边
sort(nums, mid + 1, high);
// 左右归并
merge(nums, low, mid, high);
}
return nums;
}
各种常见排序算法
最新推荐文章于 2024-01-06 23:08:55 发布