Advertisement
Guest User

Untitled

a guest
Apr 8th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.35 KB | None | 0 0
  1. package com.example.bill.androidredblacktree;
  2.  
  3. import android.content.Intent;
  4. import android.net.Uri;
  5. import android.os.Debug;
  6. import android.support.v4.content.FileProvider;
  7. import android.support.v7.app.AppCompatActivity;
  8. import android.os.Bundle;
  9. import android.support.v7.widget.Toolbar;
  10. import android.util.Log;
  11. import android.view.View;
  12.  
  13. import java.util.Arrays;
  14. import java.util.Map;
  15. import java.util.Random;
  16. import java.util.concurrent.ThreadLocalRandom;
  17.  
  18. public class QuicksortBenchmarks extends AppCompatActivity {
  19.  
  20.  
  21.     public native void QuicksortCPassArray(int a[]);
  22.  
  23.     @Override
  24.     protected void onCreate(Bundle savedInstanceState) {
  25.         super.onCreate(savedInstanceState);
  26.         setContentView(R.layout.activity_quicksort_benchmarks);
  27.  
  28.  
  29.                     int n1[] = {2000,20000};
  30.                     int j1 = 0;
  31.                     for (j1 = 0; j1 < n1.length; j1++) {
  32.                         int counts=0;
  33.                         for (int k1=0;k1<3;k1++) {
  34.                             int i, n = n1[j1];
  35.                             long startTime, estimatedTime, estimatedTime1;
  36.  
  37.                             int a[];
  38.                             a = new int[n];
  39.                             for (i = 0; i < n; i++) {
  40.                                 a[i] = i;
  41.                             }
  42.  
  43.                             int z, j;
  44.                             Random rnd = ThreadLocalRandom.current();
  45.                             for (j = n - 1; j > 0; j--) {
  46.                                 z = rnd.nextInt(j + 1);
  47.                                 swap(a, z, j);
  48.                             }
  49.  
  50.                             int b[]= Arrays.copyOf(a,a.length);
  51.  
  52.                             startTime = System.nanoTime();
  53.                             quicksort(a, 0, n - 1);
  54.                             estimatedTime = System.nanoTime() - startTime;
  55.                             System.out.print("Java " + estimatedTime + '\n');
  56.  
  57.                             startTime = System.nanoTime();
  58.                             QuicksortCPassArray(b);
  59.                             estimatedTime1 = System.nanoTime() - startTime;                          
  60.                             System.out.print("C " + estimatedTime1 + '\n');
  61.  
  62.                         }
  63.  
  64.                     }
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.     }
  73.  
  74.     private static void quicksort(int a[], int x, int y) {
  75.  
  76.         int q;
  77.         if (x < y) {
  78.             q = partition(a, x, y);
  79.             quicksort(a, x, q - 1);
  80.             quicksort(a, q + 1, y);
  81.         }
  82.     }
  83.  
  84.     private static int partition(int a[], int x, int y) {
  85.         int temp = a[y];
  86.         int i = x - 1;
  87.         int j;
  88.         for (j = x; j <= y - 1; j++) {
  89.             if (a[j] <= temp) {
  90.                 i++;
  91.                 swap(a, i, j);
  92.             }
  93.         }
  94.         swap(a, i + 1, y);
  95.         return (i + 1);
  96.     }
  97.  
  98.     private static void swap(int a[], int i, int j) {
  99.         int t = a[i];
  100.         a[i] = a[j];
  101.         a[j] = t;
  102.     }
  103.  
  104.     private int[] shuffleArray(int a[]){
  105.         int i;
  106.         for (i = 0; i < a.length; i++) {
  107.             a[i] = i;
  108.         }
  109.  
  110.         int z;
  111.         Random rnd = ThreadLocalRandom.current();
  112.         for (int j = a.length - 1; j > 0; j--) {
  113.             z = rnd.nextInt(j + 1);
  114.             swap(a, z, j);
  115.         }
  116.         return a;
  117.  
  118.     }
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement