0
Ce code trie correctement. Est-ce un genre d'insertion?Mon code est-il une implémentation correcte du tri par insertion?
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]+" ");
}
}
}
J'ai mis en retrait le code correctement pour vous, ce qui le rend un peu plus facile à lire :) Vous pouvez utiliser ce site pour le faire automatiquement: http://www.prettyprinter.de/ – Wolph
Écrire un test et le tester, nous ne sommes pas des compilateurs :) – Drakosha
S'il est conforme à la [définition de l'algorithme] (http://en.wikipedia.org/wiki/Insertion_sort#Algorithm), alors oui ... – Miguel