第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

如何根據(jù)不完整的標(biāo)準(zhǔn)進(jìn)行排序?

如何根據(jù)不完整的標(biāo)準(zhǔn)進(jìn)行排序?

慕俠2389804 2022-12-22 09:37:31
首先,我嘗試將自己的函數(shù)傳遞給Array.sort,但排序不正確。注意結(jié)果中的'c'before'a'是如何出現(xiàn)的,即使案例if (b == 'a' && a == 'c')處理正確。這些數(shù)據(jù)只是舉例。我的實(shí)際數(shù)據(jù)不按字母順序排序。它必須使用a_before_b和b_before_a函數(shù)中說明的邏輯。由于我只有確定某些(不是全部)元素對(duì)的相對(duì)順序的條件,因此可能存在多個(gè)有效的元素順序。我只需要生成任何有效的順序,其中有效的方式不與我的任何條件(在a_before_b和b_before_a函數(shù)中定義)相矛盾。const sorted = ['a', 'b', 'c', 'd']; // I do NOT have access to thisconst unsorted = ['c', 'd', 'a', 'b'];const a_before_b = (a, b) => {  if (a == 'a' && b == 'd') return true;  if (a == 'b' && b == 'c') return true;}const b_before_a = (a, b) => {  if (b == 'a' && a == 'c') return true;  if (b == 'b' && a == 'c') return true;}const mySortingFunction = (a, b) => {  if (a_before_b(a, b)) return -1;  if (b_before_a(a, b)) return 1;  return 0;}// doesn't produce correct sorting console.log(unsorted.sort(mySortingFunction)); // [ 'c', 'a', 'd', 'b' ]然后我嘗試從頭開始編寫自己的排序。但是進(jìn)入了死循環(huán),不知道為什么。const sorted = ['a', 'b', 'c', 'd'];const unsorted = ['c', 'd', 'a', 'b'];const a_before_b = (a, b) => {  if (a == 'a' && b == 'd') return true;  if (a == 'b' && b == 'c') return true;}const b_before_a = (a, b) => {  if (b == 'a' && a == 'c') return true;  if (b == 'b' && a == 'c') return true;}const findAnUnsortedElement = array => {  for (let [i, element] of Object.entries(array)) {    i = +i;    const a = element;    const b = array[i + 1];    if (b === undefined) return 'SORTING_COMPLETE';    if (!a_before_b(a, b)) console.log(a, 'should not be before', b);    if (b_before_a(a, b)) console.log(b, 'should be before', a);    if (!a_before_b(a, b) || b_before_a(a, b)) return a;  }}// from w3schoolsfunction move(arr, old_index, new_index) {  while (old_index < 0) {    old_index += arr.length;  }  while (new_index < 0) {    new_index += arr.length;  }  if (new_index >= arr.length) {    var k = new_index - arr.length;    while ((k--) + 1) {      arr.push(undefined);    }  }  arr.splice(new_index, 0, arr.splice(old_index, 1)[0]);  return arr;}
查看完整描述

3 回答

?
藍(lán)山帝景

TA貢獻(xiàn)1843條經(jīng)驗(yàn) 獲得超7個(gè)贊

const unsorted = ['c', 'd', 'a', 'b'];
const sorted = unsorted.sort();

它應(yīng)該工作 我不確定你的問題是什么。


查看完整回答
反對(duì) 回復(fù) 2022-12-22
?
犯罪嫌疑人X

TA貢獻(xiàn)2080條經(jīng)驗(yàn) 獲得超4個(gè)贊

我之前給出的答案中的算法(您(首先)接受了該算法)實(shí)際上是基于啟發(fā)式算法。


為了保證排序后的輸出沒有任何違規(guī),您可以將此問題視為圖形問題。只要兩個(gè)值可以進(jìn)行比較true(使用任一比較器函數(shù)),那么該對(duì)就代表圖中的一條邊。


如果順序一致,那么一定有一個(gè)值是其他值中最小的,否則就會(huì)有一個(gè)循環(huán)。


因此,有了這些知識(shí),我們就可以為圖中的每個(gè)節(jié)點(diǎn)確定到這樣一個(gè)最小節(jié)點(diǎn)的最長路徑有多長。當(dāng)您找到到此類最小節(jié)點(diǎn)的最長距離時(shí),您可以使用該路徑的長度作為絕對(duì)順序指示。


這是一個(gè)實(shí)現(xiàn):


class Node {

    constructor(value) {

        this.value = value;

        this.prev = new Set;

        this.order = 0; // No order yet

    }

    orderWith(other) {

        if (other === this) return;

        if (a_before_b(this.value, other.value) || b_before_a(other.value, this.value)) {

            other.prev.add(this);

        } else if (a_before_b(other.value, this.value) || b_before_a(this.value, other.value)) {

            this.prev.add(other);

        }

    }

    setOrder(path = new Set) {

        // Use recursion to find length of longest path to "least" node.

        if (this.order) return; // already done

        if (path.has(this)) throw "cycle detected";

        let order = 1;

        for (let prev of this.prev) {

            prev.setOrder(path.add(this));

            order = Math.max(order, prev.order + 1);

        }

        this.order = order; // If order is 1, it is a "least" node

    }

}


const a_before_b = (a, b) => {

  if (a == 'a' && b == 'd') return true;

  if (a == 'b' && b == 'c') return true;

}


const b_before_a = (a, b) => {

  if (b == 'a' && a == 'c') return true;

  if (b == 'b' && a == 'c') return true;

}


function mySort(arr) {

    // Create a graph: first the nodes

    let nodes = {}; // keyed by values in arr

    for (let value of arr) nodes[value] = nodes[value] || new Node(value);


    // Then the edges...

    for (let i = 0; i < arr.length; i++) {

        for (let j = i+1; j < arr.length; j++) {

            nodes[arr[i]].orderWith(nodes[arr[j]]);

        }

    }

    

    // Set absolute order, using the longest path from a node to a "least" node.

    for (let node of Object.values(nodes)) node.setOrder();

    

    // Sort array by order:

    return arr.sort((a, b) => nodes[a].order - nodes[b].order);

}


const sorted = ['a', 'b', 'c', 'd'];

const unsorted = ['c', 'd', 'a', 'b'];

console.log(mySort(unsorted));


查看完整回答
反對(duì) 回復(fù) 2022-12-22
?
小唯快跑啊

TA貢獻(xiàn)1863條經(jīng)驗(yàn) 獲得超2個(gè)贊

也許是這樣的


const sorted = ['a', 'b', 'c', 'd']; // I do NOT have access to this

const unsorted = ['c', 'd', 'a', 'b'];


const a_before_b = (a, b) => {

  if (a == 'a' && b == 'd') return true;

  if (a == 'b' && b == 'c') return true;

  if (a == 'a' && b == 'c') return true;


}


const b_before_a = (a, b) => {

  if (b == 'a' && a == 'c') return true;

  if (b == 'b' && a == 'c') return true;

}


const mySortingFunction = (a, b) => {

  if (a_before_b(a, b)) return -1;

  if (b_before_a(a, b)) return 1;

  return 0;

}


// doesn't produce correct sorting 

console.log(unsorted.sort(mySortingFunction));


查看完整回答
反對(duì) 回復(fù) 2022-12-22
  • 3 回答
  • 0 關(guān)注
  • 127 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)