public void sort(int[] data) {
if (data != null) {
quickSort(data, data.length);
}
}
private void quickSort(int[] data, int count) {
if (count <= 1) {
return;
}
int[] largeData = new int[count];
int[] smallData = new int[count];
int largeCount = 0;
int smallCount = 0;
int pivot = data[0];
for (int index = 1; index < count; index ++) {
if (data[index] > pivot) {
largeData[largeCount] = data[index];
largeCount ++;
}
else {
smallData[smallCount] = data[index];
smallCount ++;
}
}
quickSort(largeData, largeCount);
quickSort(smallData, smallCount);
int sortedIndex = 0;
for (int smallIndex = 0; smallIndex < smallCount; smallIndex ++) {
data[sortedIndex] = smallData[smallIndex];
sortedIndex ++;
}
data[sortedIndex] = pivot;
sortedIndex ++;
for (int largeIndex = 0; largeIndex < largeCount; largeIndex ++) {
data[sortedIndex] = largeData[largeIndex];
sortedIndex ++;
}
}
ICAgIHB1YmxpYyB2b2lkIHNvcnQoaW50W10gZGF0YSkgewoJCWlmIChkYXRhICE9IG51bGwpIHsKCQkJcXVpY2tTb3J0KGRhdGEsIGRhdGEubGVuZ3RoKTsKCQl9Cgl9Cglwcml2YXRlIHZvaWQgcXVpY2tTb3J0KGludFtdIGRhdGEsIGludCBjb3VudCkgewoJCWlmIChjb3VudCA8PSAxKSB7CgkJCXJldHVybjsKCQl9CgkJaW50W10gbGFyZ2VEYXRhID0gbmV3IGludFtjb3VudF07CgkJaW50W10gc21hbGxEYXRhID0gbmV3IGludFtjb3VudF07CgkJaW50IGxhcmdlQ291bnQgPSAwOwoJCWludCBzbWFsbENvdW50ID0gMDsKCQlpbnQgcGl2b3QgPSBkYXRhWzBdOwoJCWZvciAoaW50IGluZGV4ID0gMTsgaW5kZXggPCBjb3VudDsgaW5kZXggKyspIHsKCQkJaWYgKGRhdGFbaW5kZXhdID4gcGl2b3QpIHsKCQkJCWxhcmdlRGF0YVtsYXJnZUNvdW50XSA9IGRhdGFbaW5kZXhdOwoJCQkJbGFyZ2VDb3VudCArKzsKCQkJfQoJCQllbHNlIHsKCQkJCXNtYWxsRGF0YVtzbWFsbENvd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