排序算法
2021-03-30 03:24
package org.lanqiao.数据结构;
public class Demo {
static int[] items= {11,21,13,14,35,26,57,88,59,110,15};
//99,88,77,111,11,1
//
public static void main(String[] args) {
p();
System.out.println("排序后");
quickSort(0,items.length-1);
p();
}
public static void p() {
System.out.println();
for (int i : items) {
System.out.print(i+"\t");
}
}
public static void quickSort(int begin,int end) {
//{45,21,13,11,35,26,57,88,59,110,15};
//[15,21,13,11,35,26],45,[57,88,59,110]
//[13,11],15,[21,35,26],[45],57,[88,59,110]
int i=begin,j=end,num=items[i];
while (i
break;
}
j--;
}
while (i
items[j]=items[i];
break;
}
i++;
}
}
items[i]=num;
if (i-1>begin) {
quickSort(begin,i-1);
}
if (i+1
}
}
public static void insertSort() {
//{111,21,13,11,35,26,57,88,59,110,15};
int temp=0;
for (int i = 1; i for (int j = 0; j temp=items[i];
if (items[i]>items[j]) {
for (int k = i; k >j; k--) {
items[k]=items[k-1];
}
items[j]=temp;
break;
}
}
}
}
public static void selectSort() {
int temp=0;
int maxIndex=0;
for (int i = 0; i maxIndex=i;
for (int j = i+1; j if (items[maxIndex]
}
}
//maxIndex是最大下标值
temp=items[i];
items[i]=items[maxIndex];
items[maxIndex]=temp;
}
}
public static void mpSort() {
//11,21,13,14,35,26,57,88,59,110,15
int temp=0;
for (int i = 0; i for (int j = 0; j if (items[j]>items[j+1]) {
temp=items[j];
items[j]=items[j+1];
items[j+1]=temp;
}
}
}
}
//二分查找
public static int test1(int num) {
int min=0,max=items.length-1,mid=0;
//结束条件大家自己去想
while (true) {
mid=(min+max)/2;
if (num==items[mid]) {
return mid;
}else if(num>items[mid]){
min=mid;
}else {
max=mid;
}
}
}
}
上一篇:C#实现按键弹出查询窗口