Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <jni.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- void quicksort(jint *a, jint x, jint y);
- jint partition(jint *a, jint x, jint y);
- void swap(jint *a, jint *b);
- JNIEXPORT void JNICALL
- Java_com_example_bill_androidredblacktree_QuicksortBenchmarks_QuicksortCPassArray(
- JNIEnv *env,
- jobject this,
- jintArray arr) {
- jint *c_array = (*env)->GetIntArrayElements(env, arr, 0);
- jint n = (*env)->GetArrayLength(env, arr);
- quicksort(c_array, 0, n - 1);
- (*env)->ReleaseIntArrayElements(env, arr, c_array, 0);
- }
- void quicksort(jint *a, jint x, jint y) {
- jint q;
- if (x < y) {
- q = partition(a, x, y);
- quicksort(a, x, q - 1);
- quicksort(a, q + 1, y);
- }
- }
- jint partition(jint *a, jint x, jint y) {
- jint temp = *(a + y);
- jint i = x - 1;
- jint j;
- for (j = x; j <= y - 1; j++) {
- if (*(a + j) <= temp) {
- i++;
- jint temp1 = *(a + i);
- *(a + i) = *(a + j);
- *(a + j) = temp1;
- //swap(&a[i], &a[j]);
- }
- }
- jint temp2 = *(a + i + 1);
- *(a + i + 1) = *(a + y);
- *(a + y) = temp2;
- //swap(&a[i + 1], &a[y]);
- return (i + 1);
- }
- void swap(jint *a, jint *b) {
- jint temp = *a;
- *a = *b;
- *b = temp;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement