Program for sorting variables of any data type
Last Updated :
07 May, 2025
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 90
Input: arr[] = ["banana", "apple", "orange", "grape", "kiwi"]
Output: apple banana grape kiwi orange
Input: 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++
// 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;
}
Java
// 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
# 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#
// 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
// 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);
Output11 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.
CPP
// 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;
}
Java
// 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
# 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#
// 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
// 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);
Output11 12 22 25 34 64 90
apple banana grape kiwi orange
Similar Reads
C Program to Sort an array of names or strings Given an array of strings in which all characters are of the same case, write a C function to sort them alphabetically. The idea is to use qsort() in C and write a comparison function that uses strcmp() to compare two strings. C #include <stdio.h> #include <stdlib.h> #include <string.
2 min read
C++ program for Sorting Dates using Selection Sort C // C++ program for sorting dates using selection sort #include<bits/stdc++.h> using namespace std; struct date { int day; int month; int year; }; int main() { struct date input[5]; for(int i=0; i<5; i++) { cin>>input[i].day; cin>>input[i].month; cin>>input[i].year; } for
1 min read
Analysis of different sorting techniques In this article, we will discuss important properties of different sorting techniques including their complexity, stability and memory constraints. Before understanding this article, you should understand basics of different sorting techniques (See : Sorting Techniques). Time complexity Analysis - W
5 min read
Sorting | Natural Language Programming The only compound data types native to Osmosian Plain English are records and doubly-linked lists. When we need to sort a list, we use the simple recursive merge sort that I'll describe below. But first we need something to sort. Let's sort fruits, and let's begin with a type definition: A fruit is
4 min read
Program to sort string in descending order Given a string, sort it in descending order. Examples: Input : alkasingh Output : snlkihgaa Input : nupursingh Output : uusrpnnihg Input : geeksforgeeks Output : ssrokkggfeeee Recommended PracticeSort the string in descending orderTry It! A simple solution is to use library sort function std::sort()
7 min read
Commonly Asked Data Structure Interview Questions on Sorting Sorting is a fundamental concept in computer science and data structures, often tested in technical interviews. Sorting algorithms are essential for organizing data in a specific order, whether it's ascending or descending. Understanding various sorting techniquesâlike Quick Sort, Merge Sort, Bubble
4 min read