Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package runningtimealgorithm;
- import java.util.Random;
- /**
- *
- * @author elmaleek
- */
- public class QuickSort {
- static void quickSort (int a[], int lo, int hi){
- // lo adalah index bawah, hi adalah index atas
- // dari bagian array yang akan di urutkan
- int i=lo, j=hi, h;
- int pivot=a[lo];
- // pembagian
- do{
- while (a[i]<pivot) i++;
- while (a[j]>pivot) j--;
- if (i<=j)
- {
- h=a[i]; a[i]=a[j]; a[j]=h;//tukar
- i++; j--;
- }
- } while (i<=j);
- // pengurutan
- if (lo<j) quickSort(a, lo, j);
- if (i<hi) quickSort(a, i, hi);
- }
- public static void main(String[] args) {
- Random rand = new Random();
- int n = rand.nextInt(100);
- n += 1;
- int[] arr = new int[100];
- System.out.println("Array Sebelum Quick Sort");
- for (int i = 0; i < arr.length; ++i) {
- arr[i] = rand.nextInt(100);
- System.out.println(arr[i]);
- }
- double start = System.nanoTime();
- quickSort(arr, 0, n-1);
- double end = System.nanoTime();
- System.out.println("");
- System.out.println("Array Setelah Quick Sort");
- for (int i = 0; i < arr.length; ++i) {
- System.out.println(arr[i]);
- }
- double elapsedTime = end - start;
- System.out.println("");
- double seconds = elapsedTime / 1000000000;
- System.out.println("Waktu dalam nanoseken: " +elapsedTime );
- System.out.println("Waktu dalam detik " +seconds );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement