Skip to content
  • SiteMap
  • Our Services
  • Frequently Asked Questions (FAQ)
  • Support
  • About Us

UpdateGadh

Update Your Skills.

  • Home
  • Projects
    • Β Blockchain projects
    • Python Project
    • Data Science
    • Β Ai projects
    • Machine Learning
    • PHP Project
    • React Projects
    • Java Project
    • SpringBoot
    • JSP Projects
    • Java Script Projects
    • Code Snippet
    • Free Projects
  • Tutorials
    • Ai
    • Machine Learning
    • Advance Python
    • Advance SQL
    • DBMS Tutorial
    • Data Analyst
    • Deep Learning Tutorial
    • Data Science
    • Nodejs Tutorial
  • Blog
  • Contact us
  • Toggle search form
Sorting Algorithms

πŸš€ Sorting Algorithms: Merge, Heap, and Radix With Code

Posted on September 20, 2023January 2, 2025 By Updategadh No Comments on πŸš€ Sorting Algorithms: Merge, Heap, and Radix With Code

three different sorting algorithms: Merge Sort, Heap Sort, and Radix Sort. These algorithms are each explained briefly, and the code is provided for each one

Sorting Algorithms

Table of Contents

  • Sorting Algorithms
  • Merge Sort:
  • Heap Sort:
  • Radix Sort:

Merge Sort:

  • Merge Sort is a divide-and-conquer algorithm that divides the input array into two halves, recursively sorts them, and then merges the sorted halves.
  • Time Complexity: O(n log n)
  • Space Complexity: O(n)
import java.util.Arrays;

public class MergeSort {
    public static void mergeSort(int[] arr) {
        if (arr.length > 1) {
            int mid = arr.length / 2;
            int[] left = Arrays.copyOfRange(arr, 0, mid);
            int[] right = Arrays.copyOfRange(arr, mid, arr.length);

            mergeSort(left);
            mergeSort(right);

            int i = 0, j = 0, k = 0;
            while (i < left.length && j < right.length) {
                if (left[i] < right[j]) {
                    arr[k++] = left[i++];
                } else {
                    arr[k++] = right[j++];
                }
            }

            while (i < left.length) {
                arr[k++] = left[i++];
            }

            while (j < right.length) {
                arr[k++] = right[j++];
            }
        }
    }

    public static void main(String[] args) {
        int[] arr = {12, 11, 13, 5, 6, 7};
        System.out.println("Original array: " + Arrays.toString(arr));
        mergeSort(arr);
        System.out.println("Sorted array: " + Arrays.toString(arr));
    }
}
Sorting Algorithms
Sorting Algorithms

Heap Sort:

  • Heap Sort is based on the binary heap data structure. It builds a max-heap and repeatedly extracts the maximum element from it.
  • Time Complexity: O(n log n)
  • Space Complexity: O(1)
import java.util.Arrays;

public class HeapSort {
    public static void heapSort(int[] arr) {
        int n = arr.length;

        // Build a max heap
        for (int i = n / 2 - 1; i >= 0; i--) {
            heapify(arr, n, i);
        }

        // Extract elements from the heap one by one
        for (int i = n - 1; i >= 0; i--) {
            int temp = arr[0];
            arr[0] = arr[i];
            arr[i] = temp;

            heapify(arr, i, 0);
        }
    }

    public static void heapify(int[] arr, int n, int i) {
        int largest = i;
        int left = 2 * i + 1;
        int 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) {
            int swap = arr[i];
            arr[i] = arr[largest];
            arr[largest] = swap;

            heapify(arr, n, largest);
        }
    }

    public static void main(String[] args) {
        int[] arr = {12, 11, 13, 5, 6, 7};
        System.out.println("Original array: " + Arrays.toString(arr));
        heapSort(arr);
        System.out.println("Sorted array: " + Arrays.toString(arr));
    }
}

Radix Sort:

  • Radix Sort is a non-comparative sorting algorithm that sorts integers by processing individual digits. It can be applied to integers or strings with a fixed length.
  • Time Complexity: O(n * k)
  • Space Complexity: O(n + k)
import java.util.Arrays;

public class RadixSort {
    public static void radixSort(int[] arr) {
        int max = Arrays.stream(arr).max().getAsInt();
        for (int exp = 1; max / exp > 0; exp *= 10) {
            countingSort(arr, exp);
        }
    }

    public static void countingSort(int[] arr, int exp) {
        int n = arr.length;
        int[] output = new int[n];
        int[] count = new int[10];

        Arrays.fill(count, 0);

        for (int i = 0; i < n; i++) {
            count[(arr[i] / exp) % 10]++;
        }

        for (int i = 1; i < 10; i++) {
            count[i] += count[i - 1];
        }

        for (int i = n - 1; i >= 0; i--) {
            output[count[(arr[i] / exp) % 10] - 1] = arr[i];
            count[(arr[i] / exp) % 10]--;
        }

        System.arraycopy(output, 0, arr, 0, n);
    }

    public static void main(String[] args) {
        int[] arr = {170, 45, 75, 90, 802, 24, 2, 66};
        System.out.println("Original array: " + Arrays.toString(arr));
        radixSort(arr);
        System.out.println("Sorted array: " + Arrays.toString(arr));
    }
}

You can run each of these sorting algorithms independently to see how they work on different input arrays.

Sorting Algorithms
Sorting Algorithms
  • Online Examination System in PHP with Source Code
  • AI Chatbot for College and Hospital
  • Job Portal Web Application in PHP MySQL
  • Online Tutorial Portal Site in PHP MySQL β€” Full Project with Source Code
  • Online Job Portal System in JSP Servlet MySQL

https://updategadh.com/chatcpt/create-a-chatbot-with-openai-chatgpt/

Java Projects Video :Click here

https://updategadh.com/php-project-demo-vedios
https://updategadh.com/java-projects-demo-video
SPRINGBOOT PROJECTS
https://updategadh.com/python-projects/top-10-real-time-python-projects/
Post Views: 1,351
Java Notes Tags:Advance Java, Java, Java Project

Post navigation

Previous Post: E-Learning Project: Create Your E-Learning Web Portal with Java + Spring MVC!
Next Post: Boost Coding Skills :Linked Lists Data Structures and Algorithms πŸš€

More Related Articles

Java Collections Java Collection Java Notes
Linked Lists Data Structures Boost Coding Skills :Linked Lists Data Structures and Algorithms πŸš€ Java Notes
Java Exercises Basics to Advance - Free Java Programming Tutorial Java Exercises Basics to Advance – Free Java Programming Tutorial Java Notes

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

You may also like

  1. Boost Coding Skills :Linked Lists Data Structures and Algorithms πŸš€
  2. Java Collection
  3. Java Exercises Basics to Advance – Free Java Programming Tutorial

Most Viewed Posts

  1. Top Large Language Models in 2025
  2. Online Shopping System using PHP, MySQL with Free Source Code
  3. login form in php and mysql , Step-by-Step with Free Source Code
  4. Flipkart Clone using PHP And MYSQL Free Source Code
  5. News Portal Project in PHP and MySql Free Source Code
  6. User Login & Registration System Using PHP and MySQL Free Code
  7. Top 10 Final Year Project Ideas in Python
  8. Online Bike Rental Management System Using PHP and MySQL
  9. E learning Website in php with Free source code
  10. E-Commerce Website Project in Java Servlets (JSP)
  • AI
  • ASP.NET
  • Blockchain
  • ChatCPT
  • code Snippets
  • Collage Projects
  • Data Science Project
  • Data Science Tutorial
  • DBMS Tutorial
  • Deep Learning Tutorial
  • Final Year Projects
  • Free Projects
  • How to
  • html
  • Interview Question
  • Java Notes
  • Java Project
  • Java Script Notes
  • JAVASCRIPT
  • Javascript Project
  • JSP JAVA(J2EE)
  • Machine Learning Project
  • Machine Learning Tutorial
  • MySQL Tutorial
  • Node.js Tutorial
  • PHP Project
  • Portfolio
  • Python
  • Python Interview Question
  • Python Projects
  • PythonFreeProject
  • React Free Project
  • React Projects
  • Spring boot
  • SQL Tutorial
  • TOP 10
  • Uncategorized
  • Online Examination System in PHP with Source Code
  • AI Chatbot for College and Hospital
  • Job Portal Web Application in PHP MySQL
  • Online Tutorial Portal Site in PHP MySQL β€” Full Project with Source Code
  • Online Job Portal System in JSP Servlet MySQL

Most Viewed Posts

  • Top Large Language Models in 2025 (8,613)
  • Online Shopping System using PHP, MySQL with Free Source Code (5,213)
  • login form in php and mysql , Step-by-Step with Free Source Code (4,867)

Copyright Β© 2026 UpdateGadh.

Powered by PressBook Green WordPress theme