1.冒泡排序: 首先从数组的最左边开始的,取出第0位置的(左边)的数据和第一号的位置的(右边)的数据,如果左边的数据大于右边的数据,则进行交换,否则不进行交换,沿着这个算法一直延续下去,最大的就会排到最右边,然后再取出这个最大的数放到一边,把剩余的在重新按照这个算法再排,就排出来了。
2.动态分析:
3 1 6 2 5 第一次排序 1 3 6 2 5 1 3 6 2 5 1 3 2 6 5 1 3 2 5 6 1 3 2 5 第二次排序 1 3 2 5 1 2 3 5 1 2 3 第三次排序 1 2 3 1 2 3 1 2 第四次排序 1 2
3.相关的Java代码:
package com.cal.paisu; public class Maopao { public static void main(String[] args) { int a[] = {3,1,6,2,5}; int temp; for(int i=a.length-1;i>0;i--){ for(int j=0;j<i;j++){ if(a[j]>a[j+1]){ temp = a[j]; a[j] = a[j+1]; a[j+1] = temp; } } } for(int i=0;i<a.length;i++){ System.out.println(a[i]); } } } 结果: 1 2 3 5 6相关的c语言代码 #include<stdio.h> void main(void) { int i; int j; int temp; int number[10] = {95, 45, 15, 78, 84, 51, 24, 12, 38, 97}; for(i=0;i<10;i++) { for(j=0;j<10-i-1;j++) { if(number[j]>number[j+1]) { temp = number[j]; number[j] = number[j+1]; number[j+1] = temp; } } } for(i=0;i<10;i++) { printf("%d", number[i]); printf("\n"); } }