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:
@@ -64,6 +64,9 @@ export class SortingService {
|
|||||||
changed = this.switchValuesIfCorrect(arr, i, snapshots, changed);
|
changed = this.switchValuesIfCorrect(arr, i, snapshots, changed);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
arr[end].state = 'sorted';
|
||||||
|
snapshots.push(this.createSnapshot(arr));
|
||||||
|
|
||||||
//DONE
|
//DONE
|
||||||
if (!changed) {
|
if (!changed) {
|
||||||
break;
|
break;
|
||||||
@@ -71,12 +74,12 @@ export class SortingService {
|
|||||||
|
|
||||||
changed = false;
|
changed = false;
|
||||||
end -= 1;
|
end -= 1;
|
||||||
for (let i = end; i >= start; i--) {
|
for (let i = end-1; i >= start; i--) {
|
||||||
changed = this.switchValuesIfCorrect(arr, i, snapshots, changed);
|
changed = this.switchValuesIfCorrect(arr, i, snapshots, changed);
|
||||||
}
|
}
|
||||||
|
arr[start].state = 'sorted';
|
||||||
|
snapshots.push(this.createSnapshot(arr));
|
||||||
} while (changed);
|
} while (changed);
|
||||||
|
|
||||||
|
|
||||||
return snapshots;
|
return snapshots;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ import {SortData, SortSnapshot} from './sorting.models';
|
|||||||
import { FormsModule } from '@angular/forms';
|
import { FormsModule } from '@angular/forms';
|
||||||
import {MatInput} from '@angular/material/input';
|
import {MatInput} from '@angular/material/input';
|
||||||
import {UrlConstants} from '../../../constants/UrlConstants';
|
import {UrlConstants} from '../../../constants/UrlConstants';
|
||||||
|
import {MIN} from '@angular/forms/signals';
|
||||||
@Component({
|
@Component({
|
||||||
selector: 'app-sorting',
|
selector: 'app-sorting',
|
||||||
standalone: true,
|
standalone: true,
|
||||||
@@ -49,6 +50,7 @@ export class SortingComponent implements OnInit {
|
|||||||
|
|
||||||
newArraySizeSet()
|
newArraySizeSet()
|
||||||
{
|
{
|
||||||
|
this.arraySize = Math.min(Math.max(this.arraySize, this.MIN_ARRAY_SIZE), this.MAX_ARRAY_SIZE);
|
||||||
if (this.arraySize == this.sortArray.length)
|
if (this.arraySize == this.sortArray.length)
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
|
|||||||
Reference in New Issue
Block a user