直接排序
解析:将第n位与第(n-1)位进行比较,若小则前移一位,而后继续将(n-1)与(n-2)比较,如此反复
public class direct {
// private static int a[] = { 3, 2, 9, 5, 6, 1, 7, 8 };
private static int a[] = { 9, 8, 7, 6, 5, 4, 3, 2, 1 };
static int count;
private static void insertSort() {
for (int i = 1; i < a.length; i++) {
for (int j = i - 1; j >= 0; j--) {
if (a[j + 1] < a[j]) {
int temp = a[j + 1];
a[j + 1] = a[j];
a[j] = temp;
count++;
} else {
break;
}
}
}
for (int i = 0; i < a.length; i++) {
System.out.print(a[i]);
System.out.print("-");
}
System.out.println("");
System.out.print("count:" + count);
}
public static void main(String[] args) {
insertSort();
}
}
运行结果
1-2-3-4-5-6-7-8-9-
count:36
希尔排序
解析:基本思想:算法先将要排序的一组数按某个增量d(n/2,n为要排序数的个数)分成若干组,每组中记录的下标相差d.对每组中全部元素进行直接插入排序,然后再用一个较小的增量(d/2)对它进行分组,在每组中再进行直接插入排序。当增量减到1时,进行直接插入排序后,排序完成。
public class Hill {
// private static int a[] = { 3, 2, 9, 5, 6, 1, 7, 8 };
private static int a[] = { 9, 8, 7, 6, 5, 4, 3, 2, 1 };
static int count;
private static void HillSort() {
double d1 = Math.ceil(a.length / 2);
while (true) {
int d = (int) d1;
for (int i = 0; i < d; i++) {
for (int j = d + i; j < a.length; j += d) {
for (int z = j; z > i; z -= d) {
if (a[z - d] > a[z]) {
int temp = a[z - d];
a[z - d] = a[z];
a[z] = temp;
count++;
} else {
break;
}
}
}
}
for (int i = 0; i < a.length; i++) {
System.out.print(a[i]);
System.out.print("-");
}
System.out.println("");
if (d == 1)
break;
d1 = Math.ceil(d1 / 2);
}
for (int i = 0; i < a.length; i++) {
System.out.print(a[i]);
System.out.print("-");
}
System.out.print("-" + count);
}
public static void main(String[] args) {
HillSort();
}
}
运行结果
1-4-3-2-5-8-7-6-9-
1-2-3-4-5-6-7-8-9-
1-2-3-4-5-6-7-8-9-
1-2-3-4-5-6-7-8-9--8
简单排序
解析:在要排序的一组数中,选出最小的一个数与第一个位置的数交换;然后在剩下的数当中再找最小的与第二个位置的数交换,如此循环到倒数第二个数和最后一个数比较为止。
public class selectSort {
private static int a[] = { 9, 8, 7, 6, 5, 4, 3, 2, 1 };
static int count;
private static void selectSort() {
for (int i = 0; i < a.length; i++) {
int minPos = i;
for (int j = i + 1; j < a.length; j++) {
// 取得最小数下标
if (a[j] < a[minPos]) {
minPos = j;
}
}
// 需要交换
if (minPos != i) {
int temp = a[minPos];
a[minPos] = a[i];
a[i] = temp;
count++;
}
}
System.out.print("result:");
for (int i = 0; i < a.length; i++) {
System.out.print(a[i]);
System.out.print("-");
}
System.out.println("");
System.out.print("count:" + count);
}
public static void main(String[] args) {
selectSort();
}
}
运行结果
result:1-2-3-4-5-6-7-8-9-
count:4
希尔排序
解析:选择一个基准元素,通常选择第一个元素或者最后一个元素,通过一趟扫描,将待排序列分成两部分,一部分比基准元素小,一部分大于等于基准元素,此时基准元素在其排好序后的正确位置,然后再用同样的方法递归地排序划分的两部分。
public class MyClass {
static int a[] ={49, 38, 65, 97, 76, 13, 27, 49, 78, 34, 12, 64, 5, 4, 62, 99, 98, 54, 56, 17, 18, 23, 34, 15, 35, 25, 53, 51};
public static int aaa(int[] list, int low, int high) {
int temp = list[low];
while (low < high) {
while (low < high && temp <= list[high]) {
high--;
}
list[low] = list[high];
while (low < high && temp >= list[low]) {
low++;
}
list[high] = list[low];
}
list[low] = temp;
return low;
}
public static void quickSort(int[] list, int low, int high) {
if (low < high) {
int mid = aaa(list, low, high);
quickSort(list, low, mid - 1);
quickSort(list, mid + 1, high);
}
}
public static void main(String[] args) {
quickSort(a, 0, a.length - 1);
for (int i = 0; i < a.length; i++) {
System.out.print(a[i]);
System.out.print("-");
}
}
}
运行结果
4-5-12-13-15-17-18-23-25-27-34-34-35-38-49-49-51-53-54-56-62-64-65-76-78-97-98-99-
归并排序
解析:归并(Merge)排序法是将两个(或两个以上)有序表合并成一个新的有序表,即把待排序序列分为若干个子序列,每个子序列是有序的。然后再把有序子序列合并为整体有序序列。
public class Merge {
static int a[] = {49, 38, 65, 97, 76, 13, 27, 49, 78, 34, 12, 64, 5, 4, 62, 99, 98, 54, 56, 17, 18, 23, 34, 15, 35, 25, 53, 51};
public static void merge(int[] list, int left, int right, int mid) {
//创建辅助数组,将排好的序列放在里面
int copy[] = new int[list.length];
int CLeft = left;
int Orign = left;
int CRight = right;
int CMid = mid + 1;
//最小将分为一个为一组
//每一组的第一个一定为每组最小
//将两组最小的全部移到左边一组
//左边组的组建
while (CLeft <= mid && CMid <= right) {
if (list[CLeft] <= list[CMid]) {
copy[Orign++] = list[CLeft++];
} else {
copy[Orign++] = list[CMid++];
}
}
//右边边组的组建
while (CLeft <= mid) {
copy[Orign++] = list[CLeft++];
}
while (CMid <= right) {
copy[Orign++] = list[CMid++];
}
//copy到原集合
for (int i = left; i <= right; i++) {
list[i] = copy[i];
}
}
public static void quickSort(int[] list, int left, int right) {
if (left < right) {
int mid = (left + right) / 2;
quickSort(list, left, mid);
quickSort(list, mid + 1, right);
merge(list, left, right, mid);
}
}
public static void main(String[] args) {
quickSort(a, 0, a.length - 1);
for (int i = 0; i < a.length; i++) {
System.out.print(a[i]);
System.out.print("-");
}
}
}
运行结果
4-5-12-13-15-17-18-23-25-27-34-34-35-38-49-49-51-53-54-56-62-64-65-76-78-97-98-99-