Created
May 6, 2011 09:58
-
-
Save sdarlington/958713 to your computer and use it in GitHub Desktop.
Quicksort implementation for Sybase Aleri (SPLASH)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Pretty literal implementation of the quick sort documented on Wikipedia | |
// http://en.wikipedia.org/wiki/Quicksort | |
// Datatypes can trivially be changed to others | |
int32 quicksort_double (vector(double) data) { | |
quicksort_internal_double (data, 0, size(data) - 1); | |
} | |
int32 quicksort_internal_double (vector(double) data, int32 vleft, int32 vright) { | |
if (vright > vleft) { | |
int32 pivotIndex := (vleft + vright) / 2; | |
int32 pivotNewIndex := partition_double (data, vleft, vright, pivotIndex); | |
quicksort_internal_double (data, vleft, pivotNewIndex - 1); | |
quicksort_internal_double (data, pivotNewIndex + 1, vright); | |
} | |
} | |
int32 partition_double (vector(double) data, int32 vleft, int32 vright, int32 pivotIndex) { | |
double pivotValue := data[pivotIndex]; | |
swap_double(data, pivotIndex, vright); | |
int32 storeIndex := vleft; | |
int32 i := vleft; | |
while (i < vright) { | |
if (data[i] <= pivotValue) { | |
swap_double (data, i, storeIndex); | |
storeIndex++; | |
} | |
i++; | |
} | |
swap_double (data, storeIndex, vright); | |
return storeIndex; | |
} | |
int32 swap_double (vector(double) data, int32 el1, int32 el2) { | |
double temp := data[el1]; | |
data[el1] := data[el2]; | |
data[el2] := temp; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment