Created
July 25, 2012 07:07
-
-
Save snluu/3174862 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void MergeListImproved(ref List<int> list1, List<int> list2) | |
{ | |
int list1Size = list1.Count; | |
int list2Size = list2.Count; | |
int totalSize = list1Size + list2Size; | |
var newList = new List<int>(totalSize); | |
int index1 = 0; | |
int index2 = 0; | |
while (index1 < list1Size && index2 < list2Size) | |
if (list1[index1] < list2[index2]) | |
{ | |
newList.Add(list1[index1]); | |
index1++; | |
} | |
else | |
{ | |
newList.Add(list2[index2]); | |
index2++; | |
} | |
while (index1 < list1Size) | |
{ | |
newList.Add(list1[index1]); | |
index1++; | |
} | |
while (index2 < list2Size) | |
{ | |
newList.Add(list2[index2]); | |
index2++; | |
} | |
list1.Clear(); | |
list1.Capacity = totalSize; | |
list1.AddRange(newList); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment