欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Java冒泡排序、选择排序代码实现(初学者)

程序员文章站 2022-03-29 17:09:28
...

import java.util.Arrays;

public class rank {
	public static void main(String[] args) {
		// 冒泡排序
		int[] pao = { 3, 5, 8, 1, 2, 6, 4, 9, 7 };
		System.out.println("冒泡排序:");
		for (int j = 0; j < pao.length - 1; j++) {
			for (int i = 0; i < pao.length - 1 - j; i++) {
				int temp;
				if (pao[i] > pao[i + 1]) {
					temp = pao[i];
					pao[i] = pao[i + 1];
					pao[i + 1] = temp;
				}
			}
			System.out.println(Arrays.toString(pao));
		}

		System.out.println("--------------------------");

		// 选择排序
		int[] xuanze = { 5, 8, 100, 26, 32, 16, 54, 69, 7 };
		int s;
		System.out.println("选择排序:");
		for (int i = 0; i < xuanze.length; i++) {
			int mid = i;
			s = xuanze[i];
			for (int j = i; j < xuanze.length; j++) {
				if (xuanze[j] < s) {
					s = xuanze[j];
					mid = j;
				}
			}
			xuanze[mid] = xuanze[i];
			xuanze[i] = s;
			System.out.println(Arrays.toString(xuanze));
		}

		System.out.println("--------------------------");


	}
}

相关标签: java 排序算法