Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.bill.androidredblacktree;
- import android.content.Intent;
- import android.net.Uri;
- import android.os.Debug;
- import android.support.v4.content.FileProvider;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.support.v7.widget.Toolbar;
- import android.util.Log;
- import android.view.View;
- import java.util.Arrays;
- import java.util.Map;
- import java.util.Random;
- import java.util.concurrent.ThreadLocalRandom;
- public class QuicksortBenchmarks extends AppCompatActivity {
- public native void QuicksortCPassArray(int a[]);
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_quicksort_benchmarks);
- int n1[] = {2000,20000};
- int j1 = 0;
- for (j1 = 0; j1 < n1.length; j1++) {
- int counts=0;
- for (int k1=0;k1<3;k1++) {
- int i, n = n1[j1];
- long startTime, estimatedTime, estimatedTime1;
- int a[];
- a = new int[n];
- for (i = 0; i < n; i++) {
- a[i] = i;
- }
- int z, j;
- Random rnd = ThreadLocalRandom.current();
- for (j = n - 1; j > 0; j--) {
- z = rnd.nextInt(j + 1);
- swap(a, z, j);
- }
- int b[]= Arrays.copyOf(a,a.length);
- startTime = System.nanoTime();
- quicksort(a, 0, n - 1);
- estimatedTime = System.nanoTime() - startTime;
- System.out.print("Java " + estimatedTime + '\n');
- startTime = System.nanoTime();
- QuicksortCPassArray(b);
- estimatedTime1 = System.nanoTime() - startTime;
- System.out.print("C " + estimatedTime1 + '\n');
- }
- }
- }
- private static void quicksort(int a[], int x, int y) {
- int q;
- if (x < y) {
- q = partition(a, x, y);
- quicksort(a, x, q - 1);
- quicksort(a, q + 1, y);
- }
- }
- private static int partition(int a[], int x, int y) {
- int temp = a[y];
- int i = x - 1;
- int j;
- for (j = x; j <= y - 1; j++) {
- if (a[j] <= temp) {
- i++;
- swap(a, i, j);
- }
- }
- swap(a, i + 1, y);
- return (i + 1);
- }
- private static void swap(int a[], int i, int j) {
- int t = a[i];
- a[i] = a[j];
- a[j] = t;
- }
- private int[] shuffleArray(int a[]){
- int i;
- for (i = 0; i < a.length; i++) {
- a[i] = i;
- }
- int z;
- Random rnd = ThreadLocalRandom.current();
- for (int j = a.length - 1; j > 0; j--) {
- z = rnd.nextInt(j + 1);
- swap(a, z, j);
- }
- return a;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement