Skip to main content

Posts

Selection Sort in Javascript

Selection Sort in Javascript Comparing each with the rest and swapping if greater ones are found. Time Complexity Best:  Ω(n^2) Average: θ(n^2) Worst: O(n^2) function selectionSort(arr){ let min; let i; let j; let temp; console.log("Input Array"); console.log(arr); for (i = 0; i < arr.length-1; i++) { min = i; for (j = i+1; j < arr.length; j++) { console.log(arr[i], arr[j]); if (arr[j] < arr[min]) { console.log(arr[j]); min = j; } } if (min !== i) { temp = arr[min]; arr[min] = arr[i]; arr[i] = temp; console.log(arr); } } console.log("Sorted using Selection Sort"); return arr } console.log(selectionSort([5,7,6,9,8,2,1,4,3])); // console.log(selectionSort([1,2,3,4,5,6,7,8,9])); // uncomment to try best case, i.e. sorted Run the live code here:  https://repl.it/@VinitKhandelwal/selection-sort-javascript

MongoDB — Queries at a Glance — 2

Types of MongoDB Queries at a Glance. Run the following one by one in the terminal of the folder where databases is stored. mongo Opens mongo shell show dbs displays all databases use <database name> used to get a database active db.<collection>.find().pretty() show cursor of all data from a collection db.<collection>.find({<field>: <integer>}).pretty() show cursor of data from a collection where <field> is <integer> value db.<collection>.find({<field>: {$gt: <integer>}}).pretty() show cursor of data from a collection where <field> is greater than <integer> value db.<collection>.find({<field>: {$gte: <integer>}}).pretty() show cursor of data from a collection where <field> is greater than or equal to <integer> value db.<collection>.findOne({<field>: <integer>}) show first document with <field> ...