排序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;
}
}
}