• js inplace algorithm All In One


    js in-place algorithm All In One

    solutions

    1. j = i + 1
    /**
     * @param {number[]} nums
     * @return {void} Do not return anything, modify nums in-place instead.
     */
    var sortColors = function(nums) {
      const len = nums.length;
      for (let i = 0; i < len; i++) {
        for (let j = i + 1; j < len; j++) {
          const temp = nums[i];
          const current = nums[j];
          //log('temp =', temp,';current =', current);
          if(temp > current) {
            // swap
            nums[i] = current;
            nums[j] = temp;
          }
          //log('nums =', nums);
        }
        //log('\n');
      }
    };
    
    
    1. j = 1
    /**
     * @param {number[]} nums
     * @return {void} Do not return anything, modify nums in-place instead.
     */
    var sortColors = function(nums) {
      const len = nums.length;
      for (let i = 0; i < len; i++) {
        for (let j = i; j < len; j++) {
          const temp = nums[i];
          const current = nums[j];
          //log('temp =', temp,';current =', current);
          if(temp > current) {
            // swap
            nums[i] = current;
            nums[j] = temp;
          }
          //log('nums =', nums);
        }
        //log('\n');
      }
    };
    
    
    1. j = 0
    
    /**
     * @param {number[]} nums
     * @return {void} Do not return anything, modify nums in-place instead.
     */
    var sortColors = function(nums) {
        for (let index = 0; index < nums.length; index++) {
            for (let j = 0; j < nums.length; j++) {
              const temp = nums[index];
              const current = nums[j];
              // ❓ bigger
              if(temp < current) {
                // swap
                nums[index] = current;
                nums[j] = temp;
              }
          }
        }
    };
    
    
    

    demo

    "use strict";
    
    /**
     *
     * @author xgqfrms
     * @license MIT
     * @copyright xgqfrms
     * @created 2022-01-11
     * @modified
     *
     * @description 75. Sort Colors
     * @description 75. 颜色分类
     * @difficulty Medium
     * @complexity O(n)
     * @time O(n)
     * @augments
     * @example
     * @link https://leetcode.com/problems/sort-colors/
     * @link https://leetcode-cn.com/problems/sort-colors/
     * @solutions
     *
     * @best_solutions
     *
     */
    
    const log = console.log;
    
    // @decorator
    const performanceTest = async (func, args, order = '') => {
      const start = Date.now();
      await func(args);
      const end = Date.now();
      log(`cost ${order} =`, (end - start) / 1000);
    };
    
    /**
     * @param {number[]} nums
     * @return {void} Do not return anything, modify nums in-place instead.
     */
    
     var sortColors = function(nums) {
      const len = nums.length;
      for (let i = 0; i < len; i++) {
        for (let j = i + 1; j < len; j++) {
          const temp = nums[i];
          const current = nums[j];
          // log('temp =', temp,';current =', current);
          if(temp > current) {
            // swap
            nums[i] = current;
            nums[j] = temp;
          }
          // log('nums =', nums);
        }
        // log('\n');
      }
      log('1 =', nums);
    };
    
    
    
    var sortColors2 = function(nums) {
      const len = nums.length;
      for (let i = 0; i < len; i++) {
        for (let j = i; j < len; j++) {
          const temp = nums[i];
          const current = nums[j];
          // log('temp =', temp,';current =', current);
          if(temp > current) {
            // swap
            nums[i] = current;
            nums[j] = temp;
          }
          // log('nums =', nums);
        }
        // log('\n');
      }
      log('2 =', nums);
    };
    
    var sortColors3 = function(nums) {
      const len = nums.length;
      for (let i = 0; i < len; i++) {
        for (let j = 0; j < len; j++) {
          const temp = nums[i];
          const current = nums[j];
          // log('temp =', temp,';current =', current);
          // ?
          if(temp < current) {
            // swap
            nums[i] = current;
            nums[j] = temp;
          }
          // log('nums =', nums);
        }
        // log('\n');
      }
      log('3 =', nums);
    };
    
    const nums = [ 2, 0, 2, 1, 1, 0]
    // [0, 0, 1, 1, 2, 2]
    performanceTest(sortColors, nums, 1);
    performanceTest(sortColors2, nums, 2);
    performanceTest(sortColors3, nums, 3);
    // const test = sortColors(nums);
    // const test2 = sortColors2(nums);
    // const test3 = sortColors3(nums);
    // log(`test =`, test);
    // log(`test2 =`, test2);
    // log(`test3 =`, test3);
    
    

    refs



    ©xgqfrms 2012-2020

    www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!

    原创文章,版权所有©️xgqfrms, 禁止转载 ️,侵权必究⚠️!


    xgqfrms
  • 相关阅读:
    淘票票项目总结
    淘票票 Model制作过程
    黄金梅丽号第四次讨论会议
    淘座座用户需求调查问卷的调查结果
    淘座座用户需求调查问卷
    淘座座需求分析报告
    淘座座软件工程技术说明书
    淘座座软件项目计划书
    利用hutool配置发送邮件的问题 及 阿里企业邮箱526 Authentication failure 错误问题
    DispatcherServlet和ContextLoaderListener,还有spring+servlet3.0 无web.xml启动问题
  • 原文地址:https://www.cnblogs.com/xgqfrms/p/15787877.html
Copyright © 2020-2023  润新知