Sunday, 4 October 2015

How to efficiently sort a big list dates in 20’s

Given a big list of dates in 20’s, how to efficiently sort the list.
Example:
Input:
       Date arr[] = {{20,  1, 2014},
                    {25,  3, 2010},
                    { 3, 12, 2000},
                    {18, 11, 2001},
                    {19,  4, 2015},
                    { 9,  7, 2005}}

Output:
      Date arr[] = {{ 3, 12, 2000},
                    {18, 11, 2001},
                    { 9,  7, 2005},
                    {25,  3, 2010},
                    {20,  1, 2014},
                    {19,  4, 2015}}
We strongly recommend to minimize your browser and try this yourself first.
A Simple Solution is to use a O(nLogn) algorithm like Merge Sort. We can sort the list in O(n) time using Radix Sort. In a typical Radix Sort implementation, we first sort by last digit, then by second last digit, and so on. Here we sort in following order.
1) First sort by day using counting sort
2) Then sort by month using counting sort
3) Finally sort by year using counting sort
As the number of days, months and years are fixed, all three steps take O(n) time. Therefore, overall time complexity is O(n).
Below is C implementation of above idea. Note that the implementation doesn’t do any error handing to keep the code simple.
// C program to sort an array of dates using Radix Sort
#include <stdio.h>
struct Date
{
    int d, m, y;
};
 
// Prototypes
void countSortDay(Date arr[], int n);
void countSortMonth(Date arr[], int n);
void countSortYear(Date arr[], int n);
 
// The main function  that sorts array of dates
// using Radix Sort
void radixSortDates(Date arr[], int n)
{
   // First sort by day
   countSortDay(arr, n);
 
   // Then by month
   countSortMonth(arr, n);
 
   // Finally by year
   countSortYear(arr, n);
}
 
// A function to do counting sort of arr[] according to
// day
void countSortDay(Date arr[], int n)
{
    Date output[n]; // output array
    int i, count[31] = {0};
 
    // Store count of occurrences in count[]
    for (i=0; i<n; i++)
        count[arr[i].d - 1]++;
 
    // Change count[i] so that count[i] now contains
    // actual position of this day in output[]
    for (i=1; i<31; i++)
        count[i] += count[i-1];
 
    // Build the output array
    for (i=n-1; i>=0; i--)
    {
        output[count[arr[i].d - 1] - 1] = arr[i];
        count[arr[i].d - 1]--;
    }
 
    // Copy the output array to arr[], so that arr[] now
    // contains sorted numbers according to curent digit
    for (i=0; i<n; i++)
        arr[i] = output[i];
}
 
// A function to do counting sort of arr[] according to
// month.
void countSortMonth(Date arr[], int n)
{
    Date output[n]; // output array
    int i, count[12] = {0};
 
    for (i = 0; i < n; i++)
        count[arr[i].m - 1]++;
    for (i = 1; i < 12; i++)
        count[i] += count[i - 1];
    for (i=n-1; i>=0; i--)
    {
        output[count[arr[i].m - 1] - 1] = arr[i];
        count[arr[i].m - 1]--;
    }
    for (i = 0; i < n; i++)
        arr[i] = output[i];
}
 
// A function to do counting sort of arr[] according to
// year.
void countSortYear(Date arr[], int n)
{
    Date output[n]; // output array
    int i, count[1000] = {0};
    for (i = 0; i < n; i++)
        count[arr[i].y - 2000]++;
    for (i = 1; i < 1000; i++)
        count[i] += count[i - 1];
    for (i = n - 1; i >= 0; i--)
    {
        output[count[arr[i].y - 2000] - 1] = arr[i];
        count[arr[i].y - 2000]--;
    }
    for (i = 0; i < n; i++)
        arr[i] = output[i];
}
 
// A utility function to print an array
void print(Date arr[], int n)
{
    int i;
    for (i=0; i<n; i++)
        printf("{%2d, %2d, %d}\n",
               arr[i].d, arr[i].m, arr[i].y);
}
 
// Driver program to test above functions
int main()
{
    Date arr[] = {{20, 1, 2014}, {25, 3, 2010},
                  {3, 12, 2000}, {18, 11, 2001},
                  {19, 4, 2015}, {9, 7, 2005}};
    int n = sizeof(arr)/sizeof(arr[0]);
    printf("Input Dates\n");
    print(arr, n);
    radixSortDates(arr, n);
    printf("\nSorted Dates\n");
    print(arr, n);
    return 0;
}
Output:
Input Dates
{20,  1, 2014}
{25,  3, 2010}
{ 3, 12, 2000}
{18, 11, 2001}
{19,  4, 2015}
{ 9,  7, 2005}

Sorted Dates
{ 3, 12, 2000}
{18, 11, 2001}
{ 9,  7, 2005}
{25,  3, 2010}
{20,  1, 2014}
{19,  4, 2015} 

No comments:

Post a Comment