Function to insert an integer into a sorted array of integers
-
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?
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
-
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
-
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
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
-
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
-
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?
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