|
/// <summary> /// 小根堆排序 /// </summary> /// <param name="dblarray"></param> /// <param name="startindex"></param> /// <returns></returns> private static void heapsort(ref double[] dblarray ) { for(int i = dblarray.length -1 ; i >= 0; i--) { if(2*i+1<dblarray.length) { int minchildrenindex = 2*i+1 ; //比较左子树和右子树,记录最小值的index if(2*i+2 < dblarray.length ) { if(dblarray[2*i+1]>dblarray[2*i+2]) minchildrenindex = 2*i+2; } if(dblarray[i] > dblarray[minchildrenindex]) { exchagevalue(ref dblarray[i],ref dblarray[minchildrenindex]); nodesort(ref dblarray ,minchildrenindex); } } } } /// <summary> /// 节点排序 /// </summary> /// <param name="dblarray"></param> /// <param name="startindex"></param> private static void nodesort(ref double[] dblarray,int startindex) { while(2*startindex+1 < dblarray.length) { int minchildrenindex = 2*startindex+1 ; if(2*startindex+2 < dblarray.length ) { if(dblarray[2*startindex+1]>dblarray[2*startindex+2]) { minchildrenindex = 2*startindex+2; } } if(dblarray[startindex] > dblarray[minchildrenindex]) { exchagevalue(ref dblarray[startindex],ref dblarray[minchildrenindex]); startindex = minchildrenindex ; } } } /// <summary> /// 交换值 /// </summary> /// <param name="a"></param> /// <param name="b"></param> private static void exchagevalue(ref double a , ref double b) { double temp = a ; a = b ; b = temp ; } |
新闻热点
疑难解答