Open In App

Burst Sort Algorithm

Last Updated : 24 Jan, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

What is BurstSort?

BurstSort is an advanced sorting algorithm developed by Tony P. Hoare and Charles M. Payne in 1997. It is a hybrid of two sorting algorithms, namely quicksort and radix sort. BurstSort has a faster running time than both of its parent algorithms and is particularly efficient for sorting strings.

How Does BurstSort Work?

BurstSort combines the two sorting algorithms quicksort and radix sort in order to achieve a faster sorting time.
The algorithm works in two phases. 

  • The first phase is a quicksort step that sorts the elements in the list based on the first character of each element.
  • The second phase is a radix sort step which sorts the elements in the list based on the remaining characters in each element.

How is it different from the radix sort?

BurstSort is different from radix sort in that it is a hybrid algorithm. While radix sort works by sorting elements based on their individual characters, BurstSort first sorts the elements based on their first character and then uses radix sort to sort the remaining characters. This two-step approach makes BurstSort faster than radix sort.

Illustration:

Let’s consider the following array as an example: arr[]   = [1, 5, 3, 7, 2, 4, 6] 

1 5 3 7 2 4 6

Step 1: Split the array into two sub-arrays

1 5 3
7 2 4 6

Step 2: Sort the two sub-arrays:  

1 3 5
2 4 6 7

Step 3: Merge the two sorted sub-arrays:

1 2 3 4 5 6 7

Follow the below steps to solve the problem:

  • Split the array into two parts.
  • BurstSort each part separately.
  • Use mergeSort to join them.

Below is the implementation of the above approach:

C++




#include <iostream>
#include <vector>
using namespace std;
 
// Function to merge two sorted arrays into a single sorted
// array
vector<int> merge(vector<int>& arr1, vector<int>& arr2)
{
    vector<int> arr3;
    int i = 0, j = 0;
 
    while (i < arr1.size() && j < arr2.size()) {
        if (arr1[i] < arr2[j])
            arr3.push_back(arr1[i++]);
        else
            arr3.push_back(arr2[j++]);
    }
 
    while (i < arr1.size())
        arr3.push_back(arr1[i++]);
 
    while (j < arr2.size())
        arr3.push_back(arr2[j++]);
 
    return arr3;
}
 
// Function to split an array into two sub-arrays
vector<vector<int> > split(vector<int>& arr)
{
    vector<vector<int> > subarr(2);
    int mid = arr.size() / 2;
 
    for (int i = 0; i < mid; i++)
        subarr[0].push_back(arr[i]);
 
    for (int i = mid; i < arr.size(); i++)
        subarr[1].push_back(arr[i]);
 
    return subarr;
}
 
// Function to implement Burstsort algorithm
vector<int> burstSort(vector<int>& arr)
{
    // Base case: if array size is 1, return it
    if (arr.size() == 1)
        return arr;
 
    // Split the array into two sub-arrays
    vector<vector<int> > subarr = split(arr);
 
    // Sort the two sub-arrays
    vector<int> arr1 = burstSort(subarr[0]);
    vector<int> arr2 = burstSort(subarr[1]);
 
    // Merge the sorted arrays into a single sorted array
    return merge(arr1, arr2);
}
 
// Driver code
int main()
{
    vector<int> arr = { 1, 5, 3, 7, 2, 4, 6 };
 
    arr = burstSort(arr);
 
    for (int i = 0; i < arr.size(); i++)
        cout << arr[i] << " ";
 
    return 0;
}


Java




// Java code implementation
import java.io.*;
import java.util.*;
 
class GFG {
 
  // Function to merge two sorted arrays into a single
  // sorted array
  static List<Integer> merge(List<Integer> arr1,
                             List<Integer> arr2)
  {
    List<Integer> arr3 = new ArrayList<>();
    int i = 0, j = 0;
 
    while (i < arr1.size() && j < arr2.size()) {
      if (arr1.get(i) < arr2.get(j))
        arr3.add(arr1.get(i++));
      else
        arr3.add(arr2.get(j++));
    }
 
    while (i < arr1.size())
      arr3.add(arr1.get(i++));
 
    while (j < arr2.size())
      arr3.add(arr2.get(j++));
 
    return arr3;
  }
 
  // Function to split an array into two sub-arrays
  static List<List<Integer> > split(List<Integer> arr)
  {
    List<List<Integer> > subarr
      = new ArrayList<>(Arrays.asList(
        new ArrayList<>(), new ArrayList<>()));
    int mid = arr.size() / 2;
 
    for (int i = 0; i < mid; i++)
      subarr.get(0).add(arr.get(i));
 
    for (int i = mid; i < arr.size(); i++)
      subarr.get(1).add(arr.get(i));
 
    return subarr;
  }
 
  // Function to implement Burstsort algorithm
  static List<Integer> burstSort(List<Integer> arr)
  {
    // Base case: if array size is 1, return it
    if (arr.size() == 1)
      return arr;
 
    // Split the array into two sub-arrays
    List<List<Integer> > subarr = split(arr);
 
    // Sort the two sub-arrays
    List<Integer> arr1 = burstSort(subarr.get(0));
    List<Integer> arr2 = burstSort(subarr.get(1));
 
    // Merge the sorted arrays into a single sorted
    // array
    return merge(arr1, arr2);
  }
 
  public static void main(String[] args)
  {
    List<Integer> arr = new ArrayList<>();
    arr.add(1);
    arr.add(5);
    arr.add(3);
    arr.add(7);
    arr.add(2);
    arr.add(4);
    arr.add(6);
 
    arr = burstSort(arr);
 
    for (int i = 0; i < arr.size(); i++)
      System.out.print(arr.get(i) + " ");
 
    System.out.println();
  }
}
 
// This code is contributed by lokesh.


Python3




def merge(arr1, arr2):
    arr3 = []
    i, j = 0, 0
 
    while i < len(arr1) and j < len(arr2):
        if arr1[i] < arr2[j]:
            arr3.append(arr1[i])
            i += 1
        else:
            arr3.append(arr2[j])
            j += 1
 
    while i < len(arr1):
        arr3.append(arr1[i])
        i += 1
 
    while j < len(arr2):
        arr3.append(arr2[j])
        j += 1
 
    return arr3
 
def split(arr):
    mid = len(arr) // 2
    subarr1 = arr[:mid]
    subarr2 = arr[mid:]
    return subarr1, subarr2
 
def burstSort(arr):
    # Base case: if array size is 1, return it
    if len(arr) == 1:
        return arr
 
    # Split the array into two sub-arrays
    subarr1, subarr2 = split(arr)
 
    # Sort the two sub-arrays
    arr1 = burstSort(subarr1)
    arr2 = burstSort(subarr2)
 
    # Merge the sorted arrays into a single sorted array
    return merge(arr1, arr2)
 
arr = [1, 5, 3, 7, 2, 4, 6]
arr = burstSort(arr)
print(arr)
 
#code by ksam24000


C#




using System;
using System.Linq;
using System.Collections.Generic;
 
public class GFG
{
 
  // Function to merge two sorted arrays into a single
  // sorted array
  public static List<int> merge(List<int> arr1,
                                List<int> arr2)
  {
    List<int> arr3 = new List<int>();
    int i = 0, j = 0;
 
    while (i < arr1.Count && j < arr2.Count) {
      if (arr1[i] < arr2[j])
        arr3.Add(arr1[i++]);
      else
        arr3.Add(arr2[j++]);
    }
 
    while (i < arr1.Count)
      arr3.Add(arr1[i++]);
 
    while (j < arr2.Count)
      arr3.Add(arr2[j++]);
 
    return arr3;
  }
 
  // Function to split an array into two sub-arrays
  public static List<List<int> > split(List<int> arr)
  {
    List<List<int> > subarr = new List<List<int> >() {
      new List<int>(), new List<int>()
      };
    int mid = arr.Count / 2;
 
    for (int i = 0; i < mid; i++)
      subarr[0].Add(arr[i]);
 
    for (int i = mid; i < arr.Count; i++)
      subarr[1].Add(arr[i]);
 
    return subarr;
  }
 
  // Function to implement Burstsort algorithm
  public static List<int> burstSort(List<int> arr)
  {
    // Base case: if array size is 1, return it
    if (arr.Count == 1)
      return arr;
 
    // Split the array into two sub-arrays
    var subarr = split(arr);
 
    // Sort the two sub-arrays
    var arr1 = burstSort(subarr[0]);
    var arr2 = burstSort(subarr[1]);
 
    // Merge the sorted arrays into a single sorted
    // array
    return merge(arr1, arr2);
  }
 
  // Driver code
  static public void Main()
  {
    List<int> arr
      = new List<int>() { 1, 5, 3, 7, 2, 4, 6 };
 
    arr = burstSort(arr);
 
    Console.WriteLine(string.Join(" ", arr));
  }
}
 
// This code is contributed by akashish__


Javascript




// Function to merge two sorted arrays into a single sorted
// array
function merge(arr1, arr2)
{
    let arr3=new Array();
    let i = 0, j = 0;
 
    while (i < arr1.length && j < arr2.length) {
        if (arr1[i] < arr2[j])
            arr3.push(arr1[i++]);
        else
            arr3.push(arr2[j++]);
    }
 
    while (i < arr1.length)
        arr3.push(arr1[i++]);
 
    while (j < arr2.length)
        arr3.push(arr2[j++]);
 
    return arr3;
}
 
// Function to split an array into two sub-arrays
function split(arr)
{
    let subarr=new Array(2);
    for(let i=0; i<2; i++)
        subarr[i]=new Array();
    let mid = Math.floor(arr.length / 2);
 
    for (let i = 0; i < mid; i++)
        subarr[0].push(arr[i]);
 
    for (let i = mid; i < arr.length; i++)
        subarr[1].push(arr[i]);
 
    return subarr;
}
 
// Function to implement Burstsort algorithm
function burstSort(arr)
{
    // Base case: if array size is 1, return it
    if (arr.length == 1)
        return arr;
 
    // Split the array into two sub-arrays
    let subarr = split(arr);
 
    // Sort the two sub-arrays
    let arr1 = burstSort(subarr[0]);
    let arr2 = burstSort(subarr[1]);
 
    // Merge the sorted arrays into a single sorted array
    return merge(arr1, arr2);
}
 
// Driver code
let arr = [ 1, 5, 3, 7, 2, 4, 6 ];
 
arr = burstSort(arr);
 
for (let i = 0; i < arr.length; i++)
    console.log(arr[i]+" ");
 
 // This code is contributed by poojaagarwal2.


Output

1 2 3 4 5 6 7 

Time Complexity: O(N*log(N))
Auxiliary Space: O(N)

Related Articles:



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads