I am trying to find a way to sort a List as fast as possible. I know about "bucket sort" which I will use(I think?). But before doing that. I think I am looking for the fastest possible clean algorithm first, - and then use that in bucket sort?
The strings looks like below where I add 100.000 elements in a loop:
-0,awb/aje - ddfas/asa - asoo/qwa
-1,awb/aje - ddfas/asa - asoo/qwa
-2,awb/aje - ddfas/asa - asoo/qwa
So I want to sort in a descending order for the first comma delimited argument which is a double like: -0, -1, -2 etc..
I have tried 3 Approaches where only the Approach 1 actually sorts correctly. Approach 2 and 3 do not sort entirely correctly in a descending order numeric wise.
So the approach 1 which sorts correctly do this in 30 seconds.
The thing is that I will have about 3 Million elements and not only 100.000 like in this example which then should take at least 900 seconds or more.
My question is how can we sort 100.000 or more correctly 3 million elements as fast as possible?
Running: sortingtestBENCHMARKS() will show the results
public void sortingtestBENCHMARKS()
{
List<String> dataLIST = new List<String>(); List<String> sortedLIST = new List<String>(); String resultString = ""; int index = 0;
DateTime starttime = DateTime.Now; DateTime endtime = DateTime.Now; TimeSpan span = new TimeSpan();
for (double i = 0; i < 100000; i+=1)
{
dataLIST.Add("-" + i + "," + "awb/aje" + " - " + "ddfas/asa" + " - " + "asoo/qwa");
}
dataLIST = shuffle(dataLIST);
/*--------------------------------------------------------------------------*/
//APPROACH 1: 30 seconds (Sorts correctly in descending order)
starttime = DateTime.Now;
dataLIST = sortLIST(dataLIST);
endtime = DateTime.Now;
span = endtime - starttime;
resultString = "Approach 1: " + span.TotalSeconds;
dataLIST = shuffle(dataLIST);
/*--------------------------------------------------------------------------*/
//APPROACH 2: 55 seconds (Sorts INcorrectly in descending order)
starttime = DateTime.Now;
for (int i = 0; i < dataLIST.Count; i++)
{
index = sortedLIST.BinarySearch(dataLIST[i]);
if (index < 0)
{
sortedLIST.Insert(~index, dataLIST[i]);
}
}
endtime = DateTime.Now;
span = endtime - starttime;
resultString = resultString + "\nApproach 2: " + span.TotalSeconds;
/*--------------------------------------------------------------------------*/
//APPROACH 3: 2 seconds (Sorts INcorrectly in descending order)
starttime = DateTime.Now;
dataLIST.Sort(); //1.6 seconds
endtime = DateTime.Now;
span = endtime - starttime;
resultString = resultString + "\nApproach 3: " + span.TotalSeconds;
/*--------------------------------------------------------------------------*/
MessageBox.Show("Elapsed Times:\n\n" + resultString);
}
List<String> sortLIST(List<String> theLIST)
{
System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");
theLIST.Sort(new Comparison<String>((a, b) =>
{
int result = 0;
double ad = 0;
double bd = 0;
NumberFormatInfo provider = new NumberFormatInfo();
provider.NumberGroupSeparator = ",";
provider.NumberDecimalSeparator = ".";
provider.NumberGroupSizes = new int[] { 5 };
ad = Convert.ToDouble(a.Replace("a", "").Replace("c", "").Split(',')[0], provider);
bd = Convert.ToDouble(b.Replace("a", "").Replace("c", "").Split(',')[0], provider);
if (ad < bd)
{
result = 1;
}
else if (ad > bd)
{
result = -1;
}
return result;
}));
return theLIST;
}
List<String> shuffle(List<String> list)
{
var randomizedList = new List<String>();
var rnd = new Random();
while (list.Count != 0)
{
var index = rnd.Next(0, list.Count);
randomizedList.Add(list[index]);
list.RemoveAt(index);
}
return randomizedList;
}
It seems to me that you can just split your string on the , character, strip the - off the first item in the split array, and then use OrderBy on that result:
var sorted = dataLIST.OrderBy(i => double.Parse(i.Split(',')[0].TrimStart('-'))).ToList();
I made a copy of your code and then used the one working method you have and compared it to running an OrderBy on the split string method mentioned above. The OrderBy/Split method is a little more than 30 times faster.
public static void sortingtestBENCHMARKS()
{
var dataLIST = new List<string>();
// Create the list
for (var i = 0; i < 100000; i ++)
{
dataLIST.Add("-" + i + "," + "awb/aje" + " - " + "ddfas/asa" + " - " + "asoo/qwa");
}
// Shuffle the list
dataLIST = shuffle(dataLIST);
// Make two copies of the same shuffled list
var copy1 = dataLIST.ToList();
var copy2 = dataLIST.ToList();
// Use a stopwatch for measuring time when benchmark testing
var stopwatch = new Stopwatch();
/*--------------------------------------------------------------------------*/
//APPROACH 1: 2.83 seconds (Sorts correctly in descending order)
stopwatch.Start();
copy2 = sortLIST(copy2);
stopwatch.Stop();
Console.WriteLine($"sortLIST method: {stopwatch.Elapsed.TotalSeconds} seconds");
/*--------------------------------------------------------------------------*/
//APPROACH 2: 0.09 seconds (Sorts correctly in descending order)
stopwatch.Restart();
copy1 = copy1.OrderBy(i => double.Parse(i.Split(',')[0].TrimStart('-'))).ToList();
stopwatch.Stop();
Console.WriteLine($"OrderBy method: {stopwatch.Elapsed.TotalSeconds} seconds");
// Ensure that the lists are sorted identically
Console.WriteLine($"Lists are the same: {copy1.SequenceEqual(copy2)}");
}
Output
var sortedList = theLIST.OrderByDescending(s=>s);
You should optimize the inner loop as hard as you can, because most of the processing time is spent there. I suggest implementing the string tokenizer yourself, since you only need the first token and the string is very uniform. The second optimization you might have is multiply all the number with -1 so sorting the list in reverse order is straightforward. Something like this:
private static double getNumberFromString(String s){
int posFirstComma=0;
for (; posFirstComma<s.length() && s.charAt(posFirstComma)!=','; posFirstComma++);
return Convert.toDouble(s.subString(0, posFirstComma)*(-1);
}
myData.sort(new Comparision<String>((a,b)=> getNumberFromString(a)-getNumberFromString(b));
I personally won't touch the sort algorithm in the library itself because it is already thoroughly optimized. Just optimize everything in the for loop.
I want to draw a random name for an event where users can win items specified by their ID (1,2,3 etc.). The random name part is ok by now but how can i display the result like:
The ID : 1 winner is: 'the random name'
The ID : 2 winner is: 'the random name'
etc...
till ID : 27
static void Main(string[] args)
{
string[] Names = { "Erik", "Levente", "Noel", "Áron", "Krisztián", "Kristóf", "Bence", "Roland", "Máté", "László", "Bálint" ,
"Regina", "Brigitta", "Gréta", "Hédi", "Hanna", "Boglárka", "Jázmin", "Réka", "Alexandra", "Rebeka", "Lili", "Luca", "Zsófi"};
List<string> alreadyUsed = new List<string>();
Random r = new Random();
while (alreadyUsed.Count < Names.Length)
{
int index = r.Next(0, Names.Length);
if (!alreadyUsed.Contains(Names[index]))
{
alreadyUsed.Add(Names[index]);
Console.WriteLine("The ID : 1 winner is: " + Names[index]);
}
}
Console.ReadKey(true);
}
Here is a refactored appraoch without that bad performing alreadyUsed. First I randomize the array, second I iterate and display each item with a incrementing index / id.
string[] Names = { "Erik", "Levente", "Noel", "Áron", "Krisztián", "Kristóf", "Bence", "Roland", "Máté", "László", "Bálint" , "Regina", "Brigitta", "Gréta", "Hédi", "Hanna", "Boglárka", "Jázmin", "Réka", "Alexandra", "Rebeka", "Lili", "Luca", "Zsófi"};
Random r = new Random();
Names = Names.OrderBy(_ => r.Next()).ToArray();
for(int i=0;i< Names.Length;i++)
{
Console.WriteLine("The ID : " + (i+1) + " winner is: " + Names[i]);
}
while (alreadyUsed.Count < Names.Length)
{
int index = r.Next(0, Names.Length);
if (!alreadyUsed.Contains(Names[index]))
{
alreadyUsed.Add(Names[index]);
Console.WriteLine("The ID : " + alreadyUsed.Count + " winner is: " + Names[index]);
}
}
You do not need ++i, you already have the position contained in alreadyUsed.Count that will increment itself as you use alreadyUsed.Add(...).
Try it online
I want to build a sequence randomiser mobile app in c#. I would like to retrieve the smallest and the largest number in an interval from two diffrent text boxes, and after I click the Generate button to display the random sequence of all the numbers in a new text box.
My code only displays one number. What's wrong with it?
Thanks.
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
}
private void button_Click(object sender, RoutedEventArgs e)
{
int seed = 345;
var result = "";
int min = Convert.ToInt32(textBox.Text);
int max = Convert.ToInt32(textBox2.Text);
Random r3 = new Random(seed);
for (int i = min; i < max; i++)
{
ecran.Text = (/*"," + r3.Next(min, max)*/i).ToString();
}
}
To clarify what was wrong with your solution:
Inside the loop you were constantly reassigning the value of ecran.Text.
i.e.
1st loop cycle > ecran.Text = ", " + 77
2nd loop cycle > ecran.Text = ", " + 89
//Value of ecran.Text after 1st cycle is ", 77"
//Value of ecran.Text after 2nd cycle is ", 89"
Overriding the value of ecran.Text with each iteration.
Fixed by adding a plus symbol in front of equals ecran.Text += ", " + LOGIC
This happens because you assign sequence values to ecran.Text in a loop. Instead, you should create a string representation of the sequence, and assign it at the end.
Use Shuffle<T> method from this Q&A:
int min = Convert.ToInt32(textBox.Text);
int max = Convert.ToInt32(textBox2.Text);
if (max < min) return;
var sequence = Enumerable.Range(min, max-min+1).ToList();
Shuffle(sequence);
ecran.Text = string.Join(",", sequence);
Please someone to help me to parse these sample string below? I'm having difficulty to split the data and also the data need to add carriage return at the end of every event
sample string:
L,030216,182748,00,FF,I,00,030216,182749,00,FF,I,00,030216,182750,00,FF,I,00
batch of events
expected output:
L,030216,182748,00,FF,I,00 - 1st Event
L,030216,182749,00,FF,I,00 - 2nd Event
L,030216,182750,00,FF,I,00 - 3rd Event
Seems like an easy problem. Something as easy as this should do it:
string line = "L,030216,182748,00,FF,I,00,030216,182749,00,FF,I,00,030216,182750,00,FF,I,00";
string[] array = line.Split(',');
StringBuilder sb = new StringBuilder();
for(int i=0; i<array.Length-1;i+=6)
{
sb.AppendLine(string.Format("{0},{1} - {2} event",array[0],string.Join(",",array.Skip(i+1).Take(6)), "number"));
}
output (sb.ToString()):
L,030216,182748,00,FF,I,00 - number event
L,030216,182749,00,FF,I,00 - number event
L,030216,182750,00,FF,I,00 - number event
All you have to do is work on the function that increments the ordinals (1st, 2nd, etc), but that's easy to get.
This should do the trick, given there are no more L's inside your string, and the comma place is always the sixth starting from the beginning of the batch number.
class Program
{
static void Main(string[] args)
{
String batchOfevents = "L,030216,182748,00,FF,I,00,030216,182749,00,FF,I,00,030216,182750,00,FF,I,00,030216,182751,00,FF,I,00,030216,182752,00,FF,I,00,030216,182753,00,FF,I,00";
// take out the "L," to start processing by finding the index of the correct comma to slice.
batchOfevents = batchOfevents.Substring(2);
String output = "";
int index = 0;
int counter = 0;
while (GetNthIndex(batchOfevents, ',', 6) != -1)
{
counter++;
if (counter == 1){
index = GetNthIndex(batchOfevents, ',', 6);
output += "L, " + batchOfevents.Substring(0, index) + " - 1st event\n";
batchOfevents = batchOfevents.Substring(index + 1);
} else if (counter == 2) {
index = GetNthIndex(batchOfevents, ',', 6);
output += "L, " + batchOfevents.Substring(0, index) + " - 2nd event\n";
batchOfevents = batchOfevents.Substring(index + 1);
}
else if (counter == 3)
{
index = GetNthIndex(batchOfevents, ',', 6);
output += "L, " + batchOfevents.Substring(0, index) + " - 3rd event\n";
batchOfevents = batchOfevents.Substring(index + 1);
} else {
index = GetNthIndex(batchOfevents, ',', 6);
output += "L, " + batchOfevents.Substring(0, index) + " - " + counter + "th event\n";
batchOfevents = batchOfevents.Substring(index + 1);
}
}
output += "L, " + batchOfevents + " - " + (counter+1) + "th event\n";
Console.WriteLine(output);
}
public static int GetNthIndex(string s, char t, int n)
{
int count = 0;
for (int i = 0; i < s.Length; i++)
{
if (s[i] == t)
{
count++;
if (count == n)
{
return i;
}
}
}
return -1;
}
}
Now the output will be in the format you asked for, and the original string has been decomposed.
NOTE: the getNthIndex method was taken from this old post.
If you want to split the string into multiple strings, you need a set of rules,
which are implementable. In your case i would start splitting the complete
string by the given comma , and than go though the elements in a loop.
All the strings in the loop will be appended in a StringBuilder. If your ruleset
say you need a new line, just add it via yourBuilder.Append('\r\n') or use AppendLine.
EDIT
Using this method, you can also easily add new chars like L or at the end rd Event
Look for the start index of 00,FF,I,00 in the entire string.
Extract a sub string starting at 0 and index plus 10 which is the length of the characters in 1.
Loop through it again each time with a new start index where you left of in 2.
Add a new line character each time.
Have a try the following:
string stream = "L,030216,182748,00,FF,I,00, 030216,182749,00,FF,I,00, 030216,182750,00,FF,I,00";
string[] lines = SplitLines(stream, "L", "I", ",");
Here the SplitLines function is implemented to detect variable-length events within the arbitrary-formatted stream:
string stream = "A;030216;182748 ;00;FF;AA;01; 030216;182749;AA;02";
string[] lines = SplitLines(batch, "A", "AA", ";");
Split-rules are:
- all elements of input stream are separated by separator(, for example).
- each event is bounded by the special markers(L and I for example)
- end marker is previous element of event-sequence
static string[] SplitLines(string stream, string startSeq, string endLine, string separator) {
string[] elements = stream.Split(new string[] { separator }, StringSplitOptions.RemoveEmptyEntries);
int pos = 0;
List<string> line = new List<string>();
List<string> lines = new List<string>();
State state = State.SeqStart;
while(pos < elements.Length) {
string current = elements[pos].Trim();
switch(state) {
case State.SeqStart:
if(current == startSeq)
state = State.LineStart;
continue;
case State.LineStart:
if(++pos < elements.Length) {
line.Add(startSeq);
state = State.Line;
}
continue;
case State.Line:
if(current == endLine)
state = State.LineEnd;
else
line.Add(current);
pos++;
continue;
case State.LineEnd:
line.Add(endLine);
line.Add(current);
lines.Add(string.Join(separator, line));
line.Clear();
state = State.LineStart;
continue;
}
}
return lines.ToArray();
}
enum State { SeqStart, LineStart, Line, LineEnd };
f you want to split the string into multiple strings, you need a set of rules, which are implementable. In your case i would start splitting the complete string by the given comma , and than go though the elements in a loop. All the strings in the loop will be appended in a StringBuilder. If your ruleset say you need a new line, just add it via yourBuilder.Append('\r\n') or use AppendLine.
Good day. I would like to ask if it is possible to concatenate 2 strings to get another variable.
Lets say I have this code:
string num1 = "abcdefgh";
string num2 = "ijklmnop";
int numLength = 0;
And I want to get the value of both num1 and num2 using a forloop
for(int i =1; i<= 2; i++)
{
numLength = ("num" + i).Length + numLength;
}
Console.WriteLine("Length is {0}", numLength);
I want it to output
Length is 16
I did the above code but it actually gives me different value.
Edit1: (P.S. I will be using more than 10 variables, I just indicated 2 of it to make it simple)
Edit2: Yes, yes. I want ("num"+i).Length to give me num1.Legnth + num2.Length.
First way:
I suggest you to add all of your strings into the List and then get the total length with Sum method.
List<string> allStrings = new List<string>();
allStrings.Add(num1);
allStrings.Add(num2);
...
allStrings.Add(num10);
var totalLength = allStrings.Sum(x => x.Length);
Second way
Or if you want to calculate total length with for loop:
int totalLength = 0;
for (int i = 0; i < allStrings.Count; i++)
{
totalLength = totalLength + allStrings[i].Length;
}
Third way
If you don't want to use List, then you can use String.Concat and then Length property.
var totalLength = String.Concat(num1, num2).Length;
The result is 16 in your case.
Edit:
In my opinion you think that, ("num" + i).Length will give you num1.Length and num2.Length. This is wrong.
Lets say we have some strings and we want the total length for all this strings.
In this case you need to store all strings in an array, so you can counter them and use indexes.
and after that a simple for (or foreach) loop can solve the problem:
string[] texts = new string[20]; //You can use any number u need, but in my code I wrote 20.
texts[0] = "sample text 1";
texts[1] = "sample text 2";
// add your strings ...
int totalLenght = 0;
foreach (string t in texts)
{
totalLenght += t.Length;
}
Console.WriteLine("Length is {0}", totalLenght);
If you need a variable with unlimited size, use List<T>
here is example:
List<string> texts = new List<string>();
texts.Add("sample text 1");
texts.Add("sample text 2");
// add your strings ....
int totalLenght = 0;
for (int i = 0; i < texts.Count; i++)
{
totalLenght += texts[i].Length;
}
Console.WriteLine("Length is {0}", totalLenght);