Advertisement
Guest User

quicksort

a guest
Apr 21st, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.84 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package runningtimealgorithm;
  7.  
  8. import java.util.Random;
  9.  
  10. /**
  11. *
  12. * @author elmaleek
  13. */
  14. public class QuickSort {
  15.  
  16. static void quickSort (int a[], int lo, int hi){
  17. // lo adalah index bawah, hi adalah index atas
  18. // dari bagian array yang akan di urutkan
  19. int i=lo, j=hi, h;
  20. int pivot=a[lo];
  21.  
  22. // pembagian
  23. do{
  24. while (a[i]<pivot) i++;
  25. while (a[j]>pivot) j--;
  26. if (i<=j)
  27. {
  28. h=a[i]; a[i]=a[j]; a[j]=h;//tukar
  29. i++; j--;
  30. }
  31. } while (i<=j);
  32.  
  33. // pengurutan
  34. if (lo<j) quickSort(a, lo, j);
  35. if (i<hi) quickSort(a, i, hi);
  36. }
  37.  
  38. public static void main(String[] args) {
  39. Random rand = new Random();
  40. int n = rand.nextInt(100);
  41. n += 1;
  42. int[] arr = new int[100];
  43. System.out.println("Array Sebelum Quick Sort");
  44. for (int i = 0; i < arr.length; ++i) {
  45. arr[i] = rand.nextInt(100);
  46. System.out.println(arr[i]);
  47. }
  48. double start = System.nanoTime();
  49. quickSort(arr, 0, n-1);
  50. double end = System.nanoTime();
  51.  
  52. System.out.println("");
  53. System.out.println("Array Setelah Quick Sort");
  54. for (int i = 0; i < arr.length; ++i) {
  55. System.out.println(arr[i]);
  56. }
  57.  
  58. double elapsedTime = end - start;
  59.  
  60. System.out.println("");
  61. double seconds = elapsedTime / 1000000000;
  62. System.out.println("Waktu dalam nanoseken: " +elapsedTime );
  63. System.out.println("Waktu dalam detik " +seconds );
  64.  
  65. }
  66.  
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement