Logo 
Search:

C Programming Articles

Submit Article
Home » Articles » C Programming » Data File StructureRSS Feeds

Sorting of dates using bubble sort

Posted By: Zak Brown     Category: C Programming     Views: 5555

Sorting of dates using bubble sort.

Code for Sorting of dates using bubble sort in C Programming

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

int d, m ;
longint y ;

void main( )
{
    char dates[5][12], temp[12] ;
    longint arr[5], t ;
    int i, j, valid ;

    int isvalid( ) ;
    longint getnum ( char * ) ;

    clrscr( ) ;

    printf ( "Enter any five dates (dd\\mm\\yyyy) :-\n" ) ;
    for ( i = 0 ; i <= 4 ; i++ )
    {
        printf("%d) ",i + 1 ) ;
        scanf ( "%s", dates[i] ) ;

        /* Converts date in string to equivalent integer values. */
arr[i] = getnum ( dates[i] ) ; /* Checks for valid date. */
valid = isvalid( ) ; if ( !valid ) { printf ( "This date does not exists. " ) ; printf ( "Enter any other date.\n" ) ; i-- ; } } /* Sorting of dates using Bubble sort method. */
for ( i = 0 ; i <= 3 ; i++ ) { for ( j = 0 ; j <= 3 - i ; j++ ) { if ( arr[j] > arr[j + 1] ) { t = arr[j] ; arr[j] = arr[j + 1] ; arr[j + 1] = t ; strcpy ( temp, dates[j] ) ; strcpy ( dates[j], dates[j + 1]) ; strcpy ( dates[j + 1], temp ) ; } } } printf ( "Dates in sorted order are :- \n" ) ; for ( i = 0 ; i <= 4 ; i++ ) printf ( "%s\n", dates[i] ) ; getch( ) ; } /* checks valid date */
int isvalid ( ) { int isleap ( longint ) ; if ( y <= 0 || m <= 0 || d <= 0 || m > 12 || y > 16384 || ( m == 2 && !isleap ( y ) && d > 28 ) || ( m == 2 && isleap ( y ) && d > 29 ) || ( ( m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12 ) && d > 31 ) || ( ( m == 4 || m == 6 || m == 9 || m == 11 ) && d > 30 ) ) return 0 ; elsereturn 1 ; } /* checks whether the given year is leap or not */
int isleap ( longint y ) { return ( y % 4 == 0 ) ^ ( y % 100 == 0 ) ^ ( y % 400 == 0 ) ; } /* converts dates in string into equivalent integer values */
longint getnum ( char *date ) { char *p, str[15] ; longint num = 0 ; strcpy ( str, date ) ; d = m = y = 0 ; p = strtok ( str, "\\" ) ; if ( p != NULL ) d = num = atoi ( p ) ; p = strtok(NULL, "\\"); if ( p != NULL ) m = atoi ( p ) ; num += m * 32 ; p = strtok(NULL, "\\"); if ( p != NULL ) y = atol ( p ) ; num += y * 512L ; return num ; }
  
Share: 


Didn't find what you were looking for? Find more on Sorting of dates using bubble sort Or get search suggestion and latest updates.

Zak Brown
Zak Brown author of Sorting of dates using bubble sort is from London, United Kingdom.
 
View All Articles

 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!