标签:java uil swap ++ sys heapsort int sort 堆排序
public void heapSort(int[] arry){ int len= arry.length -1 ; for(int i = (len-1)/2 ;i>=0 ;i--){ buildHeap(arry,i,len); } for(int i = len ;i >0 ;i--){ swap(arry,0 ,i) ; buildHeap(arry,0,i-1); } System.out.println("arry数组"+arry); }
public void buildHeap(int[] arry,int index,int len){ int temp=arry[index]; for(int i = 2*index+1 ;i<=len ;i=i*2+1){ if (i < len && arry[i] < arry[i+1]){ i++ ; } if (temp >= arry[i]){ break; } arry[index] =arry[i]; index = i ; } arry[index] = temp ; }
public void swap(int[] arry ,int left ,int right){ int temp =arry[left] ; arry[left] =arry[right] ; arry[right] = temp ; }
标签:java uil swap ++ sys heapsort int sort 堆排序
原文地址:https://www.cnblogs.com/52hadoop/p/9739302.html