Program for sorting variables of any data type
Last Updated :
03 Jan, 2024
Write a program for sorting variables of any datatype without the use of std::sort .
Examples:
Input : 2000, 456, -10, 0
Output : -10 0 456 2000
Input : "We do nothing"
"Hi I have something"
"Hello Join something!"
"(Why to do work)"
Output :(Why to do work)
Hello Join something!
Hi I have something
We do nothing
The examples above show, we can have any data type elements present as an input and output will be in a sorted form of the input data. The idea here to solve this problem is to make a template.
Method 1 (Writing our own sort) In below code, we have implemented Bubble Sort to sort the array.
CPP
#include <bits/stdc++.h>
using namespace std;
template < class T>
void sortArray(T a[], int n)
{
bool b = true ;
while (b) {
b = false ;
for ( size_t i=0; i<n-1; i++) {
if (a[i] > a[i + 1]) {
T temp = a[i];
a[i] = a[i + 1];
a[i + 1] = temp;
b = true ;
}
}
}
}
template < class T>
void printArray(T a[], int n)
{
for ( size_t i = 0; i < n; ++i)
cout << a[i] << " " ;
cout << endl;
}
int main()
{
int n = 4;
int intArr[n] = { 2000, 456, -10, 0 };
sortArray(intArr, n);
printArray(intArr, n);
string strArr[n] = { "We do nothing" ,
"Hi I have something" ,
"Hello Join something!" ,
"(Why to do work)" };
sortArray(strArr, n);
printArray(strArr, n);
float floatArr[n] = { 23.4, 11.4, -9.7, 11.17 };
sortArray(floatArr, n);
printArray(floatArr, n);
return 0;
}
|
Java
import java.util.Arrays;
class Main {
public static <T extends Comparable<T>> void sortArray(T[] a) {
boolean sorted;
do {
sorted = true ;
for ( int i = 0 ; i < a.length - 1 ; i++) {
if (a[i].compareTo(a[i + 1 ]) > 0 ) {
T temp = a[i];
a[i] = a[i + 1 ];
a[i + 1 ] = temp;
sorted = false ;
}
}
} while (!sorted);
}
public static <T> void printArray(T[] a) {
for (T value : a) {
System.out.print(value + " " );
}
System.out.println();
}
public static void main(String[] args) {
Integer[] intArr = { 2000 , 456 , - 10 , 0 };
sortArray(intArr);
printArray(intArr);
String[] strArr = { "We do nothing" ,
"Hi I have something" ,
"Hello Join something!" ,
"(Why to do work)" };
sortArray(strArr);
printArray(strArr);
Float[] floatArr = { 23 .4f, 11 .4f, - 9 .7f, 11 .17f };
sortArray(floatArr);
printArray(floatArr);
}
}
|
Python
def sort_array(a):
sorted = False
while not sorted :
sorted = True
for i in range ( len (a) - 1 ):
if a[i] > a[i + 1 ]:
a[i], a[i + 1 ] = a[i + 1 ], a[i]
sorted = False
def print_array(a):
for value in a:
print (value),
print ()
if __name__ = = "__main__" :
int_arr = [ 2000 , 456 , - 10 , 0 ]
sort_array(int_arr)
print_array(int_arr)
str_arr = [ "We do nothing" ,
"Hi I have something" ,
"Hello Join something!" ,
"(Why to do work)" ]
sort_array(str_arr)
print_array(str_arr)
float_arr = [ 23.4 , 11.4 , - 9.7 , 11.17 ]
sort_array(float_arr)
print_array(float_arr)
|
C#
using System;
class GFG
{
static void SortArray<T>(T[] a)
where T : IComparable<T>
{
bool b = true ;
while (b)
{
b = false ;
for ( int i = 0; i < a.Length - 1; i++)
{
if (a[i].CompareTo(a[i + 1]) > 0)
{
T temp = a[i];
a[i] = a[i + 1];
a[i + 1] = temp;
b = true ;
}
}
}
}
static void PrintArray<T>(T[] a)
{
foreach (T element in a)
{
Console.Write(element + " " );
}
Console.WriteLine();
}
static void Main()
{
int [] intArr = { 2000, 456, -10, 0 };
SortArray(intArr);
PrintArray(intArr);
string [] strArr = { "We do nothing" ,
"Hi I have something" ,
"Hello Join something!" ,
"(Why to do work)" };
SortArray(strArr);
PrintArray(strArr);
float [] floatArr = { 23.4f, 11.4f, -9.7f, 11.17f };
SortArray(floatArr);
PrintArray(floatArr);
}
}
|
Javascript
function sortArray(arr) {
let swapped;
do {
swapped = false ;
for (let i = 0; i < arr.length - 1; i++) {
if (arr[i] > arr[i + 1]) {
[arr[i], arr[i + 1]] = [arr[i + 1], arr[i]];
swapped = true ;
}
}
} while (swapped);
}
function printArray(arr) {
console.log(arr.join( ' ' ));
}
function main() {
const intArr = [2000, 456, -10, 0];
sortArray(intArr);
printArray(intArr);
const strArr = [
"We do nothing" ,
"Hi I have something" ,
"Hello Join something!" ,
"(Why to do work)"
];
sortArray(strArr);
printArray(strArr);
const floatArr = [23.4, 11.4, -9.7, 11.17];
sortArray(floatArr);
printArray(floatArr);
}
main();
|
Output
-10 0 456 2000
(Why to do work) Hello Join something! Hi I have something We do nothing
-9.7 11.17 11.4 23.4
Method 2 (Using Library Function) We can use std::sort in C++ to sort array of any data type.
CPP
#include <bits/stdc++.h>
using namespace std;
template < class T>
void printArray(T a[], int n)
{
for ( size_t i = 0; i < n; ++i)
cout << a[i] << " " ;
cout << endl;
}
int main()
{
int n = 4;
int intArr[n] = { 2000, 456, -10, 0 };
sort(intArr, intArr + n);
printArray(intArr, n);
string strArr[n] = { "We do nothing" ,
"Hi I have something" ,
"Hello Join something!" ,
"(Why to do work)" };
sort(strArr, strArr + n);
printArray(strArr, n);
float floatArr[n] = { 23.4, 11.4, -9.7, 11.17 };
sort(floatArr, floatArr+n);
printArray(floatArr, n);
return 0;
}
|
Java
import java.util.Arrays;
public class Main {
public static void printArray( int [] a) {
for ( int i = 0 ; i < a.length; i++) {
System.out.print(a[i] + " " );
}
System.out.println();
}
public static void printArray(String[] a) {
for ( int i = 0 ; i < a.length; i++) {
System.out.print(a[i] + " " );
}
System.out.println();
}
public static void printArray( float [] a) {
for ( int i = 0 ; i < a.length; i++) {
System.out.print(a[i] + " " );
}
System.out.println();
}
public static void main(String[] args) {
int n = 4 ;
int [] intArr = { 2000 , 456 , - 10 , 0 };
Arrays.sort(intArr);
printArray(intArr);
String[] strArr = { "We do nothing" ,
"Hi I have something" ,
"Hello Join something!" ,
"(Why to do work)" };
Arrays.sort(strArr);
printArray(strArr);
float [] floatArr = { 23 .4f, 11 .4f, - 9 .7f, 11 .17f};
Arrays.sort(floatArr);
printArray(floatArr);
}
}
|
Python3
def printArray(a):
for i in range ( len (a)):
print (a[i], end = " " )
print ()
def main():
n = 4
intArr = [ 2000 , 456 , - 10 , 0 ]
intArr.sort()
printArray(intArr)
strArr = [ "We do nothing" ,
"Hi I have something" ,
"Hello Join something!" ,
"(Why to do work)" ]
strArr.sort()
printArray(strArr)
floatArr = [ 23.4 , 11.4 , - 9.7 , 11.17 ]
floatArr.sort()
printArray(floatArr)
if __name__ = = "__main__" :
main()
|
C#
using System;
public class MainClass
{
public static void PrintArray<T>(T[] arr)
{
foreach (T item in arr)
{
Console.Write(item + " " );
}
Console.WriteLine();
}
public static void Main()
{
int [] intArr = { 2000, 456, -10, 0 };
Array.Sort(intArr);
PrintArray(intArr);
string [] strArr = { "We do nothing" ,
"Hi I have something" ,
"Hello Join something!" ,
"(Why to do work)" };
Array.Sort(strArr);
PrintArray(strArr);
float [] floatArr = { 23.4f, 11.4f, -9.7f, 11.17f };
Array.Sort(floatArr);
PrintArray(floatArr);
}
}
|
Javascript
<script>
function printArray(arr) {
for (let i = 0; i < arr.length; i++) {
document.write(arr[i] + " " );
}
document.write( "<br>" );
}
const intArr = [2000, 456, -10, 0];
intArr.sort((a, b) => a - b);
printArray(intArr);
const strArr = [
"We do nothing" ,
"Hi I have something" ,
"Hello Join something!" ,
"(Why to do work)"
];
strArr.sort();
printArray(strArr);
const floatArr = [23.4, 11.4, -9.7, 11.17];
floatArr.sort();
printArray(floatArr);
</script>
|
Output
-10 0 456 2000
(Why to do work) Hello Join something! Hi I have something We do nothing
-9.7 11.17 11.4 23.4
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...