1、冒泡排序
关于冒泡排序,其实就是相邻两两对比,正序反序,大的(小的)往后挪一个位置,第一遍最大(最小)肯定会在最后了,
然后第二次排序不计最后一个元素进行重排,然后以此类推
public static void main(String[] args){
int score[] = {3,5,8,3,5,6,9,7,4,1,5,98,7,6,12,7,45,56,5};
for ( int i=0;i<score.length-1;i++) {
for (int j=0;j<score.length-i-1;j++){
if(score[j]>score[j+1]){
int temp =score[j];
score[j]=score[j+1];
score[j+1]=temp;
}
}
}
for ( int x: score) {
System.out.println(x);
}
}
2、选择排序
关于选择排序,选择排序是怎样的,就是拿第一个,跟后面23456挨个去对比,如果第二个比第一个大,哎,记住第二个的下标,拿第二个跟第三个比去,以此类推,记住最大或最小的下标,然后跟第一个互换。每次第一个、第二个.....就是参与排序的里面总是最大或者最小的了
public static void main(String[] args) {
int[] score={3,5,8,3,5,6,9,7,4,1,5,98,7,6,12,7,45,56,5};
for(int i = 0; i < score.length - 1; i++) {
int step = i;
for(int j = step + 1; j < score.length; j++){
if(score[j] > score[step]){
step = j;
}
}
if(i != step){
int temp = score[i];
score[i] = score[step];
score[step] = temp;
}
}
for(int x:score){
System.out.println(x);
}
}
折半查找: