private static void heapsort(int[] arr) {//建堆 crearheap(arr); for (int i = 0; i < arr.length-1; i++) { int heapsize=arr.l文化ength-i; swap(arr,heapsize-1,0); heapsize--; shiftdown(arr,heapsize,0); } system.out.println(arrays.tostring(arr)); } private static void crearheap(int[] arr) { // 从后往前遍历(右边非叶子节点开始), 依次进行向下调整 for (int i = (arr.length-1-1)/2; i >=0 ; i--) { shiftdown(arr,arr.length,i); } }//向下调整,形成大堆 private static void shiftdown(int[] arr, int size, int i) { int parent = i; int child = parent*2+1; while (child<size){ if (child +1< size && arr[child +1]> arr[child]){ child=child+1; } if (arr[child]>arr[parent]){ swap(arr,child,parent); }el { break; } parent=child; child=parent*2+1; } }//交换 private static void swap(int[] arr, int child, int parent) { int tmp =arr[child]; arr[child] =arr[parent]; arr[parent]=tmp; }
private static void quick(int[] arr) { quicksorthelper(arr,0,arr.length-1); system.out.println(arrays.tostring(arr)); } private static void quicksorthelper(int[] arr, int left, int right) { if (left>=right){ //区间只有一个元素,或者零个元素 return; } int index = partition(arr,left,right); quicksorthelper(arr,left,index-1); 老鼠贴 quicksorthelper(arr,index+1,right); } private static int partition(int[] arr, int left, int right) { int i=left; int j=right; int bavalue=arr[right]; while (i<j){ while (i<j && arr[i]<=bavalue){ i++; } while (i<j && arr[j]>=bavalue){ j--; } if (i<j){ swap(arr,i,j); } } swap(arr,i,right); return i; } private static void swap(int[] arr, int i, int j) { int tmp =arr[i]; ar班组安全管理r[i]=arr[j]; arr[j]=tmp; }
public static void quicksortbyloop(int[] arr) { stack<integer> stack =new stack<>(); stack.push(0); stack.push(arr.length-1); while (!stack.impty()){ int right = stack.pop(); int left = stack.pop(); if (left>=right){ continue; } int index = partition(arr,left,right); //右子树 stack.push(index+1); stack.push(right); //左子树 stack.push(left); stack.push(index-1); } system.out.println(arrays.tostring(arr)); } private static int partition(int[] arr, int left, int right) { int bavalue =arr[right]; int i =left; int j =right; while (i<j){ while (i<j && arr[i]<=bavalue){ i++; } while (i<j && arr[j]>=bavalue){ j--; } if (i<j){ swap(arr,i,j); } } swap(arr,i,right); return i; } private static void swap(int[] arr, int i, int j) { int tmp = arr[i]; arr[i] = arr[j]; arr[j] = tmp; }
public static void mergesort(int[] arr){ merges相逢何必曾相识歌词orthelper(arr,0,arr.length); system.out.println(arrays.tostring(arr)); } private static void mergesorthelper(int[] arr, int left, int right) { if (right-left<=1){ return; } int mid = (right+left)/2; mergesorthelper(arr,left,mid); mergesorthelper(arr,mid,right); merge(arr,left,mid,right); } private static void merge(int[] arr, int left, int mid, int right) { int cur1 =left; int cur2 =mid; //两个数组合并后的结果 int[] output=new int[right-left]; int outputindex=0; while (cur1<mid && cur2<right){ if (arr[cur1]<=arr[cur2]) { output[outputindex++] = arr[cur1++]; }el { output[outputindex++] = arr[cur2++]; } } while (cur1<mid){ output[outputindex++] = arr[cur1++]; } while (cur2<right){ output[outputindex++] = arr[cur2++]; } for (int i = 0; i < right-left 山东高考一分一段表; i++) { arr[left+i] = output[i]; } }
public static void mergesortbyloop(int[] arr){ // gap 当前每个组中的元素个数. for (int gap =1;gap<arr.length;gap*=2){ for (int i = 0; i <arr.length ; i+=2*gap) { //相当于把两个长度为 gap 的相邻组进行了合并 int left =i; int mid =i+gap; int right=i+2*gap; if (mid > arr.length){ mid =arr.length; } if (right>arr.length){ right=arr.length; } merge(arr,left,mid,right); } } system.out.println(arrays.tostring(arr)); }
到此这篇关于java综合整理堆排序 快速排序 归并排序的文章就介绍到这了,更多相关java 排序算法内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!
本文发布于:2023-04-04 16:25:49,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/a4ec75db909dae488a7fc663c1da5818.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:Java综合整理堆排序 快速排序 归并排序.doc
本文 PDF 下载地址:Java综合整理堆排序 快速排序 归并排序.pdf
留言与评论(共有 0 条评论) |