Insertion Sort (Generic)

import java.util.*; public class Sorts { public static <T extends Comparable<T>> ArrayList<T> insertionSort(ArrayList<T> list){ ArrayList<T> listaOrdenada; listaOrdenada = new ArrayList<T>(list); for(int x = 1 ; x < listaOrdenada.size(); x++){ T aux = listaOrdenada.get(x); int y = x - 1; while(y >= 0 && aux.compareTo(listaOrdenada.get(y)) < 0){ listaOrdenada.set(y + 1, listaOrdenada.get(y)); y--; } listaOrdenada.set(y + 1, aux); } return listaOrdenada; } }

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.