快速排序
package test1;
public class QuicklySort {
public static void main(String[] args) {
int[] a ={2,1,6,3,5,9,7,8};
sort(a,0,a.length-1);
for(int i=0;i<a.length;i++)
System.out.print(a[i]);
}
public static void sort(int[] a,int start,int end){
int i=start,j=end;
//如果开始下标大于结束下标
if(start<end){
int temp = a[i];//temp就是第一个元素
while(i!=j){
while(a[j]>=temp&&j>i)//如果j>i并且a[j]>temp坐标向左移
j--;
a[i]=a[j];
while(a[i]<=temp&&j>i)//如果j>i并且a[i]<=temp坐标右移
i++;
a[j]=a[i];
}
a[i]=temp;
//递归处理左右的数据
sort(a,0,i-1);
sort(a,i+1,end);
}
}
}
冒泡排序
package test1;
public class MaopaoOrder {
public static void main(String[] args) {
int[] a = {1,6,2,8,3,5,4};
// sort(a);
supperSort(a);
for(int i=0;i<a.length;i++){
System.out.print(a[i]);
}
}
/**
* 冒泡排序是一种典型的交换方式,基本思路是:通过无序中相邻元素比较和位置的交换,是小的元素如气泡
* 一般逐步往上漂浮直到水面
* @param a
*/
public static void sort(int[] a){
int len =a.length;
for(int i=0;i<len;i++){
for(int j=len-1;j>i;j--){
if(a[j]<a[j-1]){
int temp =a[j];
a[j]=a[j-1];
a[j-1]=temp;
}
}
}
}
public static void supperSort(int[] a){
int len =a.length;
boolean flag = true;
for(int i=0;i<len;i++){
for(int j=len-1;j>i;j--){
if(a[j]<a[j-1]){
int temp = a[j];
a[j] = a[j-1];
a[j-1] = temp;
flag = false;
}
}
if(flag)
break;
}
}
}
希尔排序
package test1;
public class XierOrder {
public static void main(String[] args) {
int a[] = {5,2,1,0,8,6};
xiErOrder(a);
for(int i=0;i<a.length;i++)
System.out.print(a[i]);
}
/**
* 希尔排序实际上是一种分组插入方法,基本思想是:先取定一个小于n的整数d1作为第一个增量,
* 把表的全部元素分成d1个组,所有相互之间的距离为d1的倍数的元素放在同一个组中,在各组内
* 进行直接插入排序;然后,取第二个增量d2重复上面的步骤直到d1=1
* @param a
*/
public static void xiErOrder(int[] a){
int gap;
int len = a.length;
int j;
gap=len/2;
while(gap>0){
//对相个gap位置的元素组采用直接插入排序
for(int i =gap;i<len;i++){
int temp = a[i];
j=i-gap;
//对相隔gap的位置元素进行排序
while(j>=0&&temp<a[j]){
a[j+gap]=a[j];
j=j-gap;
}
a[j+gap]=temp;
}
gap=gap/2;
}
}
}
二分排序
package test1;
public class HalfSort {
public static void main(String[] args) {
int[] a={2,1,4,7,5,6,0};
insertSort(a);
for(int i=0;i<a.length;i++)
System.out.print(a[i]);
}
/**
* 主要是利用二分查找后再排序
* @param a
*/
public static void insertSort(int[] a){
int len = a.length;
//声明low,high,mid
int low,high,mid = 0;
//将没排序的数据依次取出进行排序
for(int i=1;i<len;i++){
int temp = a[i];
//设置high
high = i-1;
//设置low
low=0;
//只有当high>=low进行查找
while(high-low>=0){
mid=(low+high)/2;
if(temp<a[mid])
high=mid-1;
else if(temp>a[mid])
low = mid+1;
else break;
}
//如果想不清可以一个数据进行分析(i=1那么j=0,high=-1)吧
for(int j=i-1;j>=high+1;j--)
a[j+1]=a[j];
a[high+1]=temp;
}
}
}
直接排序
package test1;
public class DirectorInsert {
public static void main(String[] args) {
int a[] = {2,3,1,7,4,5};
// int[] b = directInsertOrder(a);
// for(int i=0;i<b.length;i++)
// System.out.print(b[i]);
insertOrder(a);
for(int i=0;i<a.length;i++)
System.out.print(a[i]);
}
/**
* 本方法的思路是:先分出两个数组出来,一个是已经排好序的,一个是等待排序的
* 刚开始已经排好的数据时a[0]
* 然后重待排序中取出一个和前面排好的比是从右向左比直到j<0或temp>=a[j]
* @param a
* @return
*/
public static int[] directInsertOrder(int[] a){
int len = a.length;
int[] b = new int[len];
b[0]=a[0];
for(int i=1;i<len;i++){
int temp = a[i];
int j=i-1;
while(j>=0&&temp<b[j]){
b[j+1]=b[j];
j--;
}
b[j+1]=temp;
}
return b;
}
public static void insertOrder(int[] a){
//获取数组的长度
int len = a.length;
//由于刚开始排好的是a[0]所以我们重待排的中逐步取出要排的数据
for(int i=1;i<len;i++){
//获得要排的数据
int temp = a[i];
//获取排好数组的最后元素的下标
int j = i-1;
//如果数组下标大与-1并且temp比要排好的数小
while(j>=0&&temp<a[j]){
//把数据往后移,因为要排的数是在他们中间
a[j+1]=a[j];
j--;
}
//把要排数插入
a[j+1]=temp;
}
}
}