Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

heap sort / selection sort #6

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
38 changes: 38 additions & 0 deletions src/algorithms/sorting/heapSort.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
function heapSort(arr) {
// Build the max heap
buildMaxHeap(arr);

// Heapify the array (move the root element to the end)
for (let i = arr.length - 1; i > 0; i--) {
[arr[0], arr[i]] = [arr[i], arr[0]]; // Swap the root with the last element
heapify(arr, 0, i); // Heapify the reduced heap
}

return arr;
}

function buildMaxHeap(arr) {
const n = arr.length;
for (let i = Math.floor(n / 2) - 1; i >= 0; i--) {
heapify(arr, i, n);
}
}

function heapify(arr, i, n) {
let largest = i;
let left = 2 * i + 1;
let right = 2 * i + 2;

if (left < n && arr[left] > arr[largest]) {
largest = left;
}

if (right < n && arr[right] > arr[largest]) {
largest = right;
}

if (largest !== i) {
[arr[i], arr[largest]] = [arr[largest], arr[i]];
heapify(arr, largest, n);
}
}
20 changes: 20 additions & 0 deletions src/algorithms/sorting/selectionSort.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
function selectionSort(inputArr) {
let n = inputArr.length;

for (let i = 0; i < n; i++) {
// Finding the smallest number in the subarray
let min = i;
for (let j = i + 1; j < n; j++) {
if (inputArr[j] < inputArr[min]) {
min = j;
}
}
if (min != i) {
// Swapping the elements
let tmp = inputArr[i];
inputArr[i] = inputArr[min];
inputArr[min] = tmp;
}
}
return inputArr;
}
5 changes: 4 additions & 1 deletion src/components/Header/SortHeader.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,12 @@ const Sortheader = (props) => {
<MenuItem value="bubble">Bubble Sort</MenuItem>
<MenuItem value="insertion">Insertion Sort</MenuItem>
<MenuItem value="quick">Quick Sort</MenuItem>
<MenuItem value="heap">Heap Sort</MenuItem>
<MenuItem value="selection">Selection Sort</MenuItem>
</Select>
<Link className="flex" to="/path-finding-algos">
<p style={{ marginRight: 10 }}>Path Finding Algorithms</p> {arrowForward}
<p style={{ marginRight: 10 }}>Path Finding Algorithms</p>{" "}
{arrowForward}
</Link>
</>
);
Expand Down
16 changes: 16 additions & 0 deletions src/sortingTutorial.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,22 @@ const sortData = [
"Pivot is a centre point or the mid point",
],
},
{
title: "Heap Sort",
list: [
"Heap sort is a comparison-based sorting technique based on Binary Heap data structure ",
"It is similar to the selection sort where we first find the minimum element and place the minimum element at the beginning ",
"Repeat the same process for the remaining elements",
],
},
{
title: "Selection Sort",
list: [
"Selection sort is a simple and efficient sorting algorithm that works by repeatedly selecting the smallest (or largest) element from the unsorted portion of the list and moving it to the sorted portion of the list ",
"The algorithm repeatedly selects the smallest (or largest) element from the unsorted portion of the list and swaps it with the first element of the unsorted part",
"This process is repeated for the remaining unsorted portion until the entire list is sorted",
],
},
];

export default sortData;