Advertisement
nguyenvanquan7826

Untitled

Apr 11th, 2014
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.42 KB | None | 0 0
  1. /* * To change this template, choose Tools | Templates * and open the template in the editor. */
  2. package nguyenvanquan7826.temp;
  3.  
  4. /** * * @author john */
  5. public class Temp {
  6. public Temp() {
  7. int i;
  8. int array[] = { 1, 3, 3, 3, 6, 7, 2, 4 };
  9. // int array[] = { 1, 3, 5, 8, 6, 7, 2, 4 };
  10. System.out.println(" Quick Sort\n");
  11. System.out.println("Values Before the sort:\n");
  12. for (i = 0; i < array.length; i++) {
  13. System.out.print(array[i] + " ");
  14. }
  15. quickSort(array, 0, array.length - 1);
  16. System.out.print("\nValues after the sort:\n");
  17. for (i = 0; i < array.length; i++) {
  18. System.out.print(array[i] + " ");
  19. System.out.print("");
  20. System.out.print(" ; ");
  21. }
  22. }
  23.  
  24. void quickSort(int a[], int l, int r) {
  25. int key = a[(l + r) / 2]; // lay khoa la gia tri ngau nhien
  26. // tu a[l] -> a[r]
  27. // int key = a[(l+r)/2];
  28. int i = l, j = r;
  29.  
  30. while (i <= j) {
  31. while (a[i] < key)
  32. i++; // tim phan tu ben trai ma >=key
  33. while (a[j] > key)
  34. j--; // tim phan tu ben trai ma <=key
  35. if (i <= j) {
  36. if (i < j) {
  37. int temp = a[i];
  38. a[i] = a[j];
  39. a[j] = temp;
  40. }
  41. i++;
  42. j--;
  43. }
  44. }
  45. // bay gio ta co 1 mang : a[l]....a[j]..a[i]...a[r]
  46. if (l < j)
  47. quickSort(a, l, j); // lam lai voi mang a[l]....a[j]
  48. if (i < r)
  49. quickSort(a, i, r); // lam lai voi mang a[i]....a[r]
  50. }
  51.  
  52. public static void main(String[] args) {
  53. new Temp();
  54. }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement