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]+" ");
}
}
}
我已经为您正确缩进代码,这使得它稍微容易阅读:)您可以使用此网站自动执行此操作:http://www.prettyprinter.de/ – Wolph
编写测试并测试它,我们不是编译器:) – Drakosha
如果它符合[算法的定义](http://en.wikipedia.org/wiki/Insertion_sort#Algorithm),那么是的... – Miguel