Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"env": {
"browser": true,
"node": true,
"commonjs": true,
"jest": true,
"es6": true
},
"globals": {
"err": true,
"req": true,
"res": true,
"next": true
},
"extends": "eslint:recommended",
"parserOptions": {
"sourceType": "module"
},
"rules": {
"no-console": "off",
"indent": [ "error", 2 ],
"quotes": ["error", "single", { "allowTemplateLiterals": true }],
"comma-dangle": ["error", "always-multiline"],
"semi": [ "error", "always" ]
}
}

11 changes: 11 additions & 0 deletions BLOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Quick Sort

Divide and Conquer algorithm. It picks an element as pivot and partitions the given array around the picked pivot


## Trace :

Sample Array : [8,4,23,42,16,15]

![passes](p1.jpg)

6 changes: 0 additions & 6 deletions README.md

This file was deleted.

61 changes: 61 additions & 0 deletions challanges/quickSort/quickSort.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
'use strict';
function swap(arr, leftIndex, rightIndex){
let temp = arr[leftIndex];
// console.log('temp' , temp);
// console.log('left' , arr[leftIndex]);

arr[leftIndex] = arr[rightIndex];
// console.log('left after' , arr[leftIndex]);
// console.log('right' , arr[rightIndex]);

arr[rightIndex] = temp;
// console.log('right after' , arr[rightIndex]);

}
function partition(arr, leftIdx, rightIdx) {
let pivot = arr[Math.floor((rightIdx + leftIdx) / 2)],
i = leftIdx,
j = rightIdx;
console.log('pivot' ,pivot);
while (i <= j) {
while (arr[i] < pivot) {
i++;

}
while (arr[j] > pivot) {
j--;
}
if (i <= j) {
swap(arr, i, j);
i++;
j--;
}
}
return i;
}

function quickSort(arr, leftIdx, rightIdx) {
let index;
if (arr.length > 1) {
index = partition(arr, leftIdx, rightIdx);
console.log('left',leftIdx);
console.log('index',index);
console.log('right',rightIdx);
console.log('arr' ,arr);


if (leftIdx < index - 1) {
quickSort(arr, leftIdx, index - 1);
}
if (index < rightIdx) {
quickSort(arr, index, rightIdx);
}
}
return arr;
}

let arr = [8,4,23,42,16,15];
quickSort(arr, 0, arr.length - 1);
console.log(quickSort(arr, 0, arr.length - 1));
module.exports = quickSort ;
// used 'https://www.guru99.com/quicksort-in-javascript.html' for solving this code challange
18 changes: 18 additions & 0 deletions challanges/quickSort/quickSort.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@

'use strict';
// eslint-disable-next-line no-unused-vars
const quickSort = require('./quickSort.js');

describe(`quick sort`, () => {

it('sort an array', () => {
const arr = [5,2,8,4,6,7];
expect(quickSort(arr, 0, arr.length-1)).toEqual([2,4,5,6,7,8]);
});

it(' sort an array with duplicate values', () => {
const arr = [5,2,8,4,6,7,6];
expect(quickSort(arr, 0, arr.length-1)).toEqual([2,4,5,6,6,7,8]);
});

});
Binary file added p1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading