1+ E
2+ 1525410641
3+ tags : Quick Sort , Quick Select , Array
4+
5+ 给一串无序数组 , 找到median (sort之后 位置在中间的数字 ).
6+
7+ #### Quick Select
8+ - 与quickSort不同在于 , 每次只要在一半list里面recurring , 所以把O (logn )的时间复杂度降到O (n )
9+ - quickSelect 可以找到 kth 最小的元素
10+ - 利用这个原理 , 找这个kth最小值 , 然后如果 == target index , 就找到了我们的median
11+ - quick select 的template要熟悉一下 , 一下子可能想得到 , 但写不出来
12+ - 主要步骤 : partition , dfs , only recur on one part of the array
13+
14+ ```
115/*
216Given a unsorted array with integers, find the median of it.
317
1832
1933
2034*/
35+
36+ /*
37+ Thoughts:
38+ Goal: Find the median, which is N/2-th.
39+ Enumerate a few examples and find median index = (nums.length - 1) / 2
40+
41+ Quick sort has the nature of putting all smaller items on left, and larger numbers on right.
42+ If that pivot point happends to hit the midian index, that's our target.
43+ We don't necessarily need to sort all items, but just need to locate the median index.
44+ */
45+ public class Solution {
46+ /**
47+ * @param nums: A list of integers
48+ * @return: An integer denotes the middle number of the array
49+ */
50+ public int median (int [] nums ) {
51+ if (nums == null || nums .length == 0 ) {
52+ return 0 ;
53+ }
54+ if (nums .length == 1 ) {
55+ return nums [0 ];
56+ }
57+ int n = nums .length ;
58+ return quickSelect (nums , 0 , n - 1 , (n - 1 )/ 2 );
59+ }
60+
61+ private int quickSelect (int [] nums , int start , int end , int target ) {
62+ int index = partition (nums , start , end );
63+ if (index == target ) {
64+ return nums [index ];
65+ } else if (index < target ) {
66+ return quickSelect (nums , index + 1 , end , target );
67+ } else {
68+ return quickSelect (nums , start , index - 1 , target );
69+ }
70+ }
71+
72+ private int partition (int [] nums , int low , int high ) {
73+ int pivot = high ; // end
74+ int pivotValue = nums [pivot ];
75+
76+ while (low < high ) {
77+ while (low < high && nums [low ] < pivotValue ) {
78+ low ++;
79+ }
80+ while (low < high && nums [high ] >= pivotValue ) {
81+ high --;
82+ }
83+ swap (nums , low , high );
84+ }
85+
86+ swap (nums , low , pivot );
87+
88+ return low ;
89+ }
90+
91+ private void swap (int [] nums , int x , int y ) {
92+ int temp = nums [x ];
93+ nums [x ] = nums [y ];
94+ nums [y ] = temp ;
95+ }
96+ }
97+
98+
2199/*
22100 Recap 12.09.2015.
23101 O(n) means just run through it. It's similar to Partition array: it tries to split the list into 2 parts, and find the pivot.
36114*/
37115public class Solution {
38116 /**
39- * @param nums: A list of integers.
40- * @return: An integer denotes the middle number of the array.
117+ * @param nums: A list of integers
118+ * @return: An integer denotes the middle number of the array
41119 */
42120 public int median (int [] nums ) {
43121 if (nums == null || nums .length == 0 ) {
44122 return 0 ;
45123 }
46- if (nums .length % 2 == 0 ) {
47- return helper (nums , 0 , nums .length - 1 , nums .length /2 - 1 );
48- } else {
49- return helper (nums , 0 , nums .length - 1 , nums .length /2 );
124+ if (nums .length == 1 ) {
125+ return nums [0 ];
50126 }
127+ int n = nums .length ;
128+ return quickSort (nums , 0 , n - 1 , (n - 1 )/ 2 );
51129 }
52130
53- public void swap (int [] nums , int x , int y ){
54- int temp = nums [x ];
55- nums [x ] = nums [y ];
56- nums [y ] = temp ;
57- }
58-
59- public int helper (int [] nums , int start , int end , int mid ) {
131+ private int quickSort (int [] nums , int start , int end , int target ) {
60132 int pivot = end ;
61- int num = nums [pivot ];
133+ int pivotValue = nums [pivot ];
62134 int low = start ;
63135 int high = end ;
64136 while (low < high ) {
65- while (low < high && nums [low ] < num ) {
137+ while (low < high && nums [low ] < pivotValue ) {
66138 low ++;
67139 }
68- while (low < high && nums [high ] >= num ) {
140+ while (low < high && nums [high ] >= pivotValue ) {
69141 high --;
70142 }
71143 swap (nums , low , high );
72144 }
145+
73146 swap (nums , low , pivot );
74- if (low == mid ) {
147+ // dfs
148+ if (low == target ) {
75149 return nums [low ];
76- } else if (low < mid ) {
77- return helper (nums , low + 1 , end , mid );
150+ } else if (low < target ) {
151+ return quickSort (nums , low + 1 , end , target );
78152 } else {
79- return helper (nums , start , low - 1 , mid );
153+ return quickSort (nums , start , low - 1 , target );
80154 }
81155 }
156+
157+ private void swap (int [] nums , int x , int y ) {
158+ int temp = nums [x ];
159+ nums [x ] = nums [y ];
160+ nums [y ] = temp ;
161+ }
82162}
83163
84164
85-
86-
87-
88-
89-
90-
91-
92-
93-
94-
95-
96-
97-
98-
99-
100-
101-
102-
103-
104-
105-
106-
107-
165+ ```
0 commit comments