为什么我的打字稿中的选择排序不起作用

问题描述 投票:0回答:1

我学习了一个简单的选择排序算法并尝试使用 javascript/typescript。

这是我在SelectionSort类中的selection.sort算法

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;
  }
}

如果我仅调用largestTosmallest或smallestToBiggest,则仅此算法有效

我只使用最大到最小

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

我只使用最小到最大

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

但是如果我使用最大到最小和最小到最大

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

从大到小才起作用,从最小到最大不起作用。 startData 属性也被删除。

我的代码有什么问题吗? 为什么只从最大到最小的工作?

javascript typescript algorithm sorting selection-sort
1个回答
0
投票

正如卡斯滕在他的评论中指出的那样,问题是即使你定义了三个变量:

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

它们都使用相同的支持数组data,因此对三者之一所做的任何修改都会反映在其他数组中。鉴于您将

splice
应用于
biggestToSmallest()
之后的数组,变量
startData
dataForSmallestToBiggest
表示空数组,这就是为什么当您尝试打印
startData
时,您不会得到任何输出。

因此您可以在初始化时复制

data
数组,如下所示:

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

所以现在这三个变量将具有相同的数据,但指向不同的数组。

© www.soinside.com 2019 - 2024. All rights reserved.