Vorwort: Die ursprüngliche Idee, sich in Java zu verlieben, ist nie vergessen worden: „Wenn ich meine Lernergebnisse teile, ist es wichtig, eine gute Grundlage zu legen, egal wie tief die spätere Technologie ist.“
Werkzeugklassen-Swapper, diese Werkzeugklasse wird in späteren Algorithmen verwendet:
Kopieren Sie den Codecode wie folgt:
Paket com.meritit.sortord.util;
/**
* Ein Dienstprogramm zum Austauschen zweier Elemente des Arrays
*
* @Autor ysjian
* @Version 1.0
* @email [email protected]
* @QQ 646633781
* @Telefon 18192235667
* @csdnBlog http://blog.csdn.net/ysjian_pingcx
* @createTime 20.12.2013
* @copyRight Merit
*/
öffentlicher Klassen-Swapper {
privateSwapper() {
}
/**
* Tauschen Sie zwei Elemente des Arrays aus
*
* @param oneIndex
* ein Index
* @param anotherIndex
* ein anderer Index
* @param-Array
* das auszutauschende Array
* @Exception NullPointerException
* wenn das Array null ist
*/
public static <T erweitert Comparable<T>> void swap(int oneIndex,
int anotherIndex, T[] array) {
if (array == null) {
throw new NullPointerException("null value input");
}
checkIndexs(oneIndex, anotherIndex, array.length);
T temp = array[oneIndex];
array[oneIndex] = array[anotherIndex];
array[anotherIndex] = temp;
}
/**
* Tauschen Sie zwei Elemente des Arrays aus
*
* @param oneIndex
* ein Index
* @param anotherIndex
* ein anderer Index
* @param-Array
* das auszutauschende Array
* @Exception NullPointerException
* wenn das Array null ist
*/
public static void swap(int oneIndex, int anotherIndex, int[] array) {
if (array == null) {
throw new NullPointerException("null value input");
}
checkIndexs(oneIndex, anotherIndex, array.length);
int temp = array[oneIndex];
array[oneIndex] = array[anotherIndex];
array[anotherIndex] = temp;
}
/**
* Überprüfen Sie den Index, ob er in der Anordnung enthalten ist
*
* @param oneIndex
* ein Index
* @param anotherIndex
* ein anderer Index
* @param arrayLength
* die Länge des Arrays
* @Exception IllegalArgumentException
* wenn der Index außerhalb des Bereichs liegt
*/
private static void checkIndexs(int oneIndex, int anotherIndex,
int arrayLength) {
if (oneIndex < 0 || anotherIndex < 0 || oneIndex >= arrayLength
||. anotherIndex >= arrayLength) {
wirf eine neue IllegalArgumentException(
„illegalArguments für zwei Indizes [“ + oneIndex + ","
+ oneIndex + "]");
}
}
}
Direkte Einfügungssortierung, InsertionSortord:
Kopieren Sie den Codecode wie folgt:
Paket com.meritit.sortord.insertion;
/**
* Sortierreihenfolge beim Einfügen, zeitliche Komplexität ist O(n2)
*
* @Autor ysjian
* @Version 1.0
* @email [email protected]
* @QQ 646633781
* @Telefon 18192235667
* @csdnBlog http://blog.csdn.net/ysjian_pingcx
* @createTime 31.12.2013
* @copyRight Merit
* @seit 1.5
*/
öffentliche Klasse InsertionSortord {
private static final InsertionSortord INSTANCE = new InsertionSortord();
private InsertionSortord() {
}
/**
* Holen Sie sich die Instanz von InsertionSortord, nur eine Instanz
*
* @return die einzige Instanz
*/
öffentliches statisches InsertionSortord getInstance() {
return INSTANCE;
}
/**
* Sortieren Sie das Array von <code>int</code> mit der Einfügesortierreihenfolge
*
* @param-Array
* das Array von int
*/
public void doSort(int... array) {
if (array != null && array.length > 0) {
int length = array.length;
// Die Zirkulation beginnt bei 1, der Wert von Index 0 ist Referenz
for (int i = 1; i < length; i++) {
if (array[i] < array[i - 1]) {
// wenn der Wert am Index i kleiner ist als der Wert am Index i-1
int vacancy = i; // die freie Stelle als i aufzeichnen
// Setze einen Wachposten als Wert bei Index i
int sentry = array[i];
// Schlüsselzirkulation, vom Index i-1,
for (int j = i - 1; j >= 0; j--) {
if (array[j] > sentry) {
/*
* wenn der aktuelle Indexwert den überschreitet
* Wachposten, dann rückwärts bewegen, den neuen Rekord einstellen
*Stelle als j
*/
array[j + 1] = array[j];
Leerstand = j;
}
}
// Den Wachposten auf die neue Stelle einstellen
array[vacancy] = sentry;
}
}
}
}
/**
* Sortieren Sie das Array von generischen <code>T</code> mit der Einfügesortierreihenfolge
*
* @param-Array
* das generische Array
*/
public <T erweitert Comparable<T>> void doSortT(T[] array) {
if (array != null && array.length > 0) {
int length = array.length;
for (int i = 1; i < length; i++) {
if (array[i].compareTo(array[i - 1]) < 0) {
T sentry = array[i];
int freie Stelle = i;
for (int j = i - 1; j >= 0; j--) {
if (array[j].compareTo(sentry) > 0) {
array[j + 1] = array[j];
Leerstand = j;
}
}
array[vacancy] = sentry;
}
}
}
}
}
Test TestInsertionSortord:
Kopieren Sie den Codecode wie folgt:
Paket com.meritit.sortord.insertion;
java.util.Arrays importieren;
/**
* Testen Sie die Sortierreihenfolge der Einfügungen
*
* @Autor ysjian
* @Version 1.0
* @email [email protected]
* @QQ 646633781
* @Telefon 18192235667
* @createTime 31.12.2013
* @copyRight Merit
*/
öffentliche Klasse TestInsertionSortord {
public static void main(String[] args) {
InsertionSortord insertSort = InsertionSortord.getInstance();
int[] array = { 3, 5, 4, 2, 6 };
System.out.println(Arrays.toString(array));
insertSort.doSort(array);
System.out.println(Arrays.toString(array));
System.out.println("---------------");
Integer[] array1 = { 3, 5, 4, 2, 6 };
System.out.println(Arrays.toString(array1));
insertSort.doSortT(array1);
System.out.println(Arrays.toString(array1));
}
}