Prerequisite: How to dynamically allocate a 2D array in C?
Double pointer: A pointer pointing to another pointer is known as a Double pointer. To represent the double pointer ‘ ** ‘ is used. Double pointer is also called as pointer to pointer. Example:
Input: Geeks, Gfg, Placement, Sudo, Gate
Output: Gate, Geeks, Gfg, Placement, Sudo
The idea is to dynamically allocate memory and values to the strings in a form of a 2-D array. Then apply bubble sort using strcmp and strcpy function.
Implementation:
C++
#include <cstdlib>
#include <cstring>
#include <iostream>
void sort( char ** names, int n)
{
int i, j;
for (i = 0; i < n - 1; i++)
for (j = 0; j < n - i - 1; j++)
if ( strcmp (names[j], names[j + 1]) > 0) {
char * temp;
temp = ( char *) calloc (30, sizeof ( char ));
strcpy (temp, names[j]);
strcpy (names[j], names[j + 1]);
strcpy (names[j + 1], temp);
}
}
int main()
{
char ** names;
int n, i;
std::cout
<< "Enter the number of names to be printed: " ;
std::cin >> n;
names = ( char **) calloc (n, sizeof ( char *));
for (i = 0; i < n; i++)
{
names[i] = ( char *) calloc (30, sizeof ( char ));
std::cin >> names[i];
}
sort(names, n);
std::cout << "\nArray after sorting:\n" ;
for (i = 0; i < n; i++)
std::cout << names[i] << std::endl;
return 0;
}
|
C
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void sort( char ** names, int n)
{
int i, j;
for (i = 0; i < n - 1; i++)
for (j = 0; j < n - i - 1; j++)
if ( strcmp (names[j], names[j + 1]) > 0) {
char * temp;
temp = ( char *) calloc (30, sizeof ( char ));
strcpy (temp, names[j]);
strcpy (names[j], names[j + 1]);
strcpy (names[j + 1], temp);
}
}
int main()
{
char ** names;
int n, i;
printf ( "Enter the number of names to be printed: " );
scanf ( "%d\n" , &n);
names = ( char **) calloc (n, sizeof ( char *));
for (i = 0; i < n; i++)
{
names[i] = ( char *) calloc (30, sizeof ( char ));
scanf ( "%s" , names[i]);
}
sort(names, n);
printf ( "\nArray after sorting:\n" );
for (i = 0; i < n; i++)
printf ( "%s\n" , names[i]);
return 0;
}
|
Java
import java.util.Scanner;
class GFG
{
public static void sort(String names[], int n)
{
for ( int i = 0 ; i < n - 1 ; i++)
{
for ( int j = 0 ; j < n - i - 1 ; j++)
{
if (names[j].compareTo(names[j + 1 ]) > 0 )
{
String temp = names[j];
names[j] = names[j + 1 ];
names[j + 1 ] = temp;
}
}
}
}
public static void main (String[] args) {
Scanner scanner = new Scanner(System.in);
Sort ob = new Sort();
String names[] = new String[ 10 ];
int n = scanner.nextInt();
scanner.nextLine();
for ( int i = 0 ; i < n; i++){
names[i] = scanner.nextLine();
}
ob.sort(names, n);
System.out.println( "\nArray after sorting:\n" );
for ( int i = 0 ; i < n; i++){
System.out.println(names[i]);
}
}
}
|
Python3
def sort(names, n):
for i in range (n - 1 ):
for j in range (n - i - 1 ):
if (names[j] > names[j + 1 ]):
temp = names[j]
names[j] = names[j + 1 ]
names[j + 1 ] = temp
if __name__ = = '__main__' :
names = []
n = int ( input ( "Enter the number of names to be printed: " ))
for i in range ( 0 , n):
names.append( input ())
sort(names, n)
print ( "\nArray after sorting:\n" )
for i in range (n):
print (names[i])
|
Javascript
function sort(names, n){
for (let i=0; i<n-1; i++){
for (let j=0; j<n-i-1; j++){
if (names[j] > names[j+1]){
let temp = names[j];
names[j] = names[j+1];
names[j+1] = temp;
}
}
}
}
let names = [];
let n = parseInt(prompt( "Enter the number of names to be printed: " ));
for (let i=0; i<n; i++){
names.push(prompt());
}
sort(names, n);
console.log( "\nArray after sorting:\n" );
for (let i=0; i<n; i++){
console.log(names[i]);
}
|
C#
using System;
class Program {
static void Main( string [] args) {
string [] names;
int n, i;
Console.Write( "Enter the number of names to be printed: " );
string input = Console.ReadLine();
while (! int .TryParse(input, out n)) {
Console.Write( "Invalid input. Please enter a valid integer: " );
input = Console.ReadLine();
}
names = new string [n];
for (i = 0; i < n; i++)
{
names[i] = Console.ReadLine();
}
Sort(names, n);
Console.WriteLine( "\nArray after sorting:" );
for (i = 0; i < n; i++)
Console.WriteLine(names[i]);
}
static void Sort( string [] names, int n) {
int i, j;
for (i = 0; i < n - 1; i++)
for (j = 0; j < n - i - 1; j++)
if ( string .Compare(names[j], names[j + 1]) > 0) {
string temp;
temp = names[j];
names[j] = names[j + 1];
names[j + 1] = temp;
}
}
}
|
Output:

Please Login to comment...