Program for sorting variables of any data type
Given an array of any data type, the task is to sort the given array without using in-built sorting functions.
Examples:
Input: arr[] = [64, 34, 25, 12, 22, 11, 90]
Output: 11 12 22 25 34 64 90Input: arr[] = ["banana", "apple", "orange", "grape", "kiwi"]
Output: apple banana grape kiwi orangeInput: arr[] = [55.2, 10.6, 44.1, 36.0]
Output: 10.6 36 44.1 55.2
Using In-Built Sorting Functions - O(n log n) time and O(1) space
The idea is to use language specific in-built sorting functions to sort the array. Most of the modern statically typed programming languages either provide generic functions (For example C++ and Java) where we provide data type as input. In dynamically typed languages, we can anyways call functions for any data type that supports comparison.
// C++ program for sorting variables of any data type
#include <bits/stdc++.h>
using namespace std;
// Template function to implement
// bubble sort for any data type
template <typename T>
void sortArray(vector<T>& arr) {
sort(arr.begin(), arr.end());
}
template <typename T>
void print(vector<T>& arr) {
for (auto val : arr) {
cout << val << " ";
}
cout << endl;
}
int main() {
vector<int> intVector = {64, 34, 25, 12, 22, 11, 90};
sortArray(intVector);
print(intVector);
vector<string> stringVector = {"banana", "apple", "orange", "grape", "kiwi"};
sortArray(stringVector);
print(stringVector);
return 0;
}
// C++ program for sorting variables of any data type
using namespace std;
// Template function to implement
// bubble sort for any data type
template <typename T>
void sortArray(vector<T>& arr) {
sort(arr.begin(), arr.end());
}
template <typename T>
void print(vector<T>& arr) {
for (auto val : arr) {
cout << val << " ";
}
cout << endl;
}
int main() {
vector<int> intVector = {64, 34, 25, 12, 22, 11, 90};
sortArray(intVector);
print(intVector);
vector<string> stringVector = {"banana", "apple", "orange", "grape", "kiwi"};
sortArray(stringVector);
print(stringVector);
return 0;
}
// Java program for sorting variables of any data type
import java.util.*;
class GfG {
// Template function to implement
// bubble sort for any data type
static <T extends Comparable<T>> void sortArray(T[] arr) {
Arrays.sort(arr);
}
static <T> void print(T[] arr) {
for (T val : arr) {
System.out.print(val + " ");
}
System.out.println();
}
public static void main(String[] args) {
Integer[] intVector = {64, 34, 25, 12, 22, 11, 90};
sortArray(intVector);
print(intVector);
String[] stringVector = {"banana", "apple", "orange", "grape", "kiwi"};
sortArray(stringVector);
print(stringVector);
}
}
# Python program for sorting variables of any data type
# Template function to implement
# bubble sort for any data type
def sortArray(arr):
arr.sort()
def printArr(arr):
for val in arr:
print(val, end=" ")
print()
if __name__ == "__main__":
intVector = [64, 34, 25, 12, 22, 11, 90]
sortArray(intVector)
printArr(intVector)
stringVector = ["banana", "apple", "orange", "grape", "kiwi"]
sortArray(stringVector)
printArr(stringVector)
// C# program for sorting variables of any data type
using System;
class GfG {
// Template function to implement
// bubble sort for any data type
static void sortArray<T>(T[] arr) where T : IComparable<T> {
Array.Sort(arr);
}
static void print<T>(T[] arr) {
foreach (T val in arr) {
Console.Write(val + " ");
}
Console.WriteLine();
}
static void Main(string[] args) {
int[] intVector = {64, 34, 25, 12, 22, 11, 90};
sortArray(intVector);
print(intVector);
string[] stringVector = {"banana", "apple", "orange", "grape", "kiwi"};
sortArray(stringVector);
print(stringVector);
}
}
// JavaScript program for sorting variables of any data type
// Template function to implement
// bubble sort for any data type
function sortArray(arr) {
arr.sort();
}
function print(arr) {
for (let val of arr) {
process.stdout.write(val + " ");
}
console.log();
}
let intVector = [64, 34, 25, 12, 22, 11, 90];
sortArray(intVector);
print(intVector);
let stringVector = ["banana", "apple", "orange", "grape", "kiwi"];
sortArray(stringVector);
print(stringVector);
Output
11 12 22 25 34 64 90 apple banana grape kiwi orange
Writing Your Method - Example using Bubble Sort - O(n^2) time and O(1) space
The idea is to use "generic programming" or "parametric polymorphism" to create a single sorting algorithm that works with any data type.
For each language implementation:
- C++: Uses template functions (template<typename T>) to create type-agnostic code that works with any comparable data type.
- Java: Uses generic classes/methods (<T extends Comparable<T>>) to ensure type safety while allowing multiple data types.
- Python: Uses duck typing to handle different data types without explicit type declarations.
- C#: Uses generic methods/classes (<T> where T : IComparable<T>) to create reusable, type-safe algorithms.
- JavaScript: Uses its dynamic typing with implicit type conversion for operations on different data types.
// C++ program for sorting variables of any data type
#include <bits/stdc++.h>
using namespace std;
// Template function to implement
// bubble sort for any data type
template <typename T>
void sortArray(vector<T>& arr) {
int n = arr.size();
for (int i = 0; i < n - 1; i++) {
// Last i elements are already sorted,
// so we don't need to check them
for (int j = 0; j < n - i - 1; j++) {
// Compare adjacent elements and swap if needed
if (arr[j] > arr[j + 1]) {
swap(arr[j], arr[j + 1]);
}
}
}
}
template <typename T>
void print(vector<T>& arr) {
for (auto val : arr) {
cout << val << " ";
}
cout << endl;
}
int main() {
vector<int> intVector = {64, 34, 25, 12, 22, 11, 90};
sortArray(intVector);
print(intVector);
vector<string> stringVector = {"banana", "apple", "orange", "grape", "kiwi"};
sortArray(stringVector);
print(stringVector);
return 0;
}
// C++ program for sorting variables of any data type
using namespace std;
// Template function to implement
// bubble sort for any data type
template <typename T>
void sortArray(vector<T>& arr) {
int n = arr.size();
for (int i = 0; i < n - 1; i++) {
// Last i elements are already sorted,
// so we don't need to check them
for (int j = 0; j < n - i - 1; j++) {
// Compare adjacent elements and swap if needed
if (arr[j] > arr[j + 1]) {
swap(arr[j], arr[j + 1]);
}
}
}
}
template <typename T>
void print(vector<T>& arr) {
for (auto val : arr) {
cout << val << " ";
}
cout << endl;
}
int main() {
vector<int> intVector = {64, 34, 25, 12, 22, 11, 90};
sortArray(intVector);
print(intVector);
vector<string> stringVector = {"banana", "apple", "orange", "grape", "kiwi"};
sortArray(stringVector);
print(stringVector);
return 0;
}
// Java program for sorting variables of any data type
class GfG {
// Template function to implement
// bubble sort for any data type
static <T extends Comparable<T>> void sortArray(T[] arr) {
int n = arr.length;
for (int i = 0; i < n - 1; i++) {
// Last i elements are already sorted,
// so we don't need to check them
for (int j = 0; j < n - i - 1; j++) {
// Compare adjacent elements and swap if needed
if (arr[j].compareTo(arr[j + 1]) > 0) {
T temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
static <T> void print(T[] arr) {
for (T val : arr) {
System.out.print(val + " ");
}
System.out.println();
}
public static void main(String[] args) {
Integer[] intVector = {64, 34, 25, 12, 22, 11, 90};
sortArray(intVector);
print(intVector);
String[] stringVector = {"banana", "apple", "orange", "grape", "kiwi"};
sortArray(stringVector);
print(stringVector);
}
}
# Python program for sorting variables of any data type
# Template function to implement
# bubble sort for any data type
def sortArray(arr):
n = len(arr)
for i in range(n - 1):
# Last i elements are already sorted,
# so we don't need to check them
for j in range(n - i - 1):
# Compare adjacent elements and swap if needed
if arr[j] > arr[j + 1]:
arr[j], arr[j + 1] = arr[j + 1], arr[j]
def printArr(arr):
for val in arr:
print(val, end=" ")
print()
if __name__ == "__main__":
intVector = [64, 34, 25, 12, 22, 11, 90]
sortArray(intVector)
printArr(intVector)
stringVector = ["banana", "apple", "orange", "grape", "kiwi"]
sortArray(stringVector)
printArr(stringVector)
// C# program for sorting variables of any data type
using System;
class GfG {
// Template function to implement
// bubble sort for any data type
static void sortArray<T>(T[] arr) where T : IComparable<T> {
int n = arr.Length;
for (int i = 0; i < n - 1; i++) {
// Last i elements are already sorted,
// so we don't need to check them
for (int j = 0; j < n - i - 1; j++) {
// Compare adjacent elements and swap if needed
if (arr[j].CompareTo(arr[j + 1]) > 0) {
T temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
static void print<T>(T[] arr) {
foreach (var val in arr) {
Console.Write(val + " ");
}
Console.WriteLine();
}
static void Main(string[] args) {
int[] intVector = {64, 34, 25, 12, 22, 11, 90};
sortArray(intVector);
print(intVector);
string[] stringVector = {"banana", "apple", "orange", "grape", "kiwi"};
sortArray(stringVector);
print(stringVector);
}
}
// JavaScript program for sorting variables of any data type
// Template function to implement
// bubble sort for any data type
function sortArray(arr) {
let n = arr.length;
for (let i = 0; i < n - 1; i++) {
// Last i elements are already sorted,
// so we don't need to check them
for (let j = 0; j < n - i - 1; j++) {
// Compare adjacent elements and swap if needed
if (arr[j] > arr[j + 1]) {
[arr[j], arr[j + 1]] = [arr[j + 1], arr[j]];
}
}
}
}
function print(arr) {
console.log(arr.join(" "));
}
let intVector = [64, 34, 25, 12, 22, 11, 90];
sortArray(intVector);
print(intVector);
let stringVector = ["banana", "apple", "orange", "grape", "kiwi"];
sortArray(stringVector);
print(stringVector);
Output
11 12 22 25 34 64 90 apple banana grape kiwi orange