<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>quicksort Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/quicksort/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/quicksort/</link>
	<description></description>
	<lastBuildDate>Fri, 07 Feb 2020 05:10:52 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.0.8</generator>

<image>
	<url>https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/cropped-photo-32x32.jpg</url>
	<title>quicksort Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/quicksort/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 912. Sort an Array</title>
		<link>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-912-sort-an-array/</link>
					<comments>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-912-sort-an-array/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sat, 01 Feb 2020 17:49:53 +0000</pubDate>
				<category><![CDATA[Array]]></category>
		<category><![CDATA[heapsort]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[O(n)]]></category>
		<category><![CDATA[O(nlogn)]]></category>
		<category><![CDATA[quicksort]]></category>
		<category><![CDATA[sorting]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=6226</guid>

					<description><![CDATA[<p>Given an array of integers&#160;nums, sort the array in ascending order. Example 1: Input: nums = [5,2,3,1] Output: [1,2,3,5] Example 2: Input: nums = [5,1,1,2,0,0]&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/array/leetcode-912-sort-an-array/">花花酱 LeetCode 912. Sort an Array</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>Given an array of integers&nbsp;<code>nums</code>, sort the array in ascending order.</p>



<p><strong>Example 1:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [5,2,3,1]
<strong>Output:</strong> [1,2,3,5]
</pre>



<p><strong>Example 2:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [5,1,1,2,0,0]
<strong>Output:</strong> [0,0,1,1,2,5]
</pre>



<p><strong>Constraints:</strong></p>



<ul><li><code>1 &lt;= nums.length &lt;= 50000</code></li><li><code>-50000 &lt;= nums[i] &lt;= 50000</code></li></ul>



<p>Since n &lt;= 50000, any O(n^2) won&#8217;t pass, we need O(nlogn) or better</p>



<h2><strong>Solution 1: QuickSort</strong></h2>



<p>Time complexity: O(nlogn) ~ O(n^2)<br>Space complexity: O(logn) ~ O(n)</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua, 60 ms
class Solution {
public:
  vector&lt;int&gt; sortArray(vector&lt;int&gt;&amp; nums) {
    function&lt;void(int, int)&gt; quickSort = [&amp;](int l, int r) {
      if (l &gt;= r) return;      
      int i = l;
      int j = r;
      int p = nums[l + rand() % (r - l + 1)];
      while (i &lt;= j) {
        while (nums[i] &lt; p) ++i;
        while (nums[j] &gt; p) --j;
        if (i &lt;= j)
          swap(nums[i++], nums[j--]);
      }
      quickSort(l, j);
      quickSort(i, r);
    };
    quickSort(0, nums.size() - 1);
    return nums;
  }
};</pre>
</div></div>



<h2><strong>Solution 2: Counting Sort</strong></h2>



<p>Time complexity: O(n)<br>Space complexity: O(max(nums) &#8211; min(nums))</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua, 54 ms
class Solution {
public:
  vector&lt;int&gt; sortArray(vector&lt;int&gt;&amp; nums) {
    auto [min_it, max_it] = minmax_element(begin(nums), end(nums));
    int l = *min_it, r = *max_it;
    vector&lt;int&gt; count(r - l + 1);
    for (int n : nums) ++count[n - l];
    int index = 0;
    for (int i = 0; i &lt; count.size(); ++i)
      while (count[i]--) nums[index++] = i + l;
    return nums;
  }
};</pre>
</div></div>



<h2><strong>Solution 2: HeapSort</strong></h2>



<p>Time complexity: O(nlogn)<br>Space complexity: O(n)</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua, 72ms
class Solution {
public:
  vector&lt;int&gt; sortArray(vector&lt;int&gt;&amp; nums) {
    priority_queue&lt;int&gt; q(begin(nums), end(nums));
    int i = nums.size();
    while (!q.empty()) {
      nums[--i] = q.top();
      q.pop();
    }
    return nums;
  }
};</pre>
</div></div>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  vector&lt;int&gt; sortArray(vector&lt;int&gt;&amp; nums) {
    auto heapify = [&amp;](int i, int e) {
      while (i &lt;= e) {
        int l = 2 * i + 1;
        int r = 2 * i + 2;
        int j = i;
        if (l &lt;= e &amp;&amp; nums[l] &gt; nums[j]) j = l;
        if (r &lt;= e &amp;&amp; nums[r] &gt; nums[j]) j = r;
        if (j == i) break;
        swap(nums[i], nums[j]);
        swap(i, j);
      }
    };
    
    const int n = nums.size();
    
    // Init heap
    for (int i = n / 2; i &gt;= 0; i--)
      heapify(i, n - 1);
    
    // Get min.
    for (int i = n - 1; i &gt;= 1; i--) {
      swap(nums[0], nums[i]);
      heapify(0, i - 1);    
    }
    
    return nums;
  }
};</pre>
</div></div>



<h2><strong>Solution 3: MergeSort</strong></h2>



<p>Time complexity: O(nlogn)<br>Space complexity: O(logn + n)</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  vector&lt;int&gt; sortArray(vector&lt;int&gt;&amp; nums) {
    vector&lt;int&gt; t(nums.size());
    function&lt;void(int, int)&gt; mergeSort = [&amp;](int l, int r) {
      if (l + 1 &gt;= r) return;
      int m = l + (r - l) / 2;
      mergeSort(l, m);
      mergeSort(m, r);
      int i1 = l;
      int i2 = m;
      int index = 0;
      while (i1 &lt; m || i2 &lt; r)
        if (i2 == r || (i1 &lt; m &amp;&amp; nums[i1] &lt; nums[i2]))
          t[index++] = nums[i1++];
        else
          t[index++] = nums[i2++];      
      std::copy(begin(t), begin(t) + index, begin(nums) + l);
    };
    
    mergeSort(0, nums.size());
    return nums;
  }
};</pre>
</div></div>



<h2><strong>Solution 4: BST</strong></h2>



<p>Time complexity: (nlogn)<br>Space complexity: O(n)</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  vector&lt;int&gt; sortArray(vector&lt;int&gt;&amp; nums) {
    multiset&lt;int&gt; s(begin(nums), end(nums));
    return {begin(s), end(s)};
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/array/leetcode-912-sort-an-array/">花花酱 LeetCode 912. Sort an Array</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-912-sort-an-array/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
