Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Function to insert an integer into a sorted array of integers

Function to insert an integer into a sorted array of integers

Scheduled Pinned Locked Moved C#
csharptutorialdata-structureshelpquestion
6 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Samiullah
    wrote on last edited by
    #1

    Hi, As i want the small example on how to write a function to insert an integer into a sorted array of integers using c#.Net. So can anybody help me in this regard?

    Z realJSOPR 2 Replies Last reply
    0
    • S Samiullah

      Hi, As i want the small example on how to write a function to insert an integer into a sorted array of integers using c#.Net. So can anybody help me in this regard?

      Z Offline
      Z Offline
      zafersavas
      wrote on last edited by
      #2

      Try this code;

      public int[] InsertToArray(int[] arr, int myInt)
      {
      int index = 0;

              `// Find index for the new int`
              if (myInt <= arr\[0\]) `// Place at the front`
                  index = -1;
              else if (myInt >= arr\[arr.Length -1\]) `// Place at the end`
                  index = arr.Length-1;
              else
              {
                  for (int k = 0; k < arr.Length - 1; k++)
                      if (arr\[k\] <= myInt && myInt <= arr\[k + 1\])
                          index = k;
              }
      
              `// Place the new int`
              int\[\] newArr = new int\[arr.Length + 1\];
              Array.Copy(arr, 0, newArr, 0, index + 1);
              newArr\[index+1\] = myInt;
              Array.Copy(arr, index + 1, newArr, index + 2, arr.Length - index - 1);
      
              return newArr;
          }
      

      zafer

      S P O 3 Replies Last reply
      0
      • Z zafersavas

        Try this code;

        public int[] InsertToArray(int[] arr, int myInt)
        {
        int index = 0;

                `// Find index for the new int`
                if (myInt <= arr\[0\]) `// Place at the front`
                    index = -1;
                else if (myInt >= arr\[arr.Length -1\]) `// Place at the end`
                    index = arr.Length-1;
                else
                {
                    for (int k = 0; k < arr.Length - 1; k++)
                        if (arr\[k\] <= myInt && myInt <= arr\[k + 1\])
                            index = k;
                }
        
                `// Place the new int`
                int\[\] newArr = new int\[arr.Length + 1\];
                Array.Copy(arr, 0, newArr, 0, index + 1);
                newArr\[index+1\] = myInt;
                Array.Copy(arr, index + 1, newArr, index + 2, arr.Length - index - 1);
        
                return newArr;
            }
        

        zafer

        S Offline
        S Offline
        Samiullah
        wrote on last edited by
        #3

        Hi Zafer, Thank you for your assistance. Regards, Sami.

        1 Reply Last reply
        0
        • Z zafersavas

          Try this code;

          public int[] InsertToArray(int[] arr, int myInt)
          {
          int index = 0;

                  `// Find index for the new int`
                  if (myInt <= arr\[0\]) `// Place at the front`
                      index = -1;
                  else if (myInt >= arr\[arr.Length -1\]) `// Place at the end`
                      index = arr.Length-1;
                  else
                  {
                      for (int k = 0; k < arr.Length - 1; k++)
                          if (arr\[k\] <= myInt && myInt <= arr\[k + 1\])
                              index = k;
                  }
          
                  `// Place the new int`
                  int\[\] newArr = new int\[arr.Length + 1\];
                  Array.Copy(arr, 0, newArr, 0, index + 1);
                  newArr\[index+1\] = myInt;
                  Array.Copy(arr, index + 1, newArr, index + 2, arr.Length - index - 1);
          
                  return newArr;
              }
          

          zafer

          P Offline
          P Offline
          PIEBALDconsult
          wrote on last edited by
          #4

          Creating a new array seems rather inefficient. Instead you should test to see if the value will fit and only extend the array (or throw an Exception) if the new value won't fit. Edit: Oh, right, an array doesn't keep track of how many items you have in it... you'll need to track that on your own.

          modified on Saturday, August 23, 2008 10:44 AM

          1 Reply Last reply
          0
          • Z zafersavas

            Try this code;

            public int[] InsertToArray(int[] arr, int myInt)
            {
            int index = 0;

                    `// Find index for the new int`
                    if (myInt <= arr\[0\]) `// Place at the front`
                        index = -1;
                    else if (myInt >= arr\[arr.Length -1\]) `// Place at the end`
                        index = arr.Length-1;
                    else
                    {
                        for (int k = 0; k < arr.Length - 1; k++)
                            if (arr\[k\] <= myInt && myInt <= arr\[k + 1\])
                                index = k;
                    }
            
                    `// Place the new int`
                    int\[\] newArr = new int\[arr.Length + 1\];
                    Array.Copy(arr, 0, newArr, 0, index + 1);
                    newArr\[index+1\] = myInt;
                    Array.Copy(arr, index + 1, newArr, index + 2, arr.Length - index - 1);
            
                    return newArr;
                }
            

            zafer

            O Offline
            O Offline
            oobimoo
            wrote on last edited by
            #5

            And perhaps copy to the new array while searching for the index.

            1 Reply Last reply
            0
            • S Samiullah

              Hi, As i want the small example on how to write a function to insert an integer into a sorted array of integers using c#.Net. So can anybody help me in this regard?

              realJSOPR Offline
              realJSOPR Offline
              realJSOP
              wrote on last edited by
              #6

              Use a generic list, add all of your elements, and then call the list Sort() method.

              "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
              -----
              "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups