Mark sorted bounds and clamp array size

In the sorting service, mark boundary elements as 'sorted' after each forward/backward pass and push snapshots to capture those states; also adjust the backward loop to start at end-1 to avoid rechecking the already-placed element. In the component, clamp new array size to the MIN_ARRAY_SIZE..MAX_ARRAY_SIZE range in newArraySizeSet. Adds an import for MIN from @angular/forms/signals.
This commit is contained in:
2026-02-04 16:01:18 +01:00
parent 6b64c5f4e9
commit e3d835300b
2 changed files with 8 additions and 3 deletions

View File

@@ -64,6 +64,9 @@ export class SortingService {
changed = this.switchValuesIfCorrect(arr, i, snapshots, changed);
}
arr[end].state = 'sorted';
snapshots.push(this.createSnapshot(arr));
//DONE
if (!changed) {
break;
@@ -71,12 +74,12 @@ export class SortingService {
changed = false;
end -= 1;
for (let i = end; i >= start; i--) {
for (let i = end-1; i >= start; i--) {
changed = this.switchValuesIfCorrect(arr, i, snapshots, changed);
}
arr[start].state = 'sorted';
snapshots.push(this.createSnapshot(arr));
} while (changed);
return snapshots;
}

View File

@@ -11,6 +11,7 @@ import {SortData, SortSnapshot} from './sorting.models';
import { FormsModule } from '@angular/forms';
import {MatInput} from '@angular/material/input';
import {UrlConstants} from '../../../constants/UrlConstants';
import {MIN} from '@angular/forms/signals';
@Component({
selector: 'app-sorting',
standalone: true,
@@ -49,6 +50,7 @@ export class SortingComponent implements OnInit {
newArraySizeSet()
{
this.arraySize = Math.min(Math.max(this.arraySize, this.MIN_ARRAY_SIZE), this.MAX_ARRAY_SIZE);
if (this.arraySize == this.sortArray.length)
{
return;