选择排序算法:
//选择排序
public static void selectSort(int[] array) {
for(int i=0;i<array.length-1;i++) {
for(int t=i+1;t<array.length;t++) {
if(array[i]>array[t]) {
int temp=array[i];
array[i]=array[t];
array[t]=temp;
}
}
}
}
//遍历输出函数
public static void printSort(int[] array) {
System.out.print("[");
for(int i=0;i<array.length;i++) {
if(i<array.length-1) {
System.out.print(array[i]+",");
}
else {
System.out.print(array[i]+"]");
}
}
}
冒泡排序算法:
/*冒泡排序
* 思路:数组内元素将相邻两者间进行比较,每次排序都有一个元素排至最终位置
*/
public static void poolSort(int[] array) {
for(int x=0;x<array.length-1;x++) {
for(int y=0;y<array.length-1-x;y++) {
if(array[y]>array[y+1]) {
int temp=array[y];
array[y]=array[y+1];
array[y+1]=temp;
}
}
}
}
快速排序算法:
package oneDay;
public class QuickSort {
public static void main(String[] args) {
// TODO Auto-generated method stub
int A[] = new int[]{45,56,25,84,46,31,21,15,5,79};
quickSort(A,1,10);
//System.out.print("[");
for (int i=0;i<A.length;i++) {
System.out.print(A[i]+" ");
}
}
public static int Partition(int A[],int low,int high)//划分函数
{
int x = A[high-1];//将数组A内最后一个元素值赋给变量x
int i = low - 1;//最低下标
int temp;
for(int j=low;j<=high-1;j++){
if(A[j-1]<=x){
i++;
temp = A[j-1];
A[j-1] = A[i-1];
A[i-1] = temp;
}
}
temp = A[high-1];
A[high-1] = A[i];
A[i] = temp;
return i+1;
}
public static void quickSort(int A[],int low,int high) //排序函数
{
if(low < high){
int q = Partition(A, low, high);
quickSort(A, low, q-1);
quickSort(A, q+1, high);
}
}
}