public class Main {
public static void main(String[] args)
{
int [] a ={3,5,6,7,8,23,33,44,42,34,56,-1,90,45,11,54};
// MergeSort(a,0,a.length-1);
// HeapSort(a);
// getBig(a);
// BinarySort(a);
// ShellSort(a);
// BubbleSort(a);
// QuickSort(a,0,a.length-1);
SelectSort(a);
for (int i = 0; i <a.length ; i++)
{
System.out.println(a[i]);
}
// System.out.println(a[index]);
}
//二路归并排序
public static void MergeSort(int[]a ,int low,int high)
{
if(low <high)
{
int mid = (low +high)/2;
MergeSort(a,low+0,mid+0);
MergeSort(a,mid+1,high+0);
merge(a,low,mid,high);
}
}
private static void merge(int[] a,int low,int mid,int high)
{
int[] tempArr = new int[a.length];
int leftStart = low;
int rightStart = mid+1;
int tempIndex =low;
while (leftStart<=mid&&rightStart<=high)
{
if (a[leftStart]<a[rightStart])
tempArr[tempIndex++] = a[rightStart++];
else
tempArr[tempIndex++] = a[leftStart++];
}
while (leftStart<=mid)
tempArr[tempIndex++] = a[leftStart++];
while (rightStart<=high)
tempArr[tempIndex++] = a[rightStart++];
while(low<=high)
{
a[low] =tempArr[low++];
}
}
//堆排序
public static void HeapSort(int[] a)
{
int len = a.length -1;//最大节点的下标
for (int i = (len - 1)/2; i >=0; i--)
{
adjustHeap(a,i,len);
}
for (int i = len; i >0 ; i--)
{
int temp = a[0];
a[0] = a[i];
a[i] = temp;
adjustHeap(a,0,i-1);
}
}
private static void adjustHeap(int[] a,int low,int high)
{
int i = low;
int temp =a[i];
int j = 2*i+1;
while (j<=high)
{
if (j<high&&a[j]>a[j+1])
++j;//选择较大的值
if (temp>a[j])
{
a[i] = a[j];
i = j;
j =2*i+1;
}
else
break;
}
将刚刚的父节点中的数据赋值给新位置
a[i] =temp;
}
//实现找出最大元素和次大元素的算法
private static int getBig(int [] a)
{
int len = a.length;
int big_1 = a[0];
int big_2 = Integer.MAX_VALUE;
if(len <2)
return -1;
for (int i = 1; i <len ; i++)
{
if (a[i]>big_1)
{
big_2 = big_1;//保存次大值
big_1 = a[i];//保存最大值
}
else if (a[i]>big_2&& a[i] != big_1)
big_2 =a[i];
}
System.out.println("最大值"+big_1+"次大值"+big_2);
return -1;
}
public static void findAndPrintClosest(int[] arrayOne, int expectedSum)
{
int lenOne = arrayOne.length; //数组长度,默认全部填满。
int diff = Integer.MAX_VALUE; //定义两数与期望值的差值;默认取最大整数
int resultOne = 0; //第一个元素
int resultTwo = 0; //另一个元素
int leftIndex = 0; //左边开始的数组下标
int rightIndex = lenOne - 1; //右边开始的数组下标
int count =0;
while (leftIndex < lenOne && rightIndex >= 0 && leftIndex != rightIndex) {//若左下标不超过数组长度,右下标不小于0,并且左右元素不相等
if (Math.abs(arrayOne[leftIndex] + arrayOne[rightIndex] //若两个数的和减去期望值 所得的差值,比当前的差值小,说明找到了更合适的元素组合
- expectedSum) < diff) {
resultOne = arrayOne[leftIndex];//更新结果值
resultTwo = arrayOne[rightIndex];
diff = Math.abs(resultOne + resultTwo - expectedSum);//更新差值
} else if (arrayOne[leftIndex] + arrayOne[rightIndex] < expectedSum) {//若当前差值依然是最小的,并且两数之和比期望值小,说明应该增大左边的值
leftIndex++;
} else { //若当前差值依然是最小的,并且两数之和比期望值大,说明应该减小右边的值
rightIndex--;
}//if
}//end of while
System.out.println("----------->"+resultOne+"----------->"+resultTwo);
}
public static void findclosedSum(int [] a,int expectedSum)
{
int len = a.length;
int low = 0;
int high = len -1;
int index1 = -1;
int index2 = -1;
int dif =Integer.MAX_VALUE;
while (low<high)
{
int temp= Math.abs(a[low]+a[high] -expectedSum);
if(temp <dif )
{
index1 = low;
index2 = high;
dif = temp;
}
else if (a[low]+a[high] <expectedSum)
{
low++;
}
else
{
high--;
}
}
System.out.println("----------->"+a[index1]+"----------->"+a[index2]);
}
//直接插入排序
public static void InsertSort_first(int [] a)
{
int len = a.length;
for (int i = 1; i < len; i++)
{
int j =i;
int temp =a[i];
while (j>=1&&a[j-1]<temp)
{
a[j] = a[j-1];
j--;
}
a[j] = temp;
}
}
public static void InsertSort_two(int [] a)
{
int len = a.length;
for (int i = 1; i < len; i++)
{
int j =i;
int temp =a[i];
for (; j >0&&a[j-1]<temp ; j--)
{
a[j] = a[j-1];
}
a[j] = temp;
}
}
//折半查找
public static int BinarySearch(int[] a,int key)
{
int low =0;
int high = a.length-1;
while (low<=high)//注意这里的判断条件
{
int mid =(low+high)/2;
if (a[mid]== key)
return mid;
else if(key>a[mid])
high =mid -1;
else
low = mid+1;
}
return -1;
}
//折半插入排序
public static void BinarySort(int [] a)
{
// int low = 0;
// int high = a.length-1;
for (int i = 0; i <a.length ; i++)
{
int low =0;
int high = i -1;
int temp =a[i];
while (low <=high)
{
int mid =(low +high)/2;
if (a[mid]<temp)//从大到小排列 从左边找插入位置
high = mid -1;
else
low = mid+1 ;
}
for (int j = i; j >low ; j--)//移动过程
{
a[j] = a[j-1];
}
a[low] = temp;
}
}
/*希尔排序:
*希尔排序 又被称之为缩小增量排序。只不过是将待排序的序列按照某种规则分成几个子序列
* 分别对子序列进行排序,这个规则体现的事增量的选取 证明了自己还是没有理解这个的意思
*/
public static void ShellSort_first(int [] a)
{
if (a==null|| a.length<=1)
return;
int len =a.length ;
int incrementNum = a.length/2;
while(incrementNum >=1)
{
for (int i = 0; i < len; i++) {//内部使用的是冒泡排序
{
for (int j = i; j < len - incrementNum; j = j + incrementNum)//对每个分组的元素进行排序
{
if (a[j] > a[j + incrementNum])
{
int temp = a[j];
a[j] = a[j + incrementNum];
a[j + incrementNum] = temp;
}
}
}
}
incrementNum = incrementNum/2;
}
}
//交换类排序--起泡排序
public static void BubbleSort(int [] a)
{
int len = a.length;
for (int i = 1; i < len; i++)
{
int flag = 0;
//这个地方有问题
for (int j = 1; j <=len-i ; j++)
{
if (a[j-1]<a[j])//换到最后去 即为最终位置
{
int temp =a[j] ;
a[j] = a[j-1];
a[j-1] =temp;
flag =1;
}
}//注:关于值传递与引用传递的思考
if (flag==0)
return;
}
}
//快速排序
public static void QuickSort(int[] a,int low,int high)
{
//int len = a.length;
int i =low;
int j =high;
int temp = a[i];
if (i<j)//递归执行条件或者说不满足则结束
{
while (i<j)
{
while (i<j&&temp>a[j])//比枢纽小的关键字放在右边
j--;
if (i<j)
{
a[i] = a[j];
i++;
}
while (i<j&&temp<=a[i])//比枢纽大的关键字放在左边
i++;
if (i<j)
{
a[j] = a[i];
j--;
}
}//最终i ==这个位置即为枢纽元素的最终位置
a[i] =temp;//交换
QuickSort(a,low,i-1);
QuickSort(a,i+1,high);
}
}
//选择排序--简单选择排序
public static void SelectSort(int [] a)
{
int len = a.length;
for (int i = 0; i <len ; i++)
{
int temp =a[i];
int k =i;
for (int j = i+1; j <len ; j++)
{
if (a[k]<a[j])
k =j;//选择最大的元素的下标
}
a[i] = a[k];
a[k] = temp;
}
}
}