public class Sort {
/**
* 插入排序
* @param numArray
*/
public void insertSort(int[] numArray)
{
if(numArray == null || numArray.length == 0)
return;
int i,j,temp;
for(i = 1;i < numArray.length;i++)
{
temp = numArray[i];
for(j = i-1;j >= 0 && numArray[j] > temp;j--)
{
numArray[j+1] = numArray[j];
}
numArray[j+1] = temp;
}
}
/**
* 冒泡排序
* @param numArray
*/
public void bubbleSort(int[] numArray)
{
int i,j,temp;
if(numArray == null || numArray.length == 0)
return;
for(i = 0;i < numArray.length-1;i++)
{
for(j = i+1;j < numArray.length;j++)
{
if(numArray[j] < numArray[i])
{
temp = numArray[i];
numArray[i] = numArray[j];
numArray[j] = temp;
}
}
}
}
/**
* 选择排序
* @param numArray
*/
public void selectSort(int[] numArray)
{
if(numArray == null || numArray.length == 0)
return;
int i,j,index,min,temp;
for(i = 0;i < numArray.length-1;i++)
{
index = i+1;
min = numArray[index];
for(j = i+1;j < numArray.length;j++)
{
if(numArray[j] < min)
{
index = j;
min = numArray[index];
}
}
if(numArray[i] > numArray[index])
{
temp = numArray[i];
numArray[i] = numArray[index];
numArray[index] = temp;
}
}
}
/**
* 归并排序
* 实现思想是: 先划分后合并,其中合并的思想就是合并两个有序数组而已啦
* @param numArray
* @param low
* @param high
*/
public void mergeSort(int[] numArray,int low,int high)
{
if(numArray == null || numArray.length == 0 ||low > high)
return;
int middle = (low+high)/2;
if(low < high)
{
//划分左边
mergeSort(numArray, low, middle);
//划分右边
mergeSort(numArray, middle+1, high);
//合并
merge(numArray, low, middle, high);
}
}
/**
* 合并两个子数组,具体来讲两个子数组的范围是:(low----middle)与(middle+1----high)
* @param numArray
* @param low
* @param middle
* @param high
*/
public void merge(int[] numArray,int low,int middle,int high)
{
int[] temp = new int[high-low+1];//设置临时数组,这个数组中存储的就是最后排好序的两个子数组的所有元素
//i,j分别用于遍历两个子数组
int i = low;
int j = middle+1;
int k = 0;
while(i <= middle && j <= high)
{
if(numArray[i] <= numArray[j])
temp[k++] = numArray[i++];
else
temp[k++] = numArray[j++];
}
while(i <= middle)
{
temp[k++] = numArray[i++];
}
while(j <= high)
{
temp[k++] = numArray[j++];
}
//替换原先数组中的元素
for(k = 0;k < temp.length;k++)
numArray[low+k] = temp[k];
}
/**
* 快速排序
* @param numArray
* @param low
* @param high
*/
public void quickSort(int[] numArray,int low,int high)
{
if(low < high)
{
int position = partation(numArray, low, high);
quickSort(numArray, low, position-1);
quickSort(numArray, position+1, high);
}
}
/**
* 快速排序的划分算法,返回值是主元所在的位置
* @param numArray
* @param low
* @param high
* @return
*/
public int partation(int[] numArray,int low,int high)
{
int privot = numArray[low];//设置主元
int position = low;//返回的主元的位置,初始值是low
int temp = low;//用于交换的中间元素
while(low < high)
{
while(low < high && numArray[high] >= privot)
high--;
if(low < high && numArray[high] < privot)
{
temp = numArray[high];
numArray[high] = numArray[position];
numArray[position] = temp;
position = high;
}
while(low < high && numArray[low] <= privot)
low++;
if(low < high && numArray[low] > privot)
{
temp = numArray[low];
numArray[low] = numArray[position];
numArray[position] = temp;
position = low;
}
}
return position;
}
/**
* 堆排序
* 实现思路:首先创建初始堆,接着将堆顶元素和最后一个元素进行交换,从上到下调整堆结构
* 如果是从小到大排序的话,创建的是大根堆,如果是从大到小排序的话,创建的是小根堆
* @param numArray
*/
public int[] heapSort(int[] numArray)
{
initHeap(numArray);//初始化堆
int count = 0;
int temp;
if(numArray == null)
return null;
if(numArray.length == 0)
return numArray;
for(int i = 0;i < numArray.length;i++)
{
temp = numArray[0];
numArray[0] = numArray[numArray.length-count-1];
numArray[numArray.length-count-1] = temp;
count++;
adjustHeap(numArray, 0,numArray.length-count);
}
return numArray;
}
/**
* 初始化建堆
* @param numArray
*/
public void initHeap(int[] numArray)
{
int temp;
int index = (numArray.length-1)/2;//初始化堆中最后一个非叶结点的位置
while(index >= 0)
{
adjustHeap(numArray, index,numArray.length);
index--;
}
}
/**
* 从上到下调整堆
* @param numArray
* @param startIndex 开始调整的位置
*/
public void adjustHeap(int[] numArray,int startIndex,int endIndex)
{
int temp;
int tempIndex;
while(startIndex < endIndex)
{
tempIndex = startIndex;
startIndex = judgeValue(numArray, startIndex*2+1, startIndex*2+2,endIndex);
if(startIndex == -1)
break;
if(numArray[tempIndex] < numArray[startIndex])
{
temp = numArray[tempIndex];
numArray[tempIndex] = numArray[startIndex];
numArray[startIndex] = temp;
}
}
}
/**
* 返回较大值所在的下标
* @param numArray
* @param first
* @param second
* @return
*/
public int judgeValue(int[] numArray,int first,int second,int endIndex)
{
int length = endIndex;
int temp,index = 0;
if(first < length && second < length)
{
if(numArray[first] >= numArray[second])
index = first;
else
index = second;
}else if(first < length && second >= length)
{
index = first;
}else if(first >= length)
{
index = -1;
}
return index;
}
/*
* 打印输出结果
* @param numArray
* @param content
*/
public void print(int[] numArray,String content)
{
System.out.print(content+": ");
for(int i = 0;i < numArray.length;i++)
System.out.print(numArray[i]+" ");
System.out.println();
}
public static void main(String[] args) {
Sort sort = new Sort();
int[] numArray = {1,2,3,5,2,3};
//sort.insertSort(numArray);
//sort.print(numArray, "插入排序结果");
//sort.bubbleSort(numArray);
//sort.print(numArray, "冒泡排序结果");
//sort.selectSort(numArray);
//sort.print(numArray, "选择排序结果");
//sort.mergeSort(numArray, 0, numArray.length-1);
//sort.print(numArray, "归并排序结果");
//sort.quickSort(numArray, 0, numArray.length-1);
//sort.print(numArray, "快速排序结果");
sort.heapSort(numArray);
sort.print(numArray, "堆排序结果");
}
}
排序类算法总结
最新推荐文章于 2023-04-28 16:59:28 发布