排序3

karte 2011-04-09 12:41:35
public static void shellSort()
{
int t = (int)(Math.log((double)elem.length + 1) / Math.log((double)2)) - 1;
int[] incr = new int[t];
for (int i = 0; i < incr.length; i++)
{
incr[i] = (int)Math.pow(2, t - i) - 1;
}

for (int i = 0; i < t; i++)
{
shellInsertSort(elem.length, incr[i]);
}
}

public static void quickSortHelp(int low, int high)
{
if(low < high)
{
int pivotLoc = partition(low, high);
quickSortHelp(low, pivotLoc - 1);
quickSortHelp(pivotLoc + 1, high);
}
}

public static int partition(int low, int high)
{
while(low < high)
{
while(low < high && elem[high] >= elem[low])
{
high--;
}
swap(low, high);

while(low < high && elem[low] <= elem[high])
{
low++;
}
swap(low, high);
}
return low;
}

public static void simpleSelectionSort()
{
for (int i = 0; i < elem.length - 1; i++)
{
int lowIndex = i;
for (int j = i + 1; j < elem.length; j++)
{
if(elem[j] < elem[lowIndex])
{
lowIndex = j;
}
}
swap(i, lowIndex);
}
}

public static void siftAdjust(int low, int high)
{
for (int i = low, j = 2 * low + 1; j <= high; j = 2 * j + 1)
{
if(j < high && elem[j] < elem[j + 1])
{
j++;
}

if(elem[i] >= elem[j])
{
break;
}
swap(j, i);
i = j;
}
}

public static void heapSort()
{
for (int i = (elem.length - 2) / 2; i >= 0; i--)
{
siftAdjust(i, elem.length - 1);
}

for(i = elem.length - 1; i > 0; i--)
{
swap(0, i);
siftAdjust(0, i - 1);
}
}

public static void swap(int low, int high)
{
int temp = 0;
if(elem[low] > elem[high])
{
temp = elem[low];
elem[low] = elem[high];
elem[high] = temp;
}
}
}
...全文
50 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

23,403

社区成员

发帖
与我相关
我的任务
社区描述
Java 非技术区
社区管理员
  • 非技术区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧