Oppgave 4 løsning
unknown
java
5 days ago
1.1 kB
4
Indexable
Never
public static void delsortering(int[] a) { int oddetall = 0; for (int i = 0; i < a.length; i++) { if (a[i] % 2 != 0) { int temp = a[oddetall]; a[oddetall] = a[i]; a[i] = temp; oddetall++; } } quicksort(a, 0, oddetall-1); quicksort(a, oddetall, a.length-1); } private static void quicksort(int[] a, int low, int high) { if (low < high) { int pivotIndex = partition(a, low, high); quicksort(a, low, pivotIndex - 1); quicksort(a, pivotIndex + 1, high); } } private static int partition(int[] a, int low, int high) { int pivot = a[high]; int i = low - 1; for (int j = low; j < high; j++) { if (a[j] < pivot) { i++; int temp = a[i]; a[i] = a[j]; a[j] = temp; } } int temp = a[i + 1]; a[i + 1] = a[high]; a[high] = temp; return i + 1; }
Leave a Comment