冒泡排序,选择排序和快速排序的Java实现

简单说:冒泡就是两两比较,交换位置,

快速就是双向遍历交换位置,直到开始和结束处于同一位置的时候。

直接贴代码:

冒泡:

 

package com.wuyjngjie.sort;

public class BubbleSort {

private static int [] intArray ={23,53,1,8,45,9,57,74};
public static void main(String[] args) {
new BubbleSort().sort();
for(int a : intArray){
System.out.println(a);
}
}

public void sort(){
int temp;
for (int i = 0; i < intArray.length-1; i++) {
for(int j = 0; j < intArray.length-1-i; j++){
if(intArray[j]>intArray[j+1]){
temp=intArray[j];
intArray[j] = intArray[j+1];
intArray[j+1] = temp;
}
}

}
}

}

 

快速:

package com.wuyjngjie.sort;

public class qucikSort {

public static void main(String[] args) {
int[] array = { 23, 56, 78, 45, 11, 4, 6, 8 };
int start = 0;
int end = array.length - 1;
sort(array, start, end);
for (int a : array) {
System.out.print(a + ",");
}

}

private static void sort(int[] array, int low, int high) {

int start = low;
int end = high;
int key = array[low];

while (end > start) {
while (end > start && array[end] > key) {
end--;
}
if (array[end] < key) {
int temp = array[start];
array[start] = array[end];
array[end] = temp;
}
while (start < end && array[start] < key) {
start++;
}
if (array[start] > key) {
int temp = array[start];
array[start] = array[end];
array[end] = temp;
}
}
int d=0;
if(start>low){
sort(array, low, start-1);
}
if(end<high){
sort(array, end+1, high);
}
}

}

 选择排序

package sortAlgorithm;

public class SelectSort {
    
    private static int [] intArray ={23,53,1,8,45,9,57,74};
    public static void main(String[] args) {
        new SelectSort().sort();
        for(int a : intArray){
            System.out.println(a);
        }
    }
    
    public void sort(){
        int temp;
        for (int i = 0; i < intArray.length-1; i++) {
            for(int j = i+1; j < intArray.length; j++){
                if(intArray[i]>intArray[j]){
                    temp=intArray[j];
                    intArray[j] = intArray[i];
                    intArray[i] = temp;
                }
            }
            
        }
    }
    
}

 

posted on 2017-04-18 13:40  逆天丶  阅读(248)  评论(0编辑  收藏  举报