Advertisement
Guest User

Untitled

a guest
Apr 8th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.34 KB | None | 0 0
  1. #include <jni.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <time.h>
  5.  
  6. void quicksort(jint *a, jint x, jint y);
  7.  
  8. jint partition(jint *a, jint x, jint y);
  9.  
  10. void swap(jint *a, jint *b);
  11.  
  12.  
  13. JNIEXPORT void JNICALL
  14. Java_com_example_bill_androidredblacktree_QuicksortBenchmarks_QuicksortCPassArray(
  15.         JNIEnv *env,
  16.         jobject this,
  17.         jintArray arr) {
  18.  
  19.     jint *c_array = (*env)->GetIntArrayElements(env, arr, 0);
  20.  
  21.     jint n = (*env)->GetArrayLength(env, arr);    
  22.  
  23.     quicksort(c_array, 0, n - 1);
  24.  
  25.     (*env)->ReleaseIntArrayElements(env, arr, c_array, 0);
  26.  
  27.  
  28. }
  29.  
  30.  
  31. void quicksort(jint *a, jint x, jint y) {
  32.     jint q;
  33.     if (x < y) {
  34.         q = partition(a, x, y);
  35.         quicksort(a, x, q - 1);
  36.         quicksort(a, q + 1, y);
  37.     }
  38. }
  39.  
  40.  
  41. jint partition(jint *a, jint x, jint y) {
  42.     jint temp = *(a + y);
  43.     jint i = x - 1;
  44.     jint j;
  45.     for (j = x; j <= y - 1; j++) {
  46.         if (*(a + j) <= temp) {
  47.             i++;
  48.             jint temp1 = *(a + i);
  49.             *(a + i) = *(a + j);
  50.             *(a + j) = temp1;
  51.             //swap(&a[i], &a[j]);
  52.         }
  53.     }
  54.     jint temp2 = *(a + i + 1);
  55.     *(a + i + 1) = *(a + y);
  56.     *(a + y) = temp2;
  57.     //swap(&a[i + 1], &a[y]);
  58.     return (i + 1);
  59. }
  60.  
  61.  
  62. void swap(jint *a, jint *b) {
  63.     jint temp = *a;
  64.     *a = *b;
  65.     *b = temp;
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement