logoNEURAL COOK
SearchAll PostsTagsNotesAuthor
logoNEURAL COOK

A posts created with Next.js and Tailwind.css

Explore

  • All posts
  • Notes
  • Snippets
  • Tags
  • RSS feed

Connect

instagramtwitterfacebookyoutubelinkedinmailgithub

© 2026 VIKI. All rights reserved.

Built with
Contents
ArraysBoyer-Moore Majority Vote AlgorithmKadane’s AlgorithmTriple Reverse algorithm aka Reversal Algorithm
Cycle detectionDijkstraDisjoint Set UnionGraphTopological SortTraversal algorithmsTree Traversal
Binary HeapBinomial HeapHeap
Cycle DetectionLinked ListSorting
Finding the majority elementRotate array by K steps
Finding If Number is PrimeGCD of two numbersPerforming ExponentiationPrime NumbersRandomising Data
3. Max Consecutive OnesHoare'S Partition SchemeLomuto Partition Scheme
DequesPriority QueueQueue
The Binary Search Master GuideThe Rotated Sorted Array Master Guide
Bubble SortCounting SortInsertion SortMerge SortPigeon Hole SortQuick SortSelection Sort
Monotonic StackMonotonic StackStackReversing an ArrayStackTower of Hanoi
Binary Tree TraversalCentre of gravityDiameter of TreeShortest Path
Algorithmic Pattern/arrays

Arrays

5 min read
On this pageOne-Dimensional Arrays <a href="#head-2-75" id="head-2-75"></a>
One-Dimensional Arrays <a href="#head-2-75" id="head-2-75"></a>Finding ItemsNonzero Lower Bounds <a href="#head-2-76" id="head-2-76"></a>Higher dimensional array in to a sigle dimension <a href="#head-2-77" id="head-2-77"></a>Triangular Arrays <a href="#head-2-77" id="head-2-77"></a>Sparse Arrays <a href="#head-2-78" id="head-2-78"></a>

An array is a chunk of contiguous memory that a program can access by using indices—one index per dimension in the array.

Info

Behind the scenes, the program allocates enough contiguous memory to hold the array's data.

  • For one-dimensional arrays, the mapping from array indices to memory entries is simple: index i maps to entry i.
  • For two-dimensional arrays, the program can map the array entries in one of two ways:
    • In row-major order, the program maps the first row of array entries to the first set of memory locations. It then maps the second row to the set of memory locations after the first and so on..
    • In column-major order, the program maps the first column of array entries to the first set of memory locations. It then maps the second column to the second set of memory locations, and so forth.

Normally, how a program maps array entries to memory locations is irrelevant to how a program works,

# One-Dimensional Arrays

# Finding Items

If the items in the array are not sorted, finding an item is a matter of performing a linear search or exhaustive search.

The linear search run-time algorithm's run time O(n)\text{O}(n)O(n)

Finding every item would take a total of

1+2+3...+n=n(n+1)21+2+3 ... +n = \frac{n(n+1)}{2}1+2+3...+n=2n(n+1)​

steps. Divide this by the number of searches, nnn , to get the average number of steps to find an item in the array (n+1)2\frac{(n+1)}{2}2(n+1)​ which means that the average run time for finding an item in the array is O(n)\text{O}(n)O(n)

# Finding Minimum, Maximum, and Average

We cannot avoid looking at every item in the array to find these values, so they have run time O(n)\text{O}(n)O(n)

# Finding Median

In case of an unsorted array either

  1. sort the array and find the middle element

    Time complexity O(nlog⁡n)\text{O}(n\log{n})O(nlogn)

  2. use the quick select to find the mid - element

    Time complexity O(n)\text{O}(n)O(n)

# Find mode

In case of unsorted array

  1. sort the array and iterate to find the longest contiguous sequence

    Time complexity O(nlog⁡n)\text{O}(n\log{n})O(nlogn), Time complexity O(1)\text{O}(1)O(1)

  2. store the count of occurrence using count array or hash table

    Time complexity O(n)\text{O}(n)O(n) , Space complexity O(n)\text{O}(n)O(n)

# Inserting Items

Inserting an item at the end of a linear array is easy (time complexity O(1)\text{O}(1)O(1)) if the array has enough space and doesn't need extending.

For inserting anywhere

  1. Find the position to insert the array
  2. Move the items to create space

# Deleting Items

Removing the item with index k from an array means moving the items that come after position k one position closer to the beginning of the array.

Info

In some cases, it may be possible to flag an entry as unused instead of actually removing it.

This technique can be particularly useful in hash tables, where resizing the array and rebuilding the hash table would be time-consuming.

If flagging many entries as unused, the array could eventually fill up with unused entries. Then, to find an item, would need to examine a lot of empty positions.

At some point, may want to compress the array to remove the empty entries.

# Nonzero Lower Bounds

Programming languages require that all arrays use 0 for a lower bound in every dimension. But sometimes it's convenient to treat an array's dimension as if it had nonzero lower bounds.

# Higher dimensional array in to a sigle dimension

Can pack an NNN-dimensional into a one-dimensional array in row-major order.

If there are NNNrows and MMM columns, allocate an array with N∗MN*MN∗M entries.

For an item in a given row and columns to find its index

index = row  × &lt;row size&gt; + column
  • because there are &lt;row size&gt; items in each row, that means those rows account for r × &lt;row size&gt; items before this one.
  • if the item's column number is ccc, then there are ccc items before this item in its row numbered 0,1,2,3...c0,1,2,3 ...c0,1,2,3...c Those items take up ccc positions in the values array.

# Triangular Arrays

In a triangular array, the values on one side of the diagonal have some default value, such as 0, null, or blank.

  • In an upper-triangular array, the real values lie on or above the diagonal.
  • In a lower-triangular array, the nondefault values lie on or below the diagonal.

Info

Some applications can save space by using triangular arrays instead of normal rectangular arrays.

For example, a connectivity matrix represents the connections between points in some sort of network.

The array's entry connected[i, j] is set to true if there is a flight from airport i to airport j. If you assume that there is a flight from airport j to airport i whenever there is a flight from airport i to airport j, then connected[i, j] = connected[j, i]. In that case, there's no need to store both connected[i, j] and connected[j, i] because they are the same.

it's probably not worth making a 100∗100100*100100∗100 triangular array because you would save only 4,9604,9604,960 entries, which still isn't all that much memory, and working with the array would be harder than using a normal array. However, a 10000∗1000010000*1000010000∗10000 triangular array would save about 505050million entries, which begins to add up to real memory savings, so it may be worth making it into a triangular array.

To build a triangular array simply pack the array's values into a one-dimensional array, skipping the entries that should not be included.

To build a triangular array with NNN rows, allocate a one-dimensional array containing N+N22\frac{N+N^2}{2}2N+N2​ items.

To find the index for an entry with row rrr and column ccc

(r−1)2+(r−1)2+c\frac{(r-1)^2+(r-1)}{2}+c2(r−1)2+(r−1)​+c

# Sparse Arrays

A sparse array lets you save even more space than a triangular array by not representing the missing entries.

One way to implement a sparse array is to make a linked list of linked lists.

  • The first list holds information about rows.

  • Each item in that list points to another linked list holding information about the array's columns for that row.

  • To make it easier to determine when a value is missing from the array, the ArrayRow objects are stored in increasing order of RowNumber.

  • the ArrayEntry objects are stored in increasing order of ColumnNumber.

    \

Next→Boyer-Moore Majority Vote Algorithm

On this page

One-Dimensional Arrays <a href="#head-2-75" id="head-2-75"></a>Finding ItemsNonzero Lower Bounds <a href="#head-2-76" id="head-2-76"></a>Higher dimensional array in to a sigle dimension <a href="#head-2-77" id="head-2-77"></a>Triangular Arrays <a href="#head-2-77" id="head-2-77"></a>Sparse Arrays <a href="#head-2-78" id="head-2-78"></a>