<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>data-structures on seth-shi</title><link>https://sethix.com/tags/data-structures/</link><description>Recent content in data-structures on seth-shi</description><generator>Hugo -- gohugo.io</generator><language>en</language><lastBuildDate>Fri, 29 Jan 2021 15:24:18 +0800</lastBuildDate><atom:link href="https://sethix.com/tags/data-structures/index.xml" rel="self" type="application/rss+xml"/><item><title>I Have a Technique Called Quick Sort</title><link>https://sethix.com/posts/2021012915/</link><pubDate>Fri, 29 Jan 2021 15:24:18 +0800</pubDate><guid>https://sethix.com/posts/2021012915/</guid><description>Recently while solving a problem on LeetCode (finding the kth largest number), I needed to sort an array. Realized I only knew basic sorting algorithms like Bubble Sort and Insertion Sort. Then I reviewed Quick Sort through animated examples online, and implemented my own version (code is similar to common implementations): // 1. Select a pivot element (can choose the first element or random element) // 2. Use two pointers starting from both ends, right pointer moves first // 3.</description></item><item><title>Sorting Algorithm - Bucket Sort</title><link>https://sethix.com/posts/2020060709/</link><pubDate>Sun, 07 Jun 2020 09:28:23 +0800</pubDate><guid>https://sethix.com/posts/2020060709/</guid><description>Bucket Sort is well-suited for sorting numbers in the range of 0~100, but its efficiency decreases when dealing with data that has a wide value range.
Algorithm Steps Create n empty buckets based on requirements Traverse the input array to find the maximum and minimum values For each element num, calculate bucket index using formula: floor((num - min) / n) If a bucket already contains elements, maintain elements in sorted order (using linked list) Repeat steps 3-4 until all elements are distributed Concatenate all buckets&amp;rsquo; linked lists and output the sorted array package main import ( &amp;#34;fmt&amp;#34; &amp;#34;math&amp;#34; ) func main() { data := []int{111, 9, 2, 4, 9, 3, 3, 5, 7, 1, 8, 2, 11, 22, 99, 192} fmt.</description></item><item><title>Dijkstra Algorithm</title><link>https://sethix.com/posts/2019031312/</link><pubDate>Wed, 13 Mar 2019 12:52:43 +0800</pubDate><guid>https://sethix.com/posts/2019031312/</guid><description>Suppose we have the following graph:
// 3 // e+-----------------+ // ^ | // 2| | // | | // 1 + 1 3 | // a +------&amp;gt; b +---&amp;gt; d +---+ | // + + | | // | | v | // 2 | |4 g &amp;lt;--+ // | | ^ // v v 1 | // c f +---+ // + ^ // | | // | 2 | // +------------------+ To find the shortest path from node a to node g, we can use the Dijkstra algorithm.</description></item><item><title>DFS (Depth-First Search) and BFS (Breadth-First Search)</title><link>https://sethix.com/posts/2019030617/</link><pubDate>Wed, 06 Mar 2019 17:05:49 +0800</pubDate><guid>https://sethix.com/posts/2019030617/</guid><description>BFS stands for Breadth-First-Search
DFS stands for Depth-first search
A classic LeetCode problem &amp;ldquo;Number of Islands&amp;rdquo;:
Given a 2D grid map of 1s (land) and 0s (water), count the number of islands. An island is surrounded by water and formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are surrounded by water.
Input:
11000
11000
00100
00011
Output: 3
Although this problem is categorized under BFS, implementing it with DFS may be more intuitive.</description></item><item><title>Reverse Linked List - Recursive Approach</title><link>https://sethix.com/posts/2019021815/</link><pubDate>Mon, 18 Feb 2019 15:43:12 +0800</pubDate><guid>https://sethix.com/posts/2019021815/</guid><description>Recently encountered a linked list reversal problem on LeetCode:
Reverse a singly linked list. Example: Input: 1-&amp;gt;2-&amp;gt;3-&amp;gt;4-&amp;gt;5-&amp;gt;NULL Output: 5-&amp;gt;4-&amp;gt;3-&amp;gt;2-&amp;gt;1-&amp;gt;NULL Iterative Solution /** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * }; */ struct ListNode* reverseList(struct ListNode* head) { struct ListNode *tmpHead = head, *nextNode; if (NULL == head) { return head; } while (NULL != head-&amp;gt;next) { // Move the next node of current &amp;#39;head&amp;#39; to real head position nextNode = head-&amp;gt;next; head-&amp;gt;next = head-&amp;gt;next-&amp;gt;next; nextNode-&amp;gt;next = tmpHead; tmpHead = nextNode; } return tmpHead; } Recursive Solution /** * Definition for singly-linked list.</description></item><item><title>Implementation of Linked List in PHP</title><link>https://sethix.com/posts/2018042410/</link><pubDate>Tue, 24 Apr 2018 10:08:57 +0800</pubDate><guid>https://sethix.com/posts/2018042410/</guid><description>Starting to learn data structures
Today I changed the code font I use. Previously I used console which looked good, but today I found a more preferable style Source Code Pro.
Here are two screenshots - it&amp;rsquo;s quite pleasing!
Source Code Pro Source Code Pro Now let&amp;rsquo;s get to the main topic: linked list operations
Node Class First we need a Node class to store data &amp;lt;?php namespace LinkedList; class Node { /** * @var $data integer */ public $data; /** * Pointer to next node * * @var $next Node */ public $next; public function __construct(int $data = null) { // Initialize data assignment, can also be assigned via $node-&amp;gt;data = X; $this-&amp;gt;data = $data; } } Linked List Manager Class (For node operations) We&amp;rsquo;ll break down the manager class code into sections due to its length Head Insertion As the name suggests, insert at the head If list is empty, initialize with current node If not empty, make new node the head public function insertHead(int $data) : bool { $newNode = new Node($data); $newNode-&amp;gt;next = $this-&amp;gt;head; $this-&amp;gt;head = $newNode; return true; } Insert at Position (index=0 is head, returns false for invalid positions) public function insert(int $index = 0, int $data) : bool { if (is_null($this-&amp;gt;head) || $index === 0) { return $this-&amp;gt;insertHead($data); } $currNode = $this-&amp;gt;head; $startIndex = 1; for ($currIndex = $startIndex; !</description></item></channel></rss>