public class InsertSort
{
public void insertSort(int[] data, boolean isDesc)
{
if(isDesc == false)
{
for(int i = 1; i < data.length; i++)
{
if(data[i] >= data[i - 1])
{
continue;
}
int key = data[i];
for(int j = i - 1; j >= 0; j--)
{
if(data[j] > key)
{
data[j + 1] = data[j];
}
else
{
data[j] = key; break;
}
}
}
}
else
{
for(int i = 1; i < data.length; i++)
{
if(data[i] <= data[i - 1])
{
continue;
}
int key = data[i];
for(int j = i - 1; j >= 0; j--)
{
if(data[j] < key)
{
data[j + 1] = data[j];
}
else
{
data[j] = key; break;
}
}
}
}
}
private void swap(int[] data, int m, int n)
{
int temp = data[m];
data[m] = data[n];
data[n] = temp;
}
}
Insert Sort
最新推荐文章于 2019-06-10 17:42:21 发布