java算法1 冒泡排序

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/AngeloWolf/article/details/52234546
package com.angelo;
import java.util.Random;
import java.util.Scanner;

public class BubbleSort {
@SuppressWarnings("resource")
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("输入数组大小:");
int k = sc.nextInt();
int array[] = new int[k];
for (int i = 0; i < array.length; i++) {
Random rd = new Random();
int s = rd.nextInt(30);
array[i] = s;
}
System.out.println("数组排序之前:");
print(array);
System.out.println("数组排序之后:");
bubbleSort(array);
print(array);
}
    public static void print(int[] a){
    for(int i=0;i<a.length;i++){
    System.out.print(a[i]+"\t");
    }
    System.out.println();
    }
    public static void bubbleSort(int [] b){  
    for(int i=0;i<b.length;i++){
    for(int j=i;j<b.length;j++){
    if(b[j]<b[i])
    {
    int temp;
    temp=b[j];
    b[j] = b[i];
    b[i] = temp;
    }
    }
    }
    }
}

猜你喜欢

转载自blog.csdn.net/AngeloWolf/article/details/52234546
今日推荐