Back to Course

0% Complete
0/0 Steps

Lesson 37 of 48
In Progress

# Cubesort in C++

##### Yasin Cakal

Cubesort is a sorting algorithm that works by recursively sorting the elements of an array into small cubes and then merging them back together. It is an efficient sorting algorithm that is particularly good at sorting small amounts of data. In this article, we will discuss how the algorithm works, its time and space complexities, and provide examples of Cubesort implemented in C++.

## How Cubesort Works

Cubesort is a sorting algorithm that works by dividing an array into small cubes, sorting each cube, and then merging the cubes back together. The algorithm begins by dividing the array into small cubes of size N (where N is typically 8 or 16). The algorithm then sorts each cube using an internal sorting algorithm such as insertion sort or quicksort. Once each cube is sorted, the algorithm then merges the cubes back together using a merge sort algorithm. The result is an array that is sorted in ascending order.

## Time Complexity

Cubesort has an average time complexity of O(n log n). This is because the algorithm performs an internal sorting algorithm on each cube, followed by a merge sort algorithm to merge the cubes together. The time complexity is dependent on the size of the array and the internal sorting algorithm used.

The best-case time complexity of Cubesort is O(n log n), which occurs when the array is already sorted. The worst-case time complexity of Cubesort is O(n^2), which occurs when the array is not in order.

## Space Complexity

The space complexity of Cubesort is O(n), which is the same as the space complexity of the internal sorting algorithm used. This is because the algorithm does not require any additional space for the sorting process.

## C++ Implementation

Now that we have an understanding of how the algorithm works and its time and space complexities, let’s take a look at an example of Cubesort implemented in C++.

#include <iostream>
#include <algorithm>

const int CUBE_SIZE = 8;

// function to sort the cube
void cubeSort(int arr[], int n)
{
// divide the array into cubes of size CUBE_SIZE
for (int i = 0; i < n; i += CUBE_SIZE)
{
std::sort(arr + i, arr + std::min(i + CUBE_SIZE, n));
}

// merge the cubes
int temp[n];
for (int i = 0; i < n; i += CUBE_SIZE)
{
std::merge(arr + i, arr + std::min(i + CUBE_SIZE, n),
arr + std::min(i + CUBE_SIZE, n),
arr + std::min(i + 2*CUBE_SIZE, n),
temp + i);
}

// copy the result from temp[] back to arr[]
for (int i = 0; i < n; i++)
arr[i] = temp[i];
}

// main function
int main()
{
// input array
int arr[] = {3, 6, 7, 1, 5, 2, 8, 4};
int n = sizeof(arr) / sizeof(arr[0]);

// call cubeSort
cubeSort(arr, n);

// print the sorted array
for (int i = 0; i < n; i++)
std::cout << arr[i] << " ";
return 0;
}

## Conclusion

Cubesort is an efficient sorting algorithm that works by recursively sorting elements of an array into small cubes and then merging them back together. The algorithm has an average time complexity of O(n log n) and a space complexity of O(n). The time complexity is dependent on the size of the array and the internal sorting algorithm used. The algorithm is particularly good at sorting small amounts of data. In this article, we discussed how Cubesort works, its time and space complexities, and provided an example of Cubesort implemented in C++.

## Exercises

#### Write a C++ program to sort an array of integers using Cubesort.

#include <iostream>
#include <algorithm>

const int CUBE_SIZE = 8;

// function to sort the cube
void cubeSort(int arr[], int n)
{
// divide the array into cubes of size CUBE_SIZE
for (int i = 0; i < n; i += CUBE_SIZE)
{
std::sort(arr + i, arr + std::min(i + CUBE_SIZE, n));
}

// merge the cubes
int temp[n];
for (int i = 0; i < n; i += CUBE_SIZE)
{
std::merge(arr + i, arr + std::min(i + CUBE_SIZE, n),
arr + std::min(i + CUBE_SIZE, n),
arr + std::min(i + 2*CUBE_SIZE, n),
temp + i);
}

// copy the result from temp[] back to arr[]
for (int i = 0; i < n; i++)
arr[i] = temp[i];
}

// main function
int main()
{
// input array
int arr[] = {3, 6, 7, 1, 5, 2, 8, 4};
int n = sizeof(arr) / sizeof(arr[0]);

// call cubeSort
cubeSort(arr, n);

// print the sorted array
for (int i = 0; i < n; i++)
std::cout << arr[i] << " ";
return 0;
}

#### Write a C++ program to find the time complexity of a Cubesort algorithm.

#include <iostream>
#include <algorithm>

const int CUBE_SIZE = 8;

// function to calculate the time complexity of Cubesort
int cubeSortTimeComplexity(int arr[], int n)
{
int complexity = 0;
// divide the array into cubes of size CUBE_SIZE
for (int i = 0; i < n; i += CUBE_SIZE)
{
complexity += n * log(n);
}

// merge the cubes
int temp[n];
for (int i = 0; i < n; i += CUBE_SIZE)
{
complexity += n * log(n);
}

return complexity;
}

// main function
int main()
{
// input array
int arr[] = {3, 6, 7, 1, 5, 2, 8, 4};
int n = sizeof(arr) / sizeof(arr[0]);

// call cubeSortTimeComplexity
int complexity = cubeSortTimeComplexity(arr, n);

// print the time complexity
std::cout << "Time complexity of Cubesort is: " << complexity << std::endl;
return 0;
}

#### Write a C++ program to find the space complexity of a Cubesort algorithm.

#include <iostream>
#include <algorithm>

const int CUBE_SIZE = 8;

// function to calculate the space complexity of Cubesort
int cubeSortSpaceComplexity(int n)
{
int complexity = 0;
// divide the array into cubes of size CUBE_SIZE
complexity += n;

// merge the cubes
int temp[n];
complexity += n;

return complexity;
}

// main function
int main()
{
// input array
int arr[] = {3, 6, 7, 1, 5, 2, 8, 4};
int n = sizeof(arr) / sizeof(arr[0]);

// call cubeSortSpaceComplexity
int complexity = cubeSortSpaceComplexity(n);

// print the space complexity
std::cout << "Space complexity of Cubesort is: " << complexity << std::endl;
return 0;
}

#### Write a C++ program to implement Cubesort in an array of strings.

#include <iostream>
#include <algorithm>
#include <string>

const int CUBE_SIZE = 8;

// function to sort the cube
void cubeSort(std::string arr[], int n)
{
// divide the array into cubes of size CUBE_SIZE
for (int i = 0; i < n; i += CUBE_SIZE)
{
std::sort(arr + i, arr + std::min(i + CUBE_SIZE, n));
}

// merge the cubes
std::string temp[n];
for (int i = 0; i < n; i += CUBE_SIZE)
{
std::merge(arr + i, arr + std::min(i + CUBE_SIZE, n),
arr + std::min(i + CUBE_SIZE, n),
arr + std::min(i + 2*CUBE_SIZE, n),
temp + i);
}

// copy the result from temp[] back to arr[]
for (int i = 0; i < n; i++)
arr[i] = temp[i];
}

// main function
int main()
{
// input array
std::string arr[] = {"c", "a", "e", "b", "d"};
int n = sizeof(arr) / sizeof(arr[0]);

// call cubeSort
cubeSort(arr, n);

// print the sorted array
for (int i = 0; i < n; i++)
std::cout << arr[i] << " ";
return 0;
}

#### Write a C++ program to sort a two-dimensional array using Cubesort.

#include <iostream>
#include <algorithm>

const int CUBE_SIZE = 8;

// function to sort the cube
void cubeSort(int arr[][CUBE_SIZE], int n)
{
// divide the array into cubes of size CUBE_SIZE
for (int i = 0; i < n; i += CUBE_SIZE)
{
for (int j = 0; j < CUBE_SIZE; j++)
std::sort(arr[i] + j, arr[i] + std::min(j + CUBE_SIZE, n));
}

// merge the cubes
int temp[n][CUBE_SIZE];
for (int i = 0; i < n; i += CUBE_SIZE)
{
for (int j = 0; j < CUBE_SIZE; j++)
std::merge(arr[i] + j, arr[i] + std::min(j + CUBE_SIZE, n),
arr[i] + std::min(j + CUBE_SIZE, n),
arr[i] + std::min(j + 2*CUBE_SIZE, n),
temp[i] + j);
}

// copy the result from temp[] back to arr[]
for (int i = 0; i < n; i++)
{
for (int j = 0; j < CUBE_SIZE; j++)
arr[i][j] = temp[i][j];
}
}

// main function
int main()
{
// input array
int arr[][CUBE_SIZE] = {{3, 6, 7, 1, 5, 2, 8, 4},
{3, 6, 7, 1, 5, 2, 8, 4},
{3, 6, 7, 1, 5, 2, 8, 4}};
int n = sizeof(arr) / sizeof(arr[0]);

// call cubeSort
cubeSort(arr, n);

// print the sorted array
for (int i = 0; i < n; i++)
{
for (int j = 0; j < CUBE_SIZE; j++)
std::cout << arr[i][j] << " ";
std::cout << std::endl;
}
return 0;
}