一、冒泡排序--最出名的排序,总共八大排序
-
public class Demo2 { public static void main(String[] args) { //冒泡排序 int[] arrays={1,20,3,7,6,34,12,19,34,11}; Demo2 demo2 = new Demo2(); demo2.sort(arrays); } public void sort(int[] arrays){ int temp; for (int i = 0; i <arrays.length-1; i++) {//-1是为了避免outofbounds错误 for (int j = 0; j <arrays.length-1 ; j++) { if (arrays[j]>arrays[j+1]){ temp=arrays[j]; arrays[j]=arrays[j+1]; arrays[j+1]=temp; } } }//第一个for是判断整个数组,第二个for是判断两个数字需不需要置换 for (int i = 0; i <arrays.length ; i++) { System.out.print(arrays[i]+" "); } } }
二、稀疏数组--五子棋