//快速排序
public class Test{
public static void main(String args []){
int [] array = {49,38,65,97,76,13,27};
sort(array,0,array.length - 1);
for(int i = 0;i < array.length;i++)
System.out.print(" "+array[i]);
}
/**
* 交换指定数组a的两个变量的值
* @param array 数组应用
* @param low 数组下标
* @param hight 数组下标
*/
public static void swap(int [] array,int low,int hight){
if(low == hight)return;
int temp = array[low];
array[low] = array[hight];
array[hight] = temp;
}
/**
*
* @param array 待排序数组
* @param low 数组下标下界
* @param hight 数组下标上界
* @return n
*/
public static int getPos(int [] array,int low,int hight){
int n = low; //当前位置为第一个元素所在位置
int m = array[n]; //采用第一个元素为轴
for(int i = low + 1;i <= hight;i++){
if(array[i] < m){
n++;
swap(array,n,i);
}
}
swap(array,low,n);
return n;
}
/**
* 快速排序实现
* @param array
* @param low
* @param hight
*/
public static void sort(int [] array,int low,int hight){
if(low < hight){
int m = getPos(array,low,hight);
getPos(array,low,m - 1);
getPos(array,m + 1,hight);
}
}
}
//选择法
public class Test1{
public static void main(String args []){
int [] array = {49,38,65,97,76,13,27};
sort(array,0,array.length - 1);
for(int i = 0;i < array.length;i++)
System.out.print(" "+array[i]);
}
public static void swap(int [] array,int low,int hight){
int temp = array[low];
array[low] = array[hight];
array[hight] = temp;
}
public static void sort(int [] array,int low,int hight){
for(int i = 0;i < array.length;i++){
for(int j = 0;j < array.length;j++)
if(array[i] > array[j])
swap(array,i,j);
}
}
}
//冒泡法
public class Test1{
public static void main(String args []){
int [] array = {49,38,65,97,76,13,27};
sort(array,0,array.length - 1);
for(int i = 0;i < array.length;i++)
System.out.print(" "+array[i]);
}
public static void swap(int [] array,int low,int hight){
int temp = array[low];
array[low] = array[hight];
array[hight] = temp;
}
public static void sort(int [] array,int low,int hight){
for(int i = 0;i < array.length;i++){
for(int j = hight;j > 0;j--)
if(array[j - 1] < array[j])
swap(array,j - 1,j);
}
}
}
//插入法
public class Test1{
public static void main(String args []){
int [] array = {49,38,65,97,76,13,27};
sort(array,0,array.length);
for(int i = 0;i < array.length;i++)
System.out.print(" "+array[i]);
}
public static void sort(int [] array,int low,int hight){
for(int i = 1;i < array.length;i++){
int j = i - 1;
int n = array[i];
while(j >= 0 && n < array[j]){
array[j+1] = array[j];
j--;
}
array[j + 1] = n;
}
}
}