0

此代码正确排序。这是插入排序吗?

import java.util.Scanner;
public class InsertionSort {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the number of elements: ");
        int count;
        count = sc.nextInt();
        int[] a = new int[count];
        System.out.println("Enter elements: ");
        for(int i = 0 ; i<count;i++){
            a[i] = sc.nextInt();
        }
        int j,temp;
        System.out.println("aftr insertion sort :");
        for(int i = 1 ; i<count;i++){
            j=i;
            while(j>0 && a[j-1] > a[j] ){
                temp = a[j];
                a[j] = a[j-1];
                a[j-1] = temp;
                j--;
            }
        }
        for(int i = 0 ; i<count;i++){
            System.out.print(a[i]+"  ");
        }
    }
}
4

1 回答 1

2

我专注于三个for循环中的第二个,即实际排序发生的那个。那个循环对我来说看起来不错。

于 2012-09-08T20:09:47.847 回答