1.冒泡排序:
public class BubbleSort {
public static void main (String [] args ){
int values []={2,4,6,8,10};
sort(values);
for (int i=0;i<values.length;i++){
System.out.println("index:"+i+" "+"value:"+values[i]);
}
}
private static void sort(int[] values) {
// TODO Auto-generated method stub
int temp;
for(int i=0;i<values.length;i++){
for(int j=0;j<values.length-i-1;j++){
temp=values[j];
values[j]=values[j+1];
values[j+1]=temp;
}
}
}
}
2.插入排序
/**
* 插入排序
* @author Administrator
*
*/
public class InsertSort{
public static int [] values=new int []{3,2,5,6,8,7,1,4,9,10};
public static void main(String[] args) {
//从数组第二个元素开始排序,因为第一个元素本身肯定是已经排好序的
for(int i=0;i<values.length;i++){
// 复杂度 n
int key=values[i];
int j=i-1;
//依次跟之前的元素进行比较,如果发现比前面的原素小,则交换位置,最终完成排序。
while(j>=0 && values[j]>key){
values[j+1]=values[j];
values[j]=key;
j--;
}
}
/*
* 所以最终复杂度为n*n=n^2。
* 最优情况下(即都已经排列好的情况下),第二个n=1, 所以在最优情况下,复杂度为n。
*
*/
// 打印数组
printArray();
}
private static void printArray() {
for (int i : values){
System.out.print(i+ " ");
}
}
}
3.快速排序
public class QuickSort {
//待排数组
private static int[] input = new int[] { 2, 1, 5, 4, 9, 8, 6, 7, 10, 3 };
public static void main(String[] args) {
//快速排序
quickSort(input, 0, input.length - 1);
//打印数组
printArray();
}
private static void quickSort(int[] array, int from, int to) {
if (from < to) {
int temp = array[to];
int i = from - 1;
for (int j = from; j < to; j++) {
if (array[j] <= temp) {
i++;
int tempValue = array[j];
array[j] = array[i];
array[i] = tempValue;
}
}
array[to] = array[i+1];
array[i+1] = temp;
quickSort(array, from, i);
quickSort(array, i + 1, to);
} }
private static void printArray() {
for (int i : input) {
System.out.print(i + " ");
}
}
}
4.选择排序
public class SelectionSort {
private static int[] input = new int[] { 2, 1, 5, 4, 9, 8, 6, 7, 10, 3 };
public static void main(String[] args) {
for (int i=0; i<input.length-1; i++) {
//复杂度:n
int key = input[i];
int index = i;
//比较当前值和下一个值的关系,记录下较小值的值和索引数,用于交换。
for (int j=i+1; j<input.length; j++) {
//复杂度:1+2+...+(n-1)=Θ(n^2)
key = key < input[j] ? key : input[j];
index = key < input[j] ? index : j;
}
input[index] = input[i];
input[i] = key;
}
/*
* 复杂度分析:
* 最坏情况下,复杂度为:n*n=n^2(若略微精确的计算即为:n-1+1+2+...+n-1=(2+n)*(n-1)/2,
* 所以复杂度仍为n^2。
* 最优情况下,由于不论原数组是否排序好,均需要全部遍历以确定当前的最小值,所以复杂度不变仍未n^2。
*/
//打印数组
printArray();
}
private static void printArray() {
for (int i : input) {
System.out.print(i + " ");
}
}
}
5、交换排序
import java.util.Arrays;
public class Swapsort {
public static void main(String[] args) {
int [] values=new int[]{
(int) (Math.random()*100),
(int) (Math.random()*100),
(int) (Math.random()*100),
(int) (Math.random()*100),
};
System.out.println(Arrays.toString(values));
swap(values);
System.out.println(Arrays.toString(values));
}
private static void swap(int[] values) {
// TODO Auto-generated method stub
int temp;
int len=values.length;
for(int i=0;i<len/2;i++){
temp=values[i];
values[i]=values[len-i-1];
values[len-i-1]=temp;
}
}
}
Java分别实现冒泡排序、插入排序、快速排序、选择排序、交换排序
最新推荐文章于 2025-03-17 17:58:34 发布