Al Youma Akmal
Al Youma Akmal

Reputation: 53

why my selection sort in typescript not working

I learn a simple selection sort algoritm and try using from javascript/typescript.

and this my selection.sort algorithm in SelectionSort class

export default class SelectionSort {
  private dataForSmallestToBiggest: number[];
  private dataForBiggestToSmallest: number[];
  public startData: number[];
  private biggestIndex: number = 0;
  private biggest: number = 0;
  private smallestIndex: number = 0;
  private smallest: number = 0;
  public iterationSmallestToBiggest: number = 0;
  public iterationBiggestToSmallest: number = 0;

  constructor(data: number[]) {
    // initiate for array smallest to biggest
    this.dataForSmallestToBiggest = data;
    // initiate for array biggest to smallest
    this.dataForBiggestToSmallest = data;
    // initiate for start data
    this.startData = data;
  }

  // set biggest data
  private setBiggest() {
    // initiate biggest data
    this.biggest = this.dataForBiggestToSmallest[0];
    //initiate biggest index data
    this.biggestIndex = 0;
    // looping for searching biggest index and biggest value from array
    this.dataForBiggestToSmallest.forEach((val, index) => {
      // if biggest found
      if (val > this.biggest) {
        // change biggest value
        this.biggest = val;
        // change biggest index
        this.biggestIndex = index;
      }
      // increase how many iteration for this looping
      this.iterationBiggestToSmallest++;
    });
  }

  // searching smallest data
  private setSmallest() {
    // initiate smallest data
    this.smallest = this.dataForSmallestToBiggest[0];
    //initiate smallest index data
    this.smallestIndex = 0;
    // looping for searching smallest index and smallest value from array
    this.dataForSmallestToBiggest.forEach((val, index) => {
      // if smallest found
      if (val < this.smallest) {
        // change the smallest value
        this.smallest = val;
        // change the smallest index
        this.smallestIndex = index;
      }
      // increase how many iteration for this looping
      this.iterationSmallestToBiggest++;
    });
  }
  // sorting from smallest to biggest
  public smallestToBiggest() {
    // initiate result variable
    let result: number[] = [];

    while (this.dataForSmallestToBiggest.length > 0) {
      //  change smallest value and smallest index
      this.setSmallest();
      // add result from smallest data;
      result.push(this.smallest);

      // remove array  by smallest inex
      this.dataForSmallestToBiggest.splice(this.smallestIndex, 1);
      // increase how many sorting smallest to biggest sort
      this.iterationSmallestToBiggest++;
    }
    return result;
  }

  // sorting from biggest to smallest
  public biggestToSmallest() {
    // initiate result variable
    let result: number[] = [];
    while (this.dataForBiggestToSmallest.length > 0) {
      //  change biggest value and biggest index
      this.setBiggest();

      // add result
      result.push(this.biggest);
      // remove array  by biggest inex
      this.dataForBiggestToSmallest.splice(this.biggestIndex, 1);
      // increase how many sorting smallest to biggest sort
      this.iterationBiggestToSmallest++;
    }
    return result;
  }
}

if i call biggestTosmallest only or smallestToBiggest only this algorithm work

i using biggest to smallest only

import SelectionSort from "./SelectionSort.js";

let selection = new SelectionSort([1, 5, 6, 3, 2, 7, 10, 3, 56, 18, 784, 83]);

let biggestToSmallest = selection.biggestToSmallest();
console.log(
  `biggest to smallest  data : ${selection.startData} iteration : ${selection.iterationBiggestToSmallest} , result :`
);
console.log(biggestToSmallest);

biggest to smallest work

i using smallest to biggest only

import SelectionSort from "./SelectionSort.js";

let selection = new SelectionSort([1, 5, 6, 3, 2, 7, 10, 3, 56, 18, 784, 83]);

let smallestToBiggest = selection.smallestToBiggest();
console.log(
  `smallest to biggest  data : ${selection.startData} iteration : ${selection.iterationSmallestToBiggest} , result :`
);
console.log(smallestToBiggest);



smallest to biggest work

But If i using biggest to smallest and smallest to biggest

import SelectionSort from "./SelectionSort.js";

let selection = new SelectionSort([1, 5, 6, 3, 2, 7, 10, 3, 56, 18, 784, 83]);

let biggestToSmallest = selection.biggestToSmallest();
console.log(
  `biggest to smallest  data : ${selection.startData} iteration : ${selection.iterationBiggestToSmallest} , result :`
);
console.log(biggestToSmallest);

let smallestToBiggest = selection.smallestToBiggest();
console.log(
  `smallest to biggest  data : ${selection.startData} iteration : ${selection.iterationSmallestToBiggest} , result :`
);
console.log(smallestToBiggest);

biggest to smallest only work

biggest to smallest only work, and smallest to biggest not work. startData property is erased too.

what wrong in my code? and why only biggest to smallest work?

Upvotes: 0

Views: 90

Answers (1)

denvicar
denvicar

Reputation: 1

As Carsten pointed out in his comment the problem is that even though you defined three variables:

// initiate for array smallest to biggest
dataForSmallestToBiggest = data;
// initiate for array biggest to smallest
this.dataForBiggestToSmallest = data;
// initiate for start data
this.startData = data;

They all use the same backing array data, so any modifications done to one of the three will be reflected in the others. Given that you apply splice to the array after biggestToSmallest() the variables startData and dataForSmallestToBiggest represent an empty array, that's why when you try to print startData you don't get any output.

So you can copy the data array on initialization like this:

// initiate for array smallest to biggest
dataForSmallestToBiggest = [...data];
// initiate for array biggest to smallest
this.dataForBiggestToSmallest = [...data];
// initiate for start data
this.startData = data;

So now the three variables will have the same data but point to different arrays.

Upvotes: 0

Related Questions