Merging unsorted overlapping intervals

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] numArray = {6, 9, 10, 12, 8, 10, 0, 5 };
            //Sort the array based on starting point
            numArray = BubbleSort(numArray);
            ArrayList numArrayList = new ArrayList();
            numArrayList.AddRange(numArray);
            Console.WriteLine("Range before merging");
            Console.WriteLine("====================");
            for (int i = 0; i  " + numArrayList[i + 1]);
            }
            for (int i = 1; i = (int)numArrayList[i + 1]) && ((int)numArrayList[i] = (int)numArrayList[i + 1]) && ((int)numArrayList[i] >= (int)numArrayList[i + 2]))
                {
                    numArrayList.RemoveAt(i+1);
                    numArrayList.RemoveAt(i+1);
                    i = i - 2;
                }
            }
            Console.WriteLine("Range after merging");
            Console.WriteLine("===================");
            for (int i = 0; i  " + numArrayList[i + 1]);
            }
        }

        public static int[] BubbleSort(int[] numArray)
        {
            int temp1, temp2;
            for (int pass = 0; pass <= numArray.Length - 4; pass++)
            {
                for (int i = 0; i  numArray[i + 2])
                    {
                        temp1 = numArray[i];
                        temp2 = numArray[i + 1];
                        numArray[i] = numArray[i + 2];
                        numArray[i + 1] = numArray[i + 3];
                        numArray[i + 2] = temp1;
                        numArray[i + 3] = temp2;
                    }
                }
            }
            return numArray;
        }
    }
}

Output
======
Range before merging
====================
0 --> 5
6 --> 9
8 --> 10
10 --> 12
Range after merging
===================
0 --> 5
6 --> 12
Press any key to continue . . .
Advertisements
This entry was posted in Information Technology. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s