Tuesday 3 October 2017

Big Sorting Hacker Rank solution in C/C++

https://www.hackerrank.com/challenges/big-sorting/forum

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

char **temp;                    //to store sorted elements
void sort(char **ar, int n)     //merge sort on strings
{
        int nl,  nh,  i,  j,  k;
        if ( n <= 1 )
                return;
        nl = n/2;                             //mid calculation
        nh = n - nl;
        sort( ar, nl );                       //passing string array with number of strings as n1(i.e, first half)
        sort( &(ar[nl]), nh );                //passing remaining half from middle to last?
        for(k = i = 0, j = nl; i < nl && j < n; k++)  //merge function
        {
                int l1 = strlen( ar[ i ] );
                int l2 = strlen( ar[ j ] );
                if ( l1 < l2 || ( l1 == l2 && strcmp( ar[i], ar[j] ) < 0) )    //sorting in ascending order
                    temp[ k ] = ar[ i++ ];
                else
                    temp[ k ] = ar[ j++ ];
        }
        while ( i < nl )
            temp[ k++ ] = ar[ i++ ];
        while ( j < n )
            temp[ k++ ] = ar[ j++ ];
        for( i = 0; i < n; i++)       //copying back the sorted array into original array
            ar[ i ] = temp[ i ];
}

int main()
{
        char **strings;         //declaration of string array
        int n, i,  j,  min;
        scanf( "%d", &n );
        strings = ( char ** )malloc( n*sizeof( char * ) );      //allocating memory to n string pointers
        temp = ( char ** )malloc( n*sizeof( char * ) );
        for( i = 0; i < n; i++)
                scanf("%ms", &( strings[ i ] ) );       //%m is used when string size is unknown and memory is allocated dynamically according to the input from user.
        sort( strings, n );                               // passing string array to function sort
        for( i = 0; i < n; i++)                              //printing sorted array
                puts( strings[ i ]);
    return 0;
}


==========================================

In C++

#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int myfun(string s1, string s2)
{
    if(s1.length() < s2.length() || (s1.length() == s2.length() && s1 < s2))
        return 1;
    else
        return 0;
}
int main()
{
    int n;
    cin >> n;
    vector< string > s;
    for(int i = 0;i < n; i++)
    {
        string temp;
        cin >> temp;
        s.push_back(temp);
    }
    sort(s.begin(), s.end(), myfun);
    for(int i = 0;i < n; i++)
        cout << s[i]<<endl;
}

my fun function can be written as follows
int myfun( string s1, string s2 )
{
    return ( s1.length() == s2.length() )? s1 <  s2 : s1.length() < s2.length();
}
===========================
The below solution passed only 3 Test Cases and getting segmentation fault for remaining test cases

I used bubble sort which has more  time complexity and is not a efficient sorting algorithm

#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>

int main(){
    // Enter your code here. Read input from STDIN. Print output to STDOUT
    unsigned long long int n,i,j;
scanf("%llu",&n);
char a[n][1000],t[1000];
for(i=0;i<n;i++)
scanf("%s",a[i]);
for(i=0;i<n-1;i++)
{
for(j=0;j<n-i-1;j++)
{
if((strlen(a[j])>strlen(a[j+1])) || (strlen(a[j])==strlen(a[j+1]) && strcmp(a[j],a[j+1])>0))
{
strcpy(t,a[j]);
strcpy(a[j],a[j+1]);
strcpy(a[j+1],t);
}
}
}
for(i=0;i<n;i++)
printf("%s\n",a[i]);
 return 0;
}

No comments:

Post a Comment