三种常见的排序算法:选择排序,冒泡排序和快速排序算法(Java语言)

选择排序算法:

//选择排序
	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);
        }
	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值