├── .gitignore ├── README.md ├── algorithm ├── bubble-sort-pro.js ├── bubble-sort-pro2.js ├── bubble-sort.js ├── insertion-sort.js ├── merge-sort.js ├── quick-sort.js └── select-sort.js ├── syntax └── Function.md └── 【2019版新东方】阅读全真模拟1000题.pdf /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | yarn-debug.log* 6 | yarn-error.log* 7 | lerna-debug.log* 8 | 9 | # Diagnostic reports (https://nodejs.org/api/report.html) 10 | report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json 11 | 12 | # Runtime data 13 | pids 14 | *.pid 15 | *.seed 16 | *.pid.lock 17 | 18 | # Directory for instrumented libs generated by jscoverage/JSCover 19 | lib-cov 20 | 21 | # Coverage directory used by tools like istanbul 22 | coverage 23 | *.lcov 24 | 25 | # nyc test coverage 26 | .nyc_output 27 | 28 | # Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files) 29 | .grunt 30 | 31 | # Bower dependency directory (https://bower.io/) 32 | bower_components 33 | 34 | # node-waf configuration 35 | .lock-wscript 36 | 37 | # Compiled binary addons (https://nodejs.org/api/addons.html) 38 | build/Release 39 | 40 | # Dependency directories 41 | node_modules/ 42 | jspm_packages/ 43 | 44 | # TypeScript v1 declaration files 45 | typings/ 46 | 47 | # TypeScript cache 48 | *.tsbuildinfo 49 | 50 | # Optional npm cache directory 51 | .npm 52 | 53 | # Optional eslint cache 54 | .eslintcache 55 | 56 | # Microbundle cache 57 | .rpt2_cache/ 58 | .rts2_cache_cjs/ 59 | .rts2_cache_es/ 60 | .rts2_cache_umd/ 61 | 62 | # Optional REPL history 63 | .node_repl_history 64 | 65 | # Output of 'npm pack' 66 | *.tgz 67 | 68 | # Yarn Integrity file 69 | .yarn-integrity 70 | 71 | # dotenv environment variables file 72 | .env 73 | .env.test 74 | 75 | # parcel-bundler cache (https://parceljs.org/) 76 | .cache 77 | 78 | # Next.js build output 79 | .next 80 | 81 | # Nuxt.js build / generate output 82 | .nuxt 83 | dist 84 | 85 | # Gatsby files 86 | .cache/ 87 | # Comment in the public line in if your project uses Gatsby and *not* Next.js 88 | # https://nextjs.org/blog/next-9-1#public-directory-support 89 | # public 90 | 91 | # vuepress build output 92 | .vuepress/dist 93 | 94 | # Serverless directories 95 | .serverless/ 96 | 97 | # FuseBox cache 98 | .fusebox/ 99 | 100 | # DynamoDB Local files 101 | .dynamodb/ 102 | 103 | # TernJS port file 104 | .tern-port 105 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # javascript-algorithm 2 | JavaScript sort algorithm 3 | 4 | ### command 5 | `node xxx.js ` -------------------------------------------------------------------------------- /algorithm/bubble-sort-pro.js: -------------------------------------------------------------------------------- 1 | /* 2 | Optimization of bubble sort 3 | The first optimization method is to set a flag bit to mark whether an exchange has occurred, and if there is no exchange, it will end early; 4 | The second optimization method is to record the position of the "last exchange", which has been sorted as the position where the next comparison ends. 5 | */ 6 | 7 | let arr = [10, 9, 8, 7, 6, 5, 3, 4, 2, 1, 1, 3, 2, 3, 2]; 8 | 9 | function bubbleSortPro(arr){ 10 | let flag; 11 | let len = arr.length 12 | for(let i = 1; i <= len - 1; i++){ 13 | flag = false; //The flag must be reset at the beginning of each round 14 | for(let j = 0; j < len - i; j++){ 15 | if(arr[j] > arr[j + 1]){ 16 | temp = arr[j]; 17 | arr[j] = arr[j + 1]; 18 | arr[j + 1] = temp; 19 | flag = true; 20 | } 21 | } 22 | if(flag === false){ 23 | break; 24 | } 25 | } 26 | return arr; 27 | } 28 | 29 | console.log(bubbleSortPro(arr)); 30 | -------------------------------------------------------------------------------- /algorithm/bubble-sort-pro2.js: -------------------------------------------------------------------------------- 1 | 2 | let arr = [10, 9, 8, 7, 6, 5, 3, 4, 2, 1, 1, 3, 2, 3, 2]; 3 | 4 | function bubbleSortPro2(arr){ 5 | let k = 0; 6 | let n = arr.length; 7 | for(let i = 1, len = arr.length; i <= len - 1; i++){ 8 | k = n; //每一轮 都需重新设置到 上一轮冒泡结束的位置 9 | n = 0; 10 | for(let j = 0; j < k - 1; j++){ 11 | if(arr[j] > arr[j + 1]){ 12 | temp = arr[j]; 13 | arr[j] = arr[j + 1]; 14 | arr[j + 1] = temp; 15 | n = j + 1; 16 | } 17 | } 18 | } 19 | return arr; 20 | } 21 | 22 | console.log(bubbleSortPro2(arr)); 23 | -------------------------------------------------------------------------------- /algorithm/bubble-sort.js: -------------------------------------------------------------------------------- 1 | /* 2 | Bubble sort 3 | The first layer of loop controls how many loops of bubbling (n - 1) are required in total. Each round will bubble up a maximum number to the end of the array 4 | The second layer of loop starts from the leftmost v[0] every time, and compares it with the element to the right of it. 5 | */ 6 | 7 | let arr = [10, 10, 9, 9, 8, 7, 6, 5, 3, 4, 2, 1, 1, 3, 2, 3, 2]; 8 | 9 | function bubbleSort(arr){ 10 | let temp; 11 | let len = arr.length; 12 | for(let i = 1; i <= len - 1; i++) { 13 | for(let j = 0; j < len - i; j++) { 14 | if(arr[j] > arr[j + 1]){ 15 | temp = arr[j]; 16 | arr[j] = arr[j + 1]; 17 | arr[j + 1] = temp; 18 | } 19 | } 20 | } 21 | return arr; 22 | } 23 | 24 | console.log(bubbleSort(arr)); 25 | -------------------------------------------------------------------------------- /algorithm/insertion-sort.js: -------------------------------------------------------------------------------- 1 | function insertionSort(arr) { 2 | let temp; 3 | for(let i = 1,len = arr.length; i < len; i++){ 4 | for(let j = i; j > 0; j--){ 5 | if(arr[j] < arr[j -1]){ 6 | temp = arr[j]; 7 | arr[j] = arr[j - 1]; 8 | arr[j - 1] = temp; 9 | }else{ 10 | break; 11 | } 12 | } 13 | } 14 | return arr; 15 | } 16 | -------------------------------------------------------------------------------- /algorithm/merge-sort.js: -------------------------------------------------------------------------------- 1 | function insertionSort(arr, l, r){ 2 | for(let i = l; i <= r; i++){ 3 | let temp = arr[i], 4 | j; 5 | for(j = i; j > l && arr[j - 1] > temp; j--){ 6 | arr[j] = arr[j - 1]; 7 | } 8 | arr[j] = temp; 9 | } 10 | } 11 | 12 | function merge(arr, l, mid, r){ 13 | let aux = new Array(r - l + 1); 14 | 15 | for(let z = l; z <= r; z++){ 16 | aux[z - l] = arr[z]; 17 | } 18 | let i = l; 19 | let j = mid + 1; 20 | for(k = l; k <= r; k++){ 21 | if(i > mid){ 22 | arr[k] = aux[j - l]; 23 | j++ 24 | }else if(j > r){ 25 | arr[k] = aux[i - l]; 26 | i++; 27 | }else if(aux[i - l] < aux[j - l]){ 28 | arr[k] = aux[i - l]; 29 | i++; 30 | }else{ 31 | arr[k] = aux[j - l]; 32 | j++; 33 | } 34 | } 35 | } 36 | 37 | function sort(arr, l, r){ 38 | // if(l >= r){ 39 | // return; 40 | // } 41 | if(r - l < 15){ 42 | insertionSort(arr, l, r); 43 | } 44 | let mid = Math.floor((r - l)/ 2); 45 | sort(arr, l, mid); 46 | sort(arr, mid + 1, r); 47 | if(arr[mid] > arr[mid + 1]){ 48 | merge(arr, l, mid, r); 49 | } 50 | } 51 | 52 | function mergeSort(arr){ 53 | sort(arr, 0, arr.length - 1); 54 | } 55 | -------------------------------------------------------------------------------- /algorithm/quick-sort.js: -------------------------------------------------------------------------------- 1 | let arr = [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]; 2 | 3 | function quickSort(arr){ 4 | if(!arr.length){ 5 | return []; 6 | } 7 | let leftArr = []; 8 | let rightArr = []; 9 | let p = arr[0]; 10 | for(let i = 1, len = arr.length; i < len; i++){ 11 | if(p > arr[i]){ 12 | leftArr.push(arr[i]); 13 | }else{ 14 | rightArr.push(arr[i]); 15 | } 16 | } 17 | return quickSort(leftArr).concat(p, quickSort(rightArr)); 18 | } 19 | 20 | let res = quickSort(arr) 21 | 22 | console.log('res::', res); 23 | -------------------------------------------------------------------------------- /algorithm/select-sort.js: -------------------------------------------------------------------------------- 1 | let arr = [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]; 2 | 3 | function swap(array, t1, t2){ 4 | let temp = array[t1]; 5 | array[t1] = array[t2]; 6 | array[t2] = temp; 7 | } 8 | 9 | function selectionSort(arr){ 10 | let minIndex; 11 | let len = arr.length; 12 | for(let i = 0; i < len; i++){ 13 | minIndex = i; 14 | for(let j = i + 1; j < len; j++){ 15 | if(arr[j] < arr[minIndex]){ 16 | minIndex = j; 17 | } 18 | } 19 | swap(arr, i, minIndex); 20 | } 21 | return arr; 22 | } 23 | 24 | console.log(selectionSort(arr)); 25 | -------------------------------------------------------------------------------- /syntax/Function.md: -------------------------------------------------------------------------------- 1 | ### 関数を定義する 2 | JavaScriptでは、関数は次のように定義される。 3 | 4 | ``` 5 | function abs(x) { 6 | if (x >= 0) { 7 | return x; 8 | } else { 9 | return -x; 10 | } 11 | } 12 | ``` 13 | 上記の`abs()`関数の定義は次のとおりです。 14 | - functionは関数を定義することを示す。 15 | - absは関数の名前です。 16 | - (x)のxは関数のパラメーターであり、複数のパラメーターは[,]で区切られている。 17 | - {...}の間のコードは関数本体であり、複数のコードを含める。 18 | 19 | 要注意:関数本体内のステートメントが実行される時、`return`が実行されると、関数が実行完了、結果が返される。 したがって、条件付きの判断とループにより、関数内に非常に複雑なロジックが実装できる。 20 | 21 | `return`ステートメントがない場合、関数は実行後に結果も返しますが、結果は`undefined`です。 22 | 23 | JavaScript関数もオブジェクトであるため、上記で定義した`abs()`関数は関数オブジェクトであり、関数名`abs`は関数を指す変数と見られる。 24 | 25 | したがって、関数を定義する2番目の方法は次のとおりです。 26 | ``` 27 | var abs = function (x) { 28 | if (x >= 0) { 29 | return x; 30 | } else { 31 | return -x; 32 | } 33 | }; 34 | ``` 35 | 36 | このように、`function(x){...}`は無名関数であり、関数名はありません。 ただし、この無名関数は変数`abs`に割り当てられているため、変数`abs`を介して関数を呼び出すことができる。 37 | 上記の2つの定義は完全に同じです。 38 | 39 | ### 関数を呼び出す 40 | 関数を呼び出す時に、パラメーターを順序で渡す。 41 | ``` 42 | abs(10); // 10を返す 43 | abs(-8); // 8を返す 44 | ``` 45 | 定義されているよりも少ないパラメータを渡しても問題はありません。 46 | ``` 47 | abs(); // NaNを返す 48 | ``` 49 | この時点、`abs(x)`関数のパラメーター`x`は`undefined`受信され、結果は`NaN`です。 50 | 51 | ### 変数のスコープ 52 | JavaScriptでは、`var`宣言された変数はスコープされる。 53 | 変数が関数本体の内部で宣言されている場合、変数のスコープは関数本体全体であり、変数を関数の外部で参照することはできません。 54 | ``` 55 | function foo() { 56 | var x = 1; 57 | x = x + 1; 58 | } 59 | x = x + 2; // ReferenceError! 関数の外部で変数xを参照できません 60 | ``` 61 | 62 | 2つの異なる関数が同じ変数を宣言する場合、その変数はそれぞれの関数の本体内でのみ機能する。つまり、異なる関数内の同じ名前の変数は互いに独立しており、互いに影響を与えません。 63 | ``` 64 | function foo() { 65 | var x = 1; 66 | x = x + 1; 67 | } 68 | 69 | function bar() { 70 | var x = 'A'; 71 | x = x + 'B'; 72 | } 73 | ``` 74 | JavaScript関数はネスト(関数内関数)できるため、この時点で、内部関数は外部関数に定義された変数にアクセスできますが、その逆はできません。 75 | ``` 76 | function foo() { 77 | var x = 1; 78 | function bar() { 79 | var y = x + 1; // barはfooの変数xにアクセスできる 80 | } 81 | var z = y + 1; // ReferenceError! fooはbarの変数yにアクセスできません 82 | } 83 | ``` 84 | 内部関数と外部関数の変数名が同じ名前の場合はどうなりますか?それをテストしてみましょう: 85 | ``` 86 | function foo() { 87 | var x = 1; 88 | function bar() { 89 | var x = 'A'; 90 | console.log('x in bar() = ' + x); // 'A' 91 | } 92 | console.log('x in foo() = ' + x); // 1 93 | bar(); 94 | } 95 | foo(); 96 | // 結果: 97 | // x in foo() = 1 98 | // x in bar() = A 99 | ``` 100 | これは、JavaScript関数が変数を探すときに独自の関数の定義から始まり、「内部」から「外部」まで検索することを示している。内部関数が外部関数と同じ名前の変数を定義している場合、内部関数の変数は外部関数の変数をシールドする。 -------------------------------------------------------------------------------- /【2019版新东方】阅读全真模拟1000题.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/baikourin/learn-javascript/95b5c5ae68dc433fc84638711e448c170f4f5ad2/【2019版新东方】阅读全真模拟1000题.pdf --------------------------------------------------------------------------------