|
@@ -11,24 +11,42 @@ import java.util.Arrays;
|
|
|
*/
|
|
|
public class SortTest {
|
|
|
|
|
|
- public static void main(String [] args){
|
|
|
+ public static void main(String[] args) {
|
|
|
|
|
|
|
|
|
- int[] arr = {4, 5, 6, 1, 2, 3, 7, 8, 9};
|
|
|
-
|
|
|
+ /*冒泡排序*/
|
|
|
+ int[] arr = {9, 8, 7, 6, 5, 4, 3, 2, 1};
|
|
|
for (int i = 0; i < arr.length; i++) {
|
|
|
- for (int j =0; j < arr.length - i-1; j++) {
|
|
|
- if (arr[j] > arr[j+1]) {
|
|
|
+ for (int j = 0; j < arr.length - i - 1; j++) {
|
|
|
+ if (arr[j] > arr[j + 1]) {
|
|
|
int temp;
|
|
|
temp = arr[j];
|
|
|
- arr[j]=arr[j+1];
|
|
|
- arr[j+1]=temp;
|
|
|
+ arr[j] = arr[j + 1];
|
|
|
+ arr[j + 1] = temp;
|
|
|
}
|
|
|
}
|
|
|
- System.out.print(arr[i]+",");
|
|
|
- System.out.println(Arrays.toString(arr));
|
|
|
}
|
|
|
- System.out.println(Arrays.toString(arr));
|
|
|
+ System.out.println("这是冒泡排序:" + Arrays.toString(arr));
|
|
|
+
|
|
|
|
|
|
+ /*插入排序*/
|
|
|
+ int[] ist = {8, 9, 7, 6, 5, 4, 3, 2, 1};
|
|
|
+ for (int i = 1; i < ist.length; i++) {
|
|
|
+ for (int j = 0; j < i; j++) {
|
|
|
+ int left = ist[i];
|
|
|
+ if (ist[i] <= ist[j]) {
|
|
|
+ for (int k = i; k > j; k--) {
|
|
|
+ ist[k] = ist[k - 1];
|
|
|
+ }
|
|
|
+ ist[j] = left;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ System.out.println("这是插入排序:" + Arrays.toString(ist));
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
}
|