hallo,
wieder mal ein problem mit einer aufgabe - pivot element (p) = letztes Element.
ich soll folgende liste: 9, 16, 17, 5, 3, 18, 14, 4, 14, 17
mit divide in folgende form bringen: 9, 16, 14, 5, 3, 4, 14, 17, 17, 18
ich komme aber immer auf: 9, 16, 3, 5, 4, 14, 14, 17, 17, 18
bitte hilfe!
wieder mal ein problem mit einer aufgabe - pivot element (p) = letztes Element.
ich soll folgende liste: 9, 16, 17, 5, 3, 18, 14, 4, 14, 17
mit divide in folgende form bringen: 9, 16, 14, 5, 3, 4, 14, 17, 17, 18
ich komme aber immer auf: 9, 16, 3, 5, 4, 14, 14, 17, 17, 18
bitte hilfe!
Java:
public static int divide(int[] list, int leftIdx, int rightIdx)
{
if (leftIdx < 0 || rightIdx < 0 || leftIdx > list.length - 1 || rightIdx > list.length - 1)
throw new IllegalArgumentException();
int p = list[rightIdx];
for (int i = 0; i < list.length - 1; i++)
if (list[i] >= p)
for (int j = list.length - 1; j > i; j--)
if (list[j] < list[i])
swap(list, i, j);
return p;
}
public static void swap(int[] list, int i, int j)
{
if (i < 0 || j < 0 || i > list.length - 1 || j > list.length - 1)
throw new IllegalArgumentException();
int temp = list[i];
list[i] = list[j];
list[j] = temp;
}