[go: up one dir, main page]

0% found this document useful (0 votes)
19 views6 pages

Sorting InDSA Durgesh

1) The document discusses several sorting algorithms: bubble sort, insertion sort, merge sort, quick sort, and heap sort. 2) Bubble sort works by repeatedly swapping adjacent elements that are in the wrong order until the list is fully sorted. 3) Merge sort divides the array into halves, recursively sorts them, and then merges the sorted halves into a single sorted array. It has a time complexity of O(n log n).
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views6 pages

Sorting InDSA Durgesh

1) The document discusses several sorting algorithms: bubble sort, insertion sort, merge sort, quick sort, and heap sort. 2) Bubble sort works by repeatedly swapping adjacent elements that are in the wrong order until the list is fully sorted. 3) Merge sort divides the array into halves, recursively sorts them, and then merges the sorted halves into a single sorted array. It has a time complexity of O(n log n).
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

SORTING IN DSA

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];


let i = 0;
while (i < arr.length - 1) {
Bubble sort is a simple sorting
let j = 0;
algorithm in which we traverse
while (j < arr.length - i - 1) {
through a list, compare adjacent
if (arr[j] > arr[j + 1]) {
elements, and swap them if they
let temp = arr[j];
are in the wrong order. This process
arr[j] = arr[j + 1];
continues until the entire list is
arr[j + 1] = temp;
sorted. Smaller elements "bubble
}
up" to their correct positions, which
j++;
is why it is called "bubble sort."
}
i++;
}
console.log(arr);
BUBBLE SORT
do-while loop Using for 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]));

You might also like