> 웹 프론트엔드 > JS 튜토리얼 > JS를 사용한 DSA: JavaScript의 사용자 정의 배열 데이터 구조 이해 - 단계별 가이드

JS를 사용한 DSA: JavaScript의 사용자 정의 배열 데이터 구조 이해 - 단계별 가이드

Mary-Kate Olsen
풀어 주다: 2024-09-30 06:18:03
원래의
686명이 탐색했습니다.

DSA with JS: Understanding Custom Array Data Structure in JavaScript - A Step-by-Step Guide

pengenalan

Tatasusunan ialah struktur data asas dalam pengaturcaraan, penting untuk mengatur dan menyimpan data dengan cekap. Mereka membenarkan pembangun mengurus koleksi elemen, seperti nombor, rentetan atau objek, dengan mengumpulkannya ke dalam satu struktur tersusun. Tatasusunan menyediakan akses mudah kepada elemen melalui pengindeksan, menjadikannya berguna untuk pelbagai tugas seperti mengisih, mencari dan memanipulasi data.

Tatasusunan asli JavaScript adalah struktur data terbina dalam yang berkuasa dan fleksibel yang boleh berkembang atau mengecut secara dinamik mengikut keperluan. Tidak seperti tatasusunan dalam bahasa peringkat rendah, yang biasanya bersaiz tetap, tatasusunan JavaScript boleh mengendalikan jenis data yang berbeza dan melaraskan saiznya secara automatik. JavaScript menyediakan banyak kaedah terbina dalam, yang mengabstrak kerumitan pengurusan memori, saiz semula dan akses elemen. Kaedah ini memudahkan manipulasi tatasusunan, membolehkan pembangun menumpukan pada menyelesaikan masalah tanpa perlu risau tentang pelaksanaan asas. Tatasusunan JavaScript dioptimumkan oleh enjin moden seperti V8, menjadikannya berprestasi tinggi untuk kebanyakan kes penggunaan.

Walaupun JavaScript menyediakan pelaksanaan tatasusunan yang mudah dan sangat dioptimumkan, membina tatasusunan tersuai membantu anda memahami mekanik pengurusan memori, saiz semula dinamik dan akses data yang cekap. Dengan membina tatasusunan tersuai, pembangun bukan sahaja meningkatkan kemahiran menyelesaikan masalah mereka tetapi juga membangunkan pemahaman yang lebih mendalam tentang prinsip teras yang memacu kecekapan pengaturcaraan, menyediakan mereka untuk struktur data yang lebih maju dan cabaran algoritma.

Membina Tatasusunan Tersuai

Biar saya tunjukkan contoh cara seseorang menulis tatasusunan menggunakan kelas dalam JavaScript. Pendekatan ini adalah lebih rendah tahap, mensimulasikan tingkah laku tatasusunan secara manual. Untuk membina tatasusunan tersuai dalam JavaScript, anda boleh mencipta kelas yang meniru gelagat tatasusunan asli JavaScript. Kelas memerlukan pembina untuk memulakan tatasusunan dan kaedah untuk melaksanakan operasi asas seperti menambah, mengalih keluar dan mengubah saiz elemen. Berikut ialah struktur ringkas:

class CustomArray {
  constructor() {
    this.data = {};  // Object to hold array data
    this.length = 0; // Length of the array
  }

  // Method to add an element at the end
  push(element) {
    this.data[this.length] = element;
    this.length++;
    return this.length;
  }

  // Method to remove the last element
  pop() {
    if (this.length === 0) return undefined;
    const lastElement = this.data[this.length - 1];
    delete this.data[this.length - 1];
    this.length--;
    return lastElement;
  }

  // Method to get the element at a specific index
  get(index) {
    return this.data[index];
  }

  // Method to delete an element at a specific index
  delete(index) {
    const item = this.data[index];
    this.shiftItems(index);  // Shift items after deletion
    return item;
  }

  // Internal method to shift items after deletion
  shiftItems(index) {
    for (let i = index; i < this.length - 1; i++) {
      this.data[i] = this.data[i + 1];
    }
    delete this.data[this.length - 1];
    this.length--;
  }
}

// Example usage
const myArray = new CustomArray();
myArray.push(10);   // [10]
myArray.push(20);   // [10, 20]
myArray.push(30);   // [10, 20, 30]

console.log(myArray.get(1));  // Output: 20

myArray.delete(1);   // [10, 30]
console.log(myArray); // { data: { '0': 10, '1': 30 }, length: 2 }

myArray.pop();  // Remove last element [10]
console.log(myArray); // { data: { '0': 10 }, length: 1 }
로그인 후 복사

Penjelasan:

  1. Pembina (pembina): Memulakan data objek kosong dan menetapkan panjang awal kepada 0. Objek (data) ini akan bertindak seperti storan dalaman tatasusunan.

  2. Tekan (tekan()): Menambah elemen baharu pada tatasusunan dengan memberikannya kepada indeks yang tersedia seterusnya (dijejaki oleh this.length), kemudian menambah panjang.

  3. Pop (pop()): Mengalih keluar elemen terakhir daripada tatasusunan dengan memadamkan indeks terakhir dan mengurangkan panjang. Ini meniru gelagat kaedah Array.prototype.pop().

  4. Dapatkan (get()): Mengambil nilai pada indeks tertentu. Ia meniru mengakses elemen dalam tatasusunan mengikut indeks (cth., arr[1]).

  5. Padam (delete()): Memadamkan elemen pada indeks tertentu dan mengalihkan elemen yang lain ke kiri untuk mengisi jurang, sama seperti Array.prototype.splice () akan dilakukan dalam tatasusunan JavaScript asli.

  6. Shift Items (shiftItems()): Selepas memadamkan elemen, kaedah ini mengalihkan semua elemen selepas indeks yang dipadamkan satu kedudukan ke kiri, yang diperlukan untuk mengekalkan tingkah laku seperti tatasusunan .

Kerumitan Masa & Prestasi

Topik pengukuran prestasi berada di bawah tatatanda Big O. Jadi, jika anda rasa anda perlu mengkaji tentang Kerumitan Masa dan Prestasi, anda boleh membaca artikel ini untuk memahami konsepnya.

operasi push().

Kerumitan Masa: O(1) (Masa malar) Kaedah push() menambahkan elemen pada penghujung tatasusunan. Memandangkan ia hanya meletakkan nilai pada indeks panjang semasa, ia berfungsi dalam masa tetap, bermakna operasi tidak bergantung pada saiz tatasusunan.

Kerumitan Angkasa: O(1) (Ruang malar) Kerumitan ruang adalah malar kerana ia hanya menambah satu elemen baharu, tanpa mengira saiz tatasusunan.

push(value) {
  this.data[this.length] = value; // O(1)
  this.length++;
}
로그인 후 복사

operasi pop().

Kerumitan Masa: O(1) (Masa malar) Kaedah pop() mengalih keluar elemen terakhir, yang melibatkan mengakses indeks terakhir dan melaraskan panjang. Ini juga dilakukan dalam masa yang tetap.

Kerumitan Ruang: O(1) (Ruang malar) Tiada memori tambahan digunakan dan hanya elemen terakhir dikeluarkan.

pop() {
  const lastItem = this.data[this.length - 1]; // O(1)
  delete this.data[this.length - 1];
  this.length--;
  return lastItem;
}
로그인 후 복사

Resizing (In the case of dynamic resizing)

Time Complexity: O(n) (Linear time) If you were to implement dynamic resizing (doubling the capacity once the array is full), copying elements to a new larger array would take O(n) time because every element has to be moved to a new location. However, this doesn't happen on every push() call, so amortized over many operations, it approaches O(1) per operation.

Space Complexity: O(n) (Linear space) When resizing, a new array with larger capacity is allocated, leading to a linear space complexity based on the number of elements.

class ResizableArray {
  constructor() {
    this.data = {};
    this.length = 0;
    this.capacity = 2; // Initial capacity
  }

  push(value) {
    if (this.length === this.capacity) {
      this._resize(); // Resize array when it's full
    }
    this.data[this.length] = value;
    this.length++;
  }

  _resize() {
    const newData = {};
    this.capacity *= 2;
    for (let i = 0; i < this.length; i++) {
      newData[i] = this.data[i]; // O(n) operation
    }
    this.data = newData;
  }
}
로그인 후 복사

these are examples of how time and space complexity can be measured for different operations in a custom array implementation. They illustrate the computational cost in terms of time (how long the operation takes) and space (how much memory it uses) based on factors like the size of the array and the type of operation (e.g., push, pop, resizing). These measurements help analyze the efficiency of data structures and algorithms.

Usefulness in coding a javascript script

Custom arrays in JavaScript can be useful in several specific scenarios where you need more control over performance, memory management, or specific behaviors that JavaScript's native array doesn't provide out of the box. Here are a few use cases for custom arrays, along with examples showing how they can provide advantages.

Fixed-Length Array (Optimized Memory Use)

In some cases, you might want an array that has a fixed size, which helps control memory usage more precisely. JavaScript's native array dynamically resizes, but with a custom array, you can allocate a fixed amount of space for efficiency.

Use Case: You are developing a real-time application (e.g., a game or embedded system) where you need strict memory constraints and know exactly how many elements are required.

class FixedArray {
  constructor(size) {
    this.data = new Array(size); // Pre-allocating memory
    this.length = size;
  }

  set(index, value) {
    if (index >= this.length) throw new Error('Index out of bounds');
    this.data[index] = value;
  }

  get(index) {
    if (index >= this.length) throw new Error('Index out of bounds');
    return this.data[index];
  }
}

const fixedArr = new FixedArray(5);
fixedArr.set(0, 'A');
console.log(fixedArr.get(0));  // Output: A
로그인 후 복사

Advantage: Memory is pre-allocated and fixed, which can be beneficial when memory optimization is crucial.

Sparse Array (Efficient for Large, Mostly Empty Arrays)

A sparse array stores only non-null or non-zero elements, which can save memory in cases where an array is large but contains mostly empty or default values.

Use Case: You need to handle a large dataset where only a small percentage of the entries hold values (e.g., managing sparse matrices in scientific computing).

class SparseArray {
  constructor() {
    this.data = {};
  }

  set(index, value) {
    if (value !== null && value !== undefined) {
      this.data[index] = value;
    }
  }

  get(index) {
    return this.data[index] || null; // Return null if the value isn't set
  }
}

const sparseArr = new SparseArray();
sparseArr.set(1000, 'A');  // Only this value takes up memory
console.log(sparseArr.get(1000));  // Output: A
console.log(sparseArr.get(999));   // Output: null
로그인 후 복사

Implementing custom arrays in JavaScript gives you the flexibility to optimize for specific use cases like memory efficiency (fixed or sparse arrays), operational efficiency (circular buffers), or even better programming practices (immutable arrays). These optimizations can significantly improve performance and code reliability in applications with specific requirements, helping you go beyond the limitations of native JavaScript arrays.

Comparing Custom Arrays with Native Arrays

When comparing custom arrays with native arrays in JavaScript, it's essential to understand the strengths and weaknesses of each in different contexts. Native arrays are a built-in feature of JavaScript, providing developers with a highly optimized, dynamic data structure that’s easy to use and integrated deeply into the language. Native arrays come with numerous methods such as push(), pop(), map(), and filter(), which make array manipulation straightforward and efficient for most use cases. Their dynamic nature means they automatically resize when new elements are added, which is convenient when you don’t need strict control over memory management or performance optimizations.

On the other hand, custom arrays allow developers to control the internal behavior of the array-like data structures. Custom arrays can be implemented to fit specific performance, memory, or structural requirements that native arrays might not handle well. For instance, if you need a fixed-size array where resizing is not required, or you need a custom resizing mechanism, a custom array implementation would allow you to pre-allocate memory, control the resizing strategy, or even optimize access patterns to achieve constant-time operations.

One key benefit of custom arrays is that they give you direct control over how memory is allocated and how operations are performed. For example, if performance is crucial in a particular algorithm and native array methods introduce overhead, custom implementations can provide fine-tuned efficiency. Custom arrays can also be designed for more specialized use cases, such as circular buffers or sparse arrays, which are not supported natively in JavaScript.

네이티브 배열은 낮은 수준의 최적화를 활용하여 JavaScript 엔진 내에서 직접 구현되므로 일반적으로 대부분의 일반적인 시나리오에서 더 빠릅니다. 따라서 둘 중 하나를 사용하기로 한 결정은 특히 성능 및 메모리 관리 측면에서 애플리케이션의 특정 요구 사항에 따라 크게 달라집니다.


궁극적으로 사용자 정의 배열 구현을 통해 JavaScript와 컴퓨터 과학 원리에 대한 이해가 깊어지고 보다 효율적이고 사려 깊은 코드를 작성할 수 있는 능력이 향상되며 기본 추상화가 부족할 때 솔루션을 최적화할 수 있는 지식을 얻을 수 있습니다.

위 내용은 JS를 사용한 DSA: JavaScript의 사용자 정의 배열 데이터 구조 이해 - 단계별 가이드의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

원천:dev.to
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
저자별 최신 기사
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿