Professional Documents
Culture Documents
Sorting InDSA Durgesh
Sorting InDSA Durgesh
Sorting InDSA Durgesh
Bubble Sort Insertion Sort Merge Sort Quick Sort Heap Sort
JavaScript
By Durgesh Bisen
BUBBLE SORT
double while loop
let arr = [64, 34, 25, 12, 22, 11, 90]; function bubbleSort(arr) {
let swapped; for (let j = 0; j < arr.length - 1; j++) {
do { for (var i = 0; i < arr.length - 1; i++) {
swapped = false; if (arr[i] > arr[i + 1]) {
for (var i = 0; i < arr.length - 1; i++) { let temp = arr[i];
if (arr[i] > arr[i + 1]) { arr[i] = arr[i + 1];
let temp = arr[i]; arr[i + 1] = temp;
arr[i] = arr[i + 1]; }
arr[i + 1] = temp; }
swapped = true; }
} return arr;
} }
} while (swapped); let result = bubbleSort([64, 34, 25, 12, 22, 11,
90]);
console.log("sorted array is: " + arr);
console.log(result);
function selectionSort(array) {
for (let i = 0; i < array.length; i++) {
let min = i;
SELECTION SORT for (let j = i + 1; j < array.length; j++) {
if (array[j] < array[min]) {
Selection Sort is a simple min = j;
sorting algorithm in which
}
we traverse through the list
and find the minimum }
value. Then, we swap that // use this method.
minimum value with the // [array[i], array[min]] = [array[min], array[i]];
element at the current
position in the list. We
repeat this process until the // or use this method.
entire list is sorted. The time temp = array[i];
complexity of Selection Sort array[i] = array[min];
is O(n^2) in both average array[min] = temp;
and worst cases.
}
return array;
}
console.log(selectionSort([4, 7, 8, 5, 3, 9, 1, 6, 0, 2]));
MERGE SORT
function mergeSort(arr) { while (i < left.length && j < right.length) {
Merge Sort is an if (arr.length <= 1) { if (left[i] < right[j]) {
efficient sorting return arr; result.push(left[i]);
algorithm that
} i++;
divides the array
const mid = Math.floor(arr.length / 2); } else {
and recursively sorts
it. Then, the sorted const left = arr.slice(0, mid); result.push(right[j]);
subarrays are const right = arr.slice(mid); j++;
merged to create return merge(mergeSort(left), mergeSort(right)); }
the final sorted } }
array. This algorithm function merge(left, right) { return result.concat(left.slice(i)).concat(right.slice(j));
has a time
let result = []; }
complexity of
let i = 0; console.log(mergeSort([64, 25, 12, 22, 11]));
O(n log n). let j = 0;
function quickSort(arr) {
if (arr.length <= 1) {
return arr;
QUICK SORT }
const pivot = arr[0];
Quick Sort is an efficient let left = [];
sorting algorithm. In this
algorithm, we choose a const right = [];
pivot element and place it
in its correct position. Then, for (let i = 1; i < arr.length; i++) {
we divide the array into two
if (arr[i] < pivot) {
partitions based on whether
the elements are greater or left.push(arr[i]);
smaller than the pivot. This } else {
process is repeated
right.push(arr[i]);
recursively until all elements
are sorted. Quick Sort has a }
time complexity of }
O(n log n). return [...quickSort(left), pivot, ...quickSort(right)];
}
console.log(quickSort([5, 2, 9, 3, 6, 8]));