Angular-Typescript: What could be causing my shell sort algorithm to halt abruptly after completing only 3 iterations across the entire array

I have been working on a sorting visualizer and have successfully implemented algorithms like bubble sort, selection sort, and insertion sort. However, I am facing an issue while trying to implement shell sort. The sorting process stops after 3 iterations through the entire array. Everything seems to be running smoothly until it suddenly halts. Could someone help me understand why this is happening? One possibility could be that the chart updates after each iteration of the inner loop. Below is the snippet of my code:

async ShellSort(delay = 5) { 
    for (let gap = Math.floor(this.data.length / 2); gap > 0; gap /= 2) {
      for (let i = gap; i < this.data.length; i++) {
        let temp = this.data[i];
        let j = i;
        while (j > gap && temp < this.data[j - gap]) {
          this.data[j] = this.data[j - gap];
          j -= gap;
        }
        this.data[j] = temp;

        //update the chart
        await new Promise(resolve =>
          setTimeout(() => {
            resolve();
          }, delay)
        );
        this.draw();

      }

    }
  }

Answer №1

Forget about it, the algorithm is functioning properly. The only issue is that JavaScript lacks an integer type, so I need to use Math.floor each time for a whole number instead of accessing a floating point index which isn't allowed.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The type inference in TypeScript sometimes struggles to accurately determine the type of an iterable

Struggling to get TypeScript to correctly infer the underlying iterable type for the function spread. The purpose of this function is to take an iterable of iterable types, infer the type of the underlying iterable, and return a new iterable with that infe ...

Angular Reactive Forms: Deleting a Validator from a Control

I'm currently tackling a challenge with Angular reactive forms. Validators are dynamically added to my controls in my specific scenario. Here's how it's done: const myControl = myFormGroup.get('myControl'); if (myControl.validat ...

Developing a JavaScript program for ATMs that can efficiently handle and dispense money in the fewest number of notes possible

When a certain amount is entered, the code should be capable of handling figures up to 20000. For instance, if the input amount is 2600 with a card balance of 3000, the output will be as follows: New Balance - 400 Notes: 2000 * 1 500 * 1 100 * 1 Only thre ...

Single-use binding format

I am a beginner with Angular and I have some doubts about the syntax used in Angular. In AngularJS, we can achieve one-time binding like this: <p>{{::myVar}}</p> In Angular, I know we can do it differently. <p [innerText]="myVar"></ ...

Asynchronous requests from clients paired with server-side rendering

Exploring the realm of SEO with Angular4/Node.js has presented a unique challenge for me. Utilizing Angular Universal allows for server-side rendering, enabling me to inject meta keywords, title, and image URLs into the HTML before it reaches the browser. ...

Show dynamically organized information within a hierarchy

Is there a more efficient way to present this dynamic hierarchical data in a visually appealing format? const data = [ { name: 'Paul', children: [ { name: 'John'}, { name: 'Rachel', children: [ { name: 'A ...

Angular 2 Aot Issue: CRITICAL ERROR: CALL_AND_RETRY_LAST Allocation unsuccessful - JavaScript heap exhausted

Encountered an issue while running Angular 2 AOT rollup: <--- Last few GCs ---> 144518 ms: Mark-sweep 1317.0 (1404.4) -> 1317.0 (1404.4) MB, 1522.9 / 0.0 ms [allocation failure] [GC in old space requested]. 146029 ms: Mark-sweep 1317.0 (1404 ...

Generics in Typescript interfaces

I'm trying to grasp the meaning of T = {} within this TypeScript interface. I've searched for documentation on this usage but haven't found anything specific. How does it differ from simply using T? interface CustomProps<T = {}> { ...

Understanding and processing HTML strings in typescript

I am currently utilizing TypeScript. Within my code, there is an object named "Reason" where all variables are defined as strings: value, display, dataType, and label. Reason = { value: '<ul><li>list item 1</li><li&g ...

Angular 2 fails to apply active class colors to tabs in a modal popup

https://i.sstatic.net/T8lIu.pnghttps://i.sstatic.net/O21ZO.pngIn my modal popup, I have multiple tabs in the modal body. The issue I am facing is that when the modal is invoked, the active tab color is not being applied. The modal itself is built using ngx ...

What is the best way to organize a for-loop to ensure it only runs once per iteration?

Looking for the first non-repeated character in a list: List s = ['l','e','e','t', 'l'] The first letter that isn't repeated is 't' Presenting my code: pointer = 0 while pointer < len( ...

Using Angular to Trigger a Keyboard Shortcut with a Button

Currently working on an Angular project that includes an event slideshow feature. Looking to make the slideshow go full screen when a button is clicked (Windows - fn+F11). Any tips on implementing a keyboard shortcut function in Angular? Appreciate any h ...

Avoid utilizing the i18n angular tool to translate any individual words within the string

For my internationalization application, I am using i18n angular. I am trying to figure out how to translate a string while keeping one word in the middle intact. This word might be an abbreviation or the name of a company. For example, when translatin ...

React-router-dom PrivateRoute component version 6.8.0

I have created a custom PrivateRoute for my chat app. This is how my PrivateRoute looks: import { useState, useEffect } from 'react'; import { Route, Navigate } from 'react-router-dom'; import axios from 'axios'; interface Pr ...

Demonstrating the Parent-Child relationship in Angular with .NET 6

I'm currently working on displaying a Client along with their list of contracts using Angular 13 and .NET 6. Here is the model I have in my WebApi: namespace CompanyApi.Models { public partial class Societaire { public Societaire() ...

The Ng4LoadingSpinner will automatically stop after 5 seconds

When utilizing Ng4LoadingSpinner, I encountered an issue where it disappears automatically after the default timeout of 5 seconds, even though our request is still processing. Increasing the timeout resolves the problem, but I am seeking an alternative s ...

Learn the process of synchronously loading forms and data in Angular

Looking to synchronize the loading of data and form... Let's start by reviewing the code: ngOnInit() { if (this.data.fromCalendar) { this.singleTraining(); } }, 200); this.formControl(); } formControl() { this.gib ...

Vue seems to intermittently catch sight of the TypeScript getter

Trying to wrap my head around when Vue decides to re-render an element. Let's explore the grid displayed below: https://i.sstatic.net/DtSQy.png In this grid, notice that Steve Rogers' total hours have been updated to 9 hours after inputting da ...

What is the best way to transpile TypeScript within the Astro framework?

Recently, I decided to dive into exploring Astro for a couple of upcoming projects. In my research, I delved into the script and typescript sections of the documentation (), as well as (). However, I found the workflow somewhat counterintuitive and struggl ...

The command "npm start" is currently experiencing issues, showing an error message stating that it failed at the start script for the project

I have been using this link as an example to learn Angular. However, when I try to run npm start, it shows an error. I have looked for solutions and they suggest updating either npm or Angular versions, but I am already using the latest versions: npm -v = ...