Click here to Skip to main content
15,895,656 members
Home / Discussions / C#
   

C#

 
QuestionFind the best sort algorithm Pin
ndkit31-Mar-10 20:39
ndkit31-Mar-10 20:39 
AnswerRe: Find the best sort algorithm Pin
Alex Manolescu31-Mar-10 21:32
Alex Manolescu31-Mar-10 21:32 
GeneralRe: Find the best sort algorithm Pin
ndkit31-Mar-10 21:42
ndkit31-Mar-10 21:42 
GeneralRe: Find the best sort algorithm Pin
Alex Manolescu1-Apr-10 0:34
Alex Manolescu1-Apr-10 0:34 
AnswerRe: Find the best sort algorithm Pin
dan!sh 31-Mar-10 21:45
professional dan!sh 31-Mar-10 21:45 
AnswerRe: Find the best sort algorithm Pin
riced1-Apr-10 1:25
riced1-Apr-10 1:25 
JokeRe: Find the best sort algorithm Pin
PIEBALDconsult1-Apr-10 6:58
mvePIEBALDconsult1-Apr-10 6:58 
AnswerRe: Find the best sort algorithm Pin
riced1-Apr-10 7:56
riced1-Apr-10 7:56 
Here's an implementation that does what I think you want.
Note: it uses Lists instead of Arrays. If you must use arrays then the output array must be big enough to hold all three input arrays.
Note: it relies on having a value (HIGH_VALUE) greater than any that can appear in any of the input arrays.
I've not done an analysis but don't think you can do it quicker. This goes through each array (List) only once. Any other approach probably needs multiple passes through the arrays.

using System;
using System.Collections.Generic;

namespace ConsoleApplication1
{
   class Program
   {
      static List<int> listA = new List<int>();
      static List<int> listB = new List<int>();
      static List<int> listC = new List<int>();
      static List<int> listOut = new List<int>();

      static void Main(string[] args)
      {
         MakeSortedLists();   //with test data for lists A, B and C
         const int HIGH_VALUE = int.MaxValue; //Dummy value greater than max in any list

         int m = 0;  // listOut index - only need this if listOut is really an array
         int i = 0;  // listA index
         int j = 0;  // listB index
         int k = 0;  // listC index

         int valueA = listA[0];
         int valueB = listB[0];
         int valueC = listC[0];

         while ((valueA < HIGH_VALUE) || (valueB < HIGH_VALUE) || (valueC < HIGH_VALUE))
         {
            if ((valueA <= valueB) && (valueA <= valueC))
            {
               listOut.Add(listA[i]);       //listOut[m] = listA[i] IF listOut is an array
               i++;
               valueA = (i < listA.Count) ? listA[i] : HIGH_VALUE;
            }
            else
            {
               if ((valueB <= valueA) && (valueB <= valueC))
               {
                  listOut.Add(listB[j]);    //listOut[m] = listB[j] IF listOut is an array
                  j++;
                  valueB = (j < listB.Count) ? listB[j] : HIGH_VALUE;
               }
               else
               {
                  listOut.Add(listC[k]);    //listOut[m] = listC[k] IF listOut is an array
                  k++;
                  valueC = (k < listC.Count) ? listC[k] : HIGH_VALUE;
               }
            }
            m++; // only need this if listOut is really an array
         }

         WriteOutList();
         Console.ReadLine();
      }

      static void MakeSortedLists()
      {
         listA.Add(10);
         listA.Add(11);
         listA.Add(2);
         listA.Add(1);
         listA.Add(16);

         listB.Add(5);
         listB.Add(13);
         listB.Add(7);

         listC.Add(10);
         listC.Add(6);
         listC.Add(16);

         listA.Sort();
         listB.Sort();
         listC.Sort();
      }

      static void WriteOutList()
      {
         Console.WriteLine("Posn    Value");
         Console.WriteLine("-------------");
         for (int m = 0; m < listOut.Count; m++)
         {
            Console.WriteLine("{0,3}  >> {1,4}", m, listOut[m]);
         }
      }
   }
}

Regards
David R
---------------------------------------------------------------
"Every program eventually becomes rococo, and then rubble." - Alan Perlis

GeneralRe: Find the best sort algorithm Pin
ndkit6-Apr-10 18:49
ndkit6-Apr-10 18:49 
Question(C#).Net Backup application for Sharepoint Pin
JurieBurie31-Mar-10 20:37
JurieBurie31-Mar-10 20:37 
QuestionHow to build C# program to solve 8-Puzzle with best first search algorithm ? Pin
nguyencuonginfo31-Mar-10 19:58
nguyencuonginfo31-Mar-10 19:58 
AnswerRe: How to build C# program to solve 8-Puzzle with best first search algorithm ? Pin
Khaniya31-Mar-10 20:20
professionalKhaniya31-Mar-10 20:20 
AnswerRe: How to build C# program to solve 8-Puzzle with best first search algorithm ? Pin
PIEBALDconsult31-Mar-10 20:26
mvePIEBALDconsult31-Mar-10 20:26 
GeneralRe: How to build C# program to solve 8-Puzzle with best first search algorithm ? Pin
J4amieC31-Mar-10 21:27
J4amieC31-Mar-10 21:27 
GeneralRe: How to build C# program to solve 8-Puzzle with best first search algorithm ? Pin
PIEBALDconsult1-Apr-10 3:32
mvePIEBALDconsult1-Apr-10 3:32 
AnswerRe: How to build C# program to solve 8-Puzzle with best first search algorithm ? Pin
nguyencuonginfo1-Apr-10 5:00
nguyencuonginfo1-Apr-10 5:00 
QuestionTimeout exception when reading from serial port Pin
mmdullah31-Mar-10 19:17
mmdullah31-Mar-10 19:17 
AnswerRe: Timeout exception when reading from serial port Pin
PIEBALDconsult31-Mar-10 19:24
mvePIEBALDconsult31-Mar-10 19:24 
GeneralRe: Timeout exception when reading from serial port Pin
mmdullah31-Mar-10 19:56
mmdullah31-Mar-10 19:56 
AnswerRe: Timeout exception when reading from serial port Pin
Alan N1-Apr-10 1:49
Alan N1-Apr-10 1:49 
QuestionRestrict User from changing time Pin
Aseem Sharma31-Mar-10 17:14
Aseem Sharma31-Mar-10 17:14 
AnswerRe: Restrict User from changing time Pin
Rod Kemp31-Mar-10 19:20
Rod Kemp31-Mar-10 19:20 
AnswerRe: Restrict User from changing time Pin
Luc Pattyn1-Apr-10 3:37
sitebuilderLuc Pattyn1-Apr-10 3:37 
QuestionGet Tittle Name for window form ? Pin
aa_zz31-Mar-10 15:38
aa_zz31-Mar-10 15:38 
AnswerRe: Get Tittle Name for window form ? [modified] Pin
Luc Pattyn31-Mar-10 16:28
sitebuilderLuc Pattyn31-Mar-10 16:28 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.