// Collection of code snippets by Arne Vajhøj // (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) using System; using System.Text; using System.Collections.Generic; namespace E { public class MainClass { public static string InsertValueInCommaList1(string s, int val) { string[] a = s.Split(','); for (int i = 1; i < a.Length - 1; i++) { if (int.Parse(a[i]) > val) { return s.Insert(s.IndexOf("," + a[i] + ","), "," + val); } } return s + val + ","; } public static string InsertValueInCommaList2(string s, int val) { SortedList lst = new SortedList(); string[] a = s.Split(','); for (int i = 1; i < a.Length - 1; i++) { int v = int.Parse(a[i]); lst.Add(v, v); } lst.Add(val, val); StringBuilder sb = new StringBuilder(); sb.Append(','); foreach (int v in lst.Keys) { sb.Append(v); sb.Append(','); } return sb.ToString(); } private static int FindLeadingComma(string s, int ix) { int res = ix; while(s[res] != ',') res--; return res; } private static int FindTrailingComma(string s, int ix) { int res = ix + 1; while(s[res] != ',') res++; return res; } private static int FindValue(string s, int ix) { int lead = FindLeadingComma(s, ix); int trail = FindTrailingComma(s, ix); return int.Parse(s.Substring(lead + 1, trail - lead - 1)); } public static string InsertValueInCommaList3(string s, int val) { if(s.Length <= 1) return "," + val + ","; int low = 1; if(FindValue(s, low) > val) return "," + val + s; int high = s.Length - 2; if(FindValue(s, high) < val) return s + val + ","; while(low < high) { int piv = (low + high) / 2; int pivval = FindValue(s, piv); if(pivval < val) { low = FindTrailingComma(s, piv); } else if(pivval > val) { high = FindLeadingComma(s, piv); } else { throw new ArgumentException(val + " already in list"); } } return s.Substring(0, low + 1) + val + s.Substring(high); } private const int N = 100; private const int REP = 1000; public static void Main(string[] args) { string s = ",1,2,3,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20," ; Console.WriteLine(InsertValueInCommaList1(s, 4)); Console.WriteLine(InsertValueInCommaList2(s, 4)); Console.WriteLine(InsertValueInCommaList3(s, 4)); Console.WriteLine(InsertValueInCommaList1(s, 0)); Console.WriteLine(InsertValueInCommaList2(s, 0)); Console.WriteLine(InsertValueInCommaList3(s, 0)); Console.WriteLine(InsertValueInCommaList1(s, 21)); Console.WriteLine(InsertValueInCommaList2(s, 21)); Console.WriteLine(InsertValueInCommaList3(s, 21)); int[] v = new int[N]; Random rng = new Random(); for(int i = 0; i < v.Length; i++) { int newv; do { newv = rng.Next(2*N); } while(Array.IndexOf(v, newv) >= 0); v[i] = newv; } long t1 = DateTime.Now.Ticks; for(int j = 0; j < REP; j++) { string s1 = ","; for(int i = 0; i < v.Length; i++) { s1 = InsertValueInCommaList1(s1, v[i]); } } long t2 = DateTime.Now.Ticks; Console.WriteLine(String.Format("1: {0,10}",(t2 - t1))); long t3 = DateTime.Now.Ticks; for(int j = 0; j < REP; j++) { string s2 = ","; for(int i = 0; i < v.Length; i++) { s2 = InsertValueInCommaList2(s2, v[i]); } } long t4 = DateTime.Now.Ticks; Console.WriteLine(String.Format("2: {0,10}",(t4 - t3))); long t5 = DateTime.Now.Ticks; for(int j = 0; j < REP; j++) { string s3 = ","; for(int i = 0; i < v.Length; i++) { s3 = InsertValueInCommaList3(s3, v[i]); } } long t6 = DateTime.Now.Ticks; Console.WriteLine(String.Format("3: {0,10}",(t6 - t5))); Console.ReadLine(); } } }