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);
}
}
|
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
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!