sorting_alltype_sorting

1.1.1 • Public • Published

Binary Search and Sorting Algorithms Package

This package provides a collection of common algorithms implemented in JavaScript, including:

  • Binary Search
  • Kadane's Algorithm
  • Sorting Algorithms:
    • Bubble Sort
    • Selection Sort
    • Insertion Sort
    • Merge Sort
    • Quick Sort
    • Heap Sort
    • Radix Sort
    • Counting Sort

Installation

You can install this package via npm:

npm install sorting_alltype_sorting

## How It Works

# Usage
# 1. Binary Search
Binary search finds the index of a target value within a sorted array. It works by repeatedly dividing the search interval in half. If the value of the target is less than the value in the middle of the interval, the search continues on the left half, otherwise, it continues on the right half. The algorithm has a time complexity of O(log n).

const { binarySearch } = require('sorting_alltype_sorting');

const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9];
const target = 5;
const index = binarySearch(arr, target);

console.log('Index of 5:', index); // Output: Index of 5: 4


# 2. Kadane's Algorithm
Kadane's Algorithm is used to find the maximum sum of a contiguous subarray in an array of integers. It works by iterating through the array, maintaining two values: currentMax, which is the maximum sum of the subarray that ends at the current index, and globalMax, which stores the maximum sum found so far. If currentMax becomes negative, it's reset to 0. The time complexity is O(n).


const { kadane } = require('sorting_alltype_sorting');

const arr = [1, -2, 3, 4, -1, 2, 1, -5, 4];
const maxSum = kadane(arr);

console.log('Maximum sum of subarray:', maxSum); // Output: Maximum sum of subarray: 9

# 3. Sorting Algorithms

#Bubble Sort
Bubble sort works by repeatedly stepping through the list to be sorted, comparing adjacent elements and swapping them if they are in the wrong order. The process is repeated until the list is sorted. The time complexity is O(n^2).

const { bubbleSort } = require('sorting_alltype_sorting');

const arr = [10, 5, 2, 8, 3, 1];
console.log('Bubble Sort:', bubbleSort(arr)); // Output: [1, 2, 3, 5, 8, 10]

#Selection Sort
Selection sort works by repeatedly finding the minimum element from the unsorted part of the list and swapping it with the first unsorted element. The time complexity is O(n^2).

const { selectionSort } = require('sorting_alltype_sorting');

const arr = [10, 5, 2, 8, 3, 1];
console.log('Selection Sort:', selectionSort(arr)); // Output: [1, 2, 3, 5, 8, 10]

#Insertion Sort
Insertion sort works by building a sorted list one element at a time. It iterates through the list and inserts each element into its correct position in the already sorted part of the list. The time complexity is O(n^2).

const { insertionSort } = require('sorting_alltype_sorting');

const arr = [10, 5, 2, 8, 3, 1];
console.log('Insertion Sort:', insertionSort(arr)); // Output: [1, 2, 3, 5, 8, 10]

#Heap Sort
Heap sort works by first building a max heap from the input data and then repeatedly extracting the maximum element from the heap and rebuilding the heap. The time complexity is O(n log n).

const { heapSort } = require('sorting_alltype_sorting');

const arr = [10, 5, 2, 8, 3, 1];
console.log('Heap Sort:', heapSort(arr)); // Output: [1, 2, 3, 5, 8, 10]

#Radix Sort
Radix sort works by sorting the input numbers digit by digit, starting from the least significant digit. It uses a stable sub-sorting algorithm (like counting sort) to sort the numbers by each digit. The time complexity is O(nk), where n is the number of elements and k is the number of digits.


const { radixSort } = require('sorting_alltype_sorting');

const arr = [10, 5, 2, 8, 3, 1];
console.log('Radix Sort:', radixSort(arr)); // Output: [1, 2, 3, 5, 8, 10]

#Counting Sort
Counting sort works by counting the occurrences of each element in the input list and using this information to place the elements in the correct sorted position. It is efficient when the range of numbers is known and not too large. The time complexity is O(n + k), where n is the number of elements and k is the range of input.

const { countingSort } = require('sorting_alltype_sorting');

const arr = [10, 5, 2, 8, 3, 1];
console.log('Counting Sort:', countingSort(arr)); // Output: [1, 2, 3, 5, 8, 10]

## 4. Example Usage with All Algorithms

const { bubbleSort, selectionSort, insertionSort, heapSort, radixSort, countingSort } = require('sorting_alltype_sorting');

const arr = [10, 5, 2, 8, 3, 1];

console.log('Bubble Sort:', bubbleSort(arr));
console.log('Selection Sort:', selectionSort(arr));
console.log('Insertion Sort:', insertionSort(arr));
console.log('Heap Sort:', heapSort(arr));
console.log('Radix Sort:', radixSort(arr));
console.log('Counting Sort:', countingSort(arr));



### Instructions:

1. **Create the `README.md` file** in the root of your project folder.
2. **Copy and paste the content** from the above markdown into the file.
3. **Customize the content** if needed, such as adjusting the package name or adding more detailed instructions.

This README file covers all the sorting algorithms, binary search, and Kadane's Algorithm, providing clear usage examples.

Package Sidebar

Install

npm i sorting_alltype_sorting

Weekly Downloads

3

Version

1.1.1

License

ISC

Unpacked Size

14.5 kB

Total Files

13

Last publish

Collaborators

  • vishnusingh