Algorithm notes 2 (the Dutch flag, fast random row)

Given an array ARR, and a number num, num please less in number the number of
the left group, the number is greater than on the right of num array.
Requires additional space complexity O (1), the time complexity of O (N)

 


Second problem (problem Dutch flag)
Given an array ARR, and a number num, num Please count is less than the array on
the left, equal to the number in the middle num array, num greater than the number of the array on
the right side.
Requires additional space complexity O (1), the time complexity of O (N)

 

 1 public class Code_08_NetherlandsFlag {
 2 
 3     public static int[] partition(int[] arr, int l, int r, int p) {
 4         int less = l - 1;
 5         int more = r + 1; 
 6         // int index  = L; //用index 遍历
 7         while (l < more) {
 8             if (arr[l] < p) {
 9                 swap(arr, ++less, l++);
10             } else if (arr[l] > p) {
11                 swap(arr, --more, l);
12             } else {
13                 l++;
14             }
15         }
16         return new int[] { less + 1, more - 1 };
17     }
18 
19     // for test
20     public static void swap(int[] arr, int i, int j) {
21         int tmp = arr[i];
22         arr[i] = arr[j];
23         arr[j] = tmp;
24     }
25 
26     // for test
27     public static int[] generateArray() {
28         int[] arr = new int[10];
29         for (int i = 0; i < arr.length; i++) {
30             arr[i] = (int) (Math.random() * 3);
31         }
32         return arr;
33     }
34 
35     // for test
36     public static void printArray(int[] arr) {
37         if (arr == null) {
38             return;
39         }
40         for (int i = 0; i < arr.length; i++) {
41             System.out.print(arr[i] + " ");
42         }
43         System.out.println();
44     }
45 
46     public static void main(String[] args) {
47         int[] test = generateArray();
48 
49         printArray(test);
50         int[] res = partition(test, 0, test.length - 1, 1);
51         printArray(test);
52         System.out.println(res[0]);
53         System.out.println(res[1]);
54 
55     }
56 }

 

Achieve fast random row

  1 package basic_class_01;
  2 
  3 import java.util.Arrays;
  4 
  5 public class Code_04_QuickSort {
  6 
  7     public static void quickSort(int[] arr) {
  8         if (arr == null || arr.length < 2) {
  9             return;
 10         }
 11         quickSort(arr, 0, arr.length - 1);
 12     }
 13 
 14     public static void quickSort(int[] arr, int l, int r) {
 15         if (l < r) { // 条件
 16             swap(arr, l + (int) (Math.random() * (r - l + 1)), r);
 17             int[] p = partition(arr, l, r);
 18             quickSort(arr, l, p[0] - 1);
 19             quickSort(arr, p[1] + 1, r);
 20         }
 21     }
 22 
 23     public static int[] partition(int[] arr, int l, int r) {
 24         int less = l - 1;
 25         int more = r;
 26         while (l < more) {
 27             if (arr[l] < arr[r]) {
 28                 swap(arr, ++less, l++);
 29             } else if (arr[l] > arr[r]) {
 30                 swap(arr, --more, l);
 31             } else {
 32                 l++;
 33             }
 34         }
 35         swap(arr, more, r);
 36         return new int[] { less + 1, more };
 37     }
 38 
 39     public static void swap(int[] arr, int i, int j) {
 40         int tmp = arr[i];
 41         arr[i] = arr[j];
 42         arr[j] = tmp;
 43     }
 44 
 45     // for test
 46     public static void comparator(int[] arr) {
 47         Arrays.sort(arr);
 48     }
 49 
 50     // for test
 51     public static int[] generateRandomArray(int maxSize, int maxValue) {
 52         int[] arr = new int[(int) ((maxSize + 1) * Math.random())];
 53         for (int i = 0; i < arr.length; i++) {
 54             arr[i] = (int) ((maxValue + 1) * Math.random()) - (int) (maxValue * Math.random());
 55         }
 56         return arr;
 57     }
 58 
 59     // for test
 60     public static int[] copyArray(int[] arr) {
 61         if (arr == null) {
 62             return null;
 63         }
 64         int[] res = new int[arr.length];
 65         for (int i = 0; i < arr.length; i++) {
 66             res[i] = arr[i];
 67         }
 68         return res;
 69     }
 70 
 71     // for test
 72     public static boolean isEqual(int[] arr1, int[] arr2) {
 73         if ((arr1 == null && arr2 != null) || (arr1 != null && arr2 == null)) {
 74             return false;
 75         }
 76         if (arr1 == null && arr2 == null) {
 77             return true;
 78         }
 79         if (arr1.length != arr2.length) {
 80             return false;
 81         }
 82         for (int i = 0; i < arr1.length; i++) {
 83             if (arr1[i] != arr2[i]) {
 84                 return false;
 85             }
 86         }
 87         return true;
 88     }
 89 
 90     // for test
 91     public static void printArray(int[] arr) {
 92         if (arr == null) {
 93             return;
 94         }
 95         for (int i = 0; i < arr.length; i++) {
 96             System.out.print(arr[i] + " ");
 97         }
 98         System.out.println();
 99     }
100 
101     // for test
102     public static void main(String[] args) {
103         int testTime = 500000;
104         int maxSize = 100;
105         int maxValue = 100;
106         boolean succeed = true;
107         for (int i = 0; i < testTime; i++) {
108             int[] arr1 = generateRandomArray(maxSize, maxValue);
109             int[] arr2 = copyArray(arr1);
110             quickSort(arr1);
111             comparator(arr2);
112             if (!isEqual(arr1, arr2)) {
113                 succeed = false;
114                 printArray(arr1);
115                 printArray(arr2);
116                 break;
117             }
118         }
119         System.out.println(succeed ? "Nice!" : "Fucking fucked!");
120 
121         int[] arr = generateRandomArray(maxSize, maxValue);
122         printArray(arr);
123         quickSort(arr);
124         printArray(arr);
125 
126     }
127 
128 }

 

Examples of non-recursive change Recursive

 

 

Full binary tree complete binary tree

 

Guess you like

Origin www.cnblogs.com/yaozhenhua/p/11224149.html