我面试时总结的:
public class Mppx {
public static void main(String[] args) {
int[] values={16,25,9,90,23};
Mppx.sort(values);
for(int i=0; i<values.length; ++i){
System.out.println("index:"+i+"value:"+values);
}
}
//定义数组
public static void sort(int[] values){
int temp;
//冒泡排序
for (int i = 0; i < values.length; i++) {
for (int j = 0; j < values.length-i-1; j++) {
//每个数比较n次,如果values[j]>values[j+1]成立,否则交换。
if(values[j]>values[j+1]){
temp = values[j];
values[j] = values[j+1];
values[j+1]=temp;
}
}
}
}
}
//冒泡排序1
int[] str3 = {1,4,5,2,0,9,5,6,8,2,4};
int temp;
for (int i = 0; i < str3.length; i++) {
for (int j = 0; j < str3.length-i-1; j++) {
if(str3[j]>str3[j+1]){
temp = str3[j];
str3[j] = str3[j+1];
str3[j+1] = temp;
}
}
}
for (int i = 0; i < str3.length; i++) {
System.out.print(str3);
}
System.out.println("----------------------------");
//冒泡排序2
String[] text = {"3","4","1","9","8","0","5","4","8","1"};
String temp1 = "";
for(int i=0; i<text.length; i++){
for(int j=i+1;j<text.length; j++){
if (text.compareTo(text[j]) < 0) {
temp1 = text;
text = text[j];
text[j] = temp1;
}
}
}
System.out.print(Arrays.toString(text));
}
|