public class TestSort {
public static void selectSort(int[] arr) {
for(int i = 0;i<arr.length;i++) {
int temp = arr[i];
int flag = i;
for(int j = i+1;j<arr.length;j++) {
if(arr[j] < temp) {
temp = arr[j];
flag = j;
}
}
if(flag != i) {
arr[flag] = arr[i];
arr[i] = temp;
}
}
}
public static void insertSort(int[] arr) {
if( arr!= null) {
for(int i = 1;i<arr.length;i++) {
int temp = arr[i];
int j = i;
if(arr[j-1]>temp) {
while(j>=1&&arr[j-1]>temp) { //选择的那个数比前面的数要小,
//则就将其跟前面的数一个一个比较,知道碰到比这个数还要小的,将其放在这
arr[j] = arr[j-1];
j--;
}
}
arr[j] = temp;
}
}
}
public static void bubbleSort(int[] arr) {
for(int i = 0;i<arr.length - 1;i++) {
for(int j = arr.length - 1;j>i;j--) {
if(arr[j] < arr[j-1]) {
int temp = arr[j];
arr[j] = arr[j-1];
arr[j-1] = temp;
}
}
}
}
public static void merge(int[] a,int s,int m,int t) {
int i,j,k;
int n1 = m - s + 1;
int n2 = t - m;
int[] left = new int[n1];
int[] right = new int[n2];
for(i = 0,k = s;i<n1;i++,k++) {
left[i] = a[k];
}
for(i = 0,k = m + 1;i<n2;i++,k++) {
right[i] = a[k];
}
for(i = 0,j = 0,k = s;i < n1 && j < n2;) {
a[k++] = (left[i]<right[j])?left[i++]:right[j++];
}
while(i<n1) {
a[k++] = left[i++];
}
while(j<n2) {
a[k++] = right[j++];
}
}
public static void mergeSort(int[] a,int s,int t) {
if(s<t) {
int q = (s + t)/2;
mergeSort(a, s, q);
mergeSort(a, q+1, t);
merge(a, s, q, t);
}
}
//快速排序
public static void quickSort(int[] a,int low,int high) {
if(low >= high) {
return;
}
int i = low,j = high;
int pivot = a[i]; //每次取第一个元素作为分割值
while(i < j) {
while(i < j&&a[j] > pivot) {
j--;
}
if(i<j) {
a[i++] = a[j];
}
while(i<j&&a[i]<pivot) {
i++;
}
if(i<j) {
a[j--] = a[i];
}
}
a[i] = pivot;
quickSort(a, low, i - 1);
quickSort(a, i + 1, high);
}
//希尔排序
public static void shellSort(int[] a) {
int len = a.length;
int h; //h为跨度
int i,j;
int temp;
for(h = len/2;h>0;h /= 2) {
for(i = h;i<len;i++) {
temp = a[i];
j = i - h; //同一组中上一个元素的下标
while(j>0 && temp < a[j]) { //当前数值小于同一组中的上一个元素
a[j + h] = a[j];
j = j - h;
}
a[j + h] = temp; //j+h,因为上面减的小于0了 = =
}
}
}
//堆排序
public static void adjustMinHeap(int[] a,int pos,int len) { //将pos位置处的结点调整为小顶堆
int child;
int temp;
for(temp = a[pos];2*pos+1<=len;pos = child) { //迭代该节点下的所有子节点
child = 2*pos+1;
if(child<len&&a[child]>a[child+1]) {
child++;
}
if(a[child]<temp) { //如果某一个结点的子节点小与该节点的值,则替换之
a[pos] = a[child];
}else break; //此时已经到头了
}
a[pos] = temp; //此时的pos为child的值,这样做就把该节点和小与该节点的子节点交换的值
}
public static void heapSort(int[] a) {
int len = a.length;
for(int i = len/2 - 1;i >= 0;i--) { //将整个二叉树调整成为小顶堆
adjustMinHeap(a, i, len - 1);
}
for(int i = len - 1;i>=0;i--) { //交换堆顶位置和最后一个位置处的值,然后再调整成为小顶堆
int temp = a[0];
a[0] = a[i];
a[i] = temp;
adjustMinHeap(a, 0, i - 1);
}
}
public static void main(String[] args) {
/*int[] a = {21,32,123,12,4543,12,354235,2,4,32,525432,425253,4,23,5,35};
selectSort(a);
for (int i : a) {
System.out.println(i);
}*/
/*MyInt firstNum = new MyInt(5);
MyInt secondNum = new MyInt(10);
swap(firstNum, secondNum);
System.out.println(firstNum.data);
System.out.println(secondNum.data);*/
int[] a = {21,32,123,12,4543,12,354235,2,4,32,525432,425253,4,23,5,35};
// bubbleSort(a);
//mergeSort(a, 0, a.length - 1);
//quickSort(a, 0, a.length - 1);
// shellSort(a);
heapSort(a);
System.out.println(Arrays.toString(a));
}
数据结构与算法排序算法java描述
最新推荐文章于 2023-12-25 19:06:15 发布