Bubble sort string array c# - c#

I have this project where I have to sort some txt files, most of them are numbers but one of them is a collection of months. I have the code that sorts the others but not the file containing the months. So i need this code to be changed so I can sort a string array any advice would be brilliant thank you!
public void SortArray(decimal[] numbers)
{
bool swap;
decimal temp;
do
{
swap = false;
for(int index = 0; index < (numbers.Length - 1); index ++)
{
if ( numbers[index] > numbers[index+1])
{
//swap
temp = numbers[index];
numbers[index] = numbers[index + 1];
numbers[index + 1] = temp;
swap = true;
}
}
} while (swap == true);
}

If you have an string array like this:
string[] s = {"bbb", "ccc", "aaa"};
The shorter way to sort it, using:
Array.Sort(s);
and the long way to sort it, using:
for (var i = 1; i < s.Length; i++)
{
for (var j = 0; j < s.Length - i; j++)
{
if (string.Compare(s[j], s[j + 1], StringComparison.Ordinal) <= 0) continue;
var temp = s[j];
s[j] = s[j + 1];
s[j + 1] = temp;
}
}

public void BubbleSortArrayString(string[] letters) //change here
{
bool swap;
string temp; //change this too
do
{
swap = false;
for (int index = 0; index < (letters.Length - 1); index++)
{
if (letters[index] > letters[index + 1]) //if first number is greater then second then swap
{
//swap
temp = letters[index];
letters[index] = letters[index + 1];
letters[index + 1] = temp;
swap = true;
}
}
} while (swap == true);
}
used this code... forgot some answered last time i tried this

Related

LCS C# Algorithm - Get the deleted lines and added lines in previous and current text

I have to rewrite the LCS algorithm because some company policies.
I've already get done the LCS algorithm, but next step is to identify which lines were removed from the previous text and which one were added in the current text.
I tried a simple check thought the lines, but it won't work if I got a text with duplicated lines.
He is my code:
LCS Method
private static string[] LcsLineByLine(string previous, string current)
{
string[] Previous = previous.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
string[] Current = current.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
string lcsResult = string.Empty;
int a = Previous.Length;
int b = Current.Length;
int[,] table = new int[a + 1, b + 1];
//create a table with first line and column equal 0
for (int i = 0; i <= a; i++)
table[i, 0] = 0;
for (int j = 0; j <= b; j++)
table[0, j] = 0;
//create a table matrix
for (int i = 1; i <= a; i++)
{
for (int j = 1; j <= b; j++)
{
if (string.Equals(Previous[i - 1].Trim(), Current[j - 1].Trim(), StringComparison.InvariantCultureIgnoreCase))
{
table[i, j] = table[i - 1, j - 1] + 1;
}
else
{
table[i, j] = Math.Max(table[i, j - 1], table[i - 1, j]);
}
}
}
//get the lcs string array with the differences
int index = table[a, b];
string[] lcs = new string[index + 1];
lcs[index] = "0";
while (a > 0 && b > 0)
{
if (string.Equals(Previous[a - 1].Trim(), Current[b - 1].Trim(), StringComparison.InvariantCultureIgnoreCase))
{
lcs[index - 1] = Previous[a - 1].Trim();
a--;
b--;
index--;
}
else if (table[a - 1, b] > table[a, b - 1])
a--;
else
b--;
}
return lcs;
}
And this is the code that is not working with duplicated lines with same value.
Method to get all deleted items in the previous text:
private List<DiffItem> GetDiffPrevious(string[] previous, string[] diff)
{
List<DiffItem> differences = new List<DiffItem>();
//check items deleted
int line = 0;
for (int i = 0; i < previous.Length; i++)
{
bool isAbsent = false;
for (int j = 0; j < diff.Length; j++)
{
if (string.Equals(previous[i].Trim(), diff[j].Trim(), StringComparison.InvariantCultureIgnoreCase))
{
differences.Add(new DiffItem() { Position = line, Text = diff[j], Status = DiffStatus.Equal });
line++;
isAbsent = false;
break;
}
else
{
isAbsent = true;
}
}
//mark as deleted
if (isAbsent)
{
differences.Add(new DiffItem() { Position = line, Text = previous[i].Trim(), Status = DiffStatus.Deleted });
line++;
}
}
return differences;
}
If anyone could help me or any feedback would be great. Just a reminder, I cannot use third party libraries.
Thanks in advance.
I found the solution!
Basically, I rewrite the two lists and translated using Hashtable, so all the values will be unique by line. Then, I use the method LCS and got the result as expected.
I hope it helps somebody.

char 2D array compression

There is a .txt file what I have to read, compress and make an output txt for the compressed txt. Could anyone tell me what should I fix in my code?
My code:
namespace Tomorites
{
class Compression
{
public void Compress(char[,] source)
{
for (int i = 0; i < source.GetLength(0); i++)
{
int white = 0;
int red = 0;
for (int j = 0; j < source.GetLength(1); j++)
{
if (source[i, j] == 'P')
{
if (source[i, j] > 0)
{
red++;
Console.Write(red + " P ");
}
}
else if(source[i,j]=='F')
{
if (source[i, j] > 0)
{
white++;
Console.Write(white + " F ");
}
}
}
Console.WriteLine();
}
}
}
}
My console output
Source txt file
The compressed txt file which has to be
You was pretty close, but messed one important thing: reset first char counter when second char appear or second char counter when first char appear.
Also source[i, j] > 0 condition do nothing for you.
So at result we may have:
Fill source array:
static void Main(string[] args)
{
// Declare array
char[,] source = new char[7, 10];
// Fill array as at example
for (int i = 0; i < source.GetLength(0); i++)
for (int j = 0; j < source.GetLength(1); j++)
source[i, j] = i == 3 || j == 3 ? 'F' : 'P';
// Print filled array to Console
Console.WriteLine("Source:\n" + new string('-', 10));
for (int i = 0; i < source.GetLength(0); i++)
{
for (int j = 0; j < source.GetLength(1); j++)
Console.Write(source[i, j] + " ");
Console.WriteLine();
}
// Just new line
Console.WriteLine();
// Now "compress"
Console.WriteLine("Compressed:\n" + new string('-', 10));
Compress(source);
Console.ReadKey();
}
Source output:
Compress source array:
static void Compress(char[,] source)
{
// Declare counters for P and F chars
int PCount = 0, FCount = 0;
for (int i = 0; i < source.GetLength(0); i++)
{
for (int j = 0; j < source.GetLength(1); j++)
{
if (source[i, j] == 'P')
{
// If char is P - then count P's up
PCount++;
// If there was F char counted before (> 0) - print it and reset counter
if (FCount > 0)
{
Console.Write(FCount + " F ");
FCount = 0;
}
}
else if (source[i, j] == 'F')
{
// If char is P - then count F's up
FCount++;
// If there was P char counted before (> 0) - print it and reset counter
if (PCount > 0)
{
Console.Write(PCount + " P ");
PCount = 0;
}
}
}
// Before go to next "row" - print remained P or F counts and again, reset counters
if (PCount > 0)
{
Console.Write(PCount + " P ");
PCount = 0;
}
else if (FCount > 0)
{
Console.Write(FCount + " F ");
FCount = 0;
}
// Go new "row"
Console.WriteLine();
}
}
Compressed output:
UPD.
My answer is just solution for a provided example source and example output. #Dmitry Bychenko's solution is universal, not dependent on any two specific characters so in general use I recommend his way.
You can try something like this (you can fiddle with the code):
public static string Compress(char[,] source) {
if (null == source || source.GetLength(0) <= 0 || source.GetLength(1) <= 0)
return "";
StringBuilder sb = new StringBuilder();
for (int row = 0; row < source.GetLength(0); ++row) {
if (sb.Length > 0)
sb.AppendLine();
int count = 0;
char prior = '\0';
bool left = true;
for (int col = 0; col < source.GetLength(1); ++col) {
if (count == 0 || prior == source[row, col])
count += 1;
else {
if (!left)
sb.Append(' ');
left = false;
sb.Append($"{count} {prior}");
count = 1;
}
prior = source[row, col];
}
if (!left)
sb.Append(' ');
sb.Append($"{count} {prior}");
}
return sb.ToString();
}
Demo:
char[,] test = new char[,] {
{'a', 'a', 'a', 'a'},
{'a', 'b', 'c', 'c'},
};
Console.WriteLine(Compress(test));
Outcome:
4 a
1 a 1 b 2 c

Bubble sort 2D string array

How can I bubble sort a 2D string array by their lenght? In the array's zeroth column there are random generated messages and in the first column there are random generated priorities.
string[,] array = new string[50, 2];
Random r = new Random();
int number = 0;
int space = 0;
double fontossag = 0;
for (int i = 0; i < 50; i++)
{
string message = "";
int hossz = r.Next(10,51);
for (int h = 0; h < hossz; h++)
{
number = r.Next(0,101);
space = r.Next(0, 101);
if (number<=50)
{
message += (char)r.Next(97,122);
}
else if(number >= 50)
{
message += (char)r.Next(65, 90);
}
if (space<=10)
{
message += " ";
}
}
for (int f = 0; f < 50; f++)
{
fontossag = r.NextDouble() * (10.0);
}
array[i, 0] += message;
array[i, 1] += fontossag;
}
I want to sort the array by the random generated messages length.
This is my method to Bubble sort on the first column length:
public static string[,] BubbleSortStringByLength(string[,] array)
{
int num = array.GetLength(0);
for (int i = 0; i < num - 1; i++)
{
for (int j = 0; j < num - i - 1; j++)
{
if (array[j, 0].Length > array[j + 1, 0].Length)
{
// swap first column
string tmp = array[j, 0];
array[j, 0] = array[j + 1, 0];
array[j + 1, 0] = tmp;
// swap second column
tmp = array[j, 1];
array[j, 1] = array[j + 1, 1];
array[j + 1, 1] = tmp;
}
}
}
return array;
}
You can download the Visual Studio solution on GitHub
So you want to compare lengths of 1st columns and swap rows to ensure descending priority:
for (bool hasWork = true; hasWork;) {
hasWork = false;
for (int row = 0; row < array.GetLength(0) - 1; ++row) {
int priority1 = array[row, 0]?.Length ?? -1;
int priority2 = array[row + 1, 0]?.Length ?? -1;
// if we have wrong order...
if (priority1 < priority2) {
// we should keep on working to sort the array
hasWork = true;
// and swap incorrect rows
for (int column = 0; column < array.GetLength(1); ++column)
(array[row, column], array[row + 1, column]) =
(array[row + 1, column], array[row, column]);
}
}
}
Please, fiddle yourself

How to fix my bubble sort code for my float array from a text file containing decimal values

I'm using the bubble sort algorithm to sort a list of float values obtained from a text file. This bubble sort code works for other arrays but not for this one; it only displays the first value in the text file.
I have used this bubble sort on existing arrays and it works but it does not work for my float array which i have converted from a string. Why does this not work?`
while(line != null)
{
int temp = 0;
for (int write = 0; write < x.Length; write++)
{
for (int sort = 0; sort < x.Length - 1; sort++)
{
if (x[sort] > x[sort + 1])
{
temp = (int)x[sort + 1];
x[sort + 1] = x[sort];
x[sort] = temp;
}
}
}
for (int i = 0; i < x.Length; i += 1)
{
Console.Write(x[i] + " ");
}
line = br.ReadLine();
Console.ReadKey();
}
Here is the code for converting the string to the x value:
string line = br.ReadLine();
float [] x = Array.ConvertAll(line.Split(','), float.Parse);
Here is the code executing the file conversion:
using (StreamReader br = new StreamReader("file.txt"))
Other than the fact that you can get the array in order with one line (Array.Sort(x), which uses the more efficient Quicksort), what you have there (1) isn't a bubble sort and (2) will only work correctly for integers. Your temporary swap variable is an integer, so values will always be converted to integer when swapping. You need something like:
public class BubbleSort<T> where T : IComparable<T>
{
public static T[] Sort(T[] array)
{
for (var i = array.Length - 1; i > 0; i--)
{
for (var j = 0; j < i; j++)
{
if (((IComparable<T>)array[j]).CompareTo(array[j + 1]) > 0)
{
var swapVar = array[j + 1];
array[j + 1] = array[j];
array[j] = swapVar;
}
}
}
return array;
}
}
Use it like this:
BubbleSort<float>.Sort(x);
after you've loaded your array (X) into memory. Note that it can sort an array of anything that can be compared against its self (float in this case).
Modifying things slightly:
public static class BubbleSort
{
public static T[] Sort<T>(T[] array) where T : IComparable<T>
{
for (var i = array.Length - 1; i > 0; i--)
{
for (var j = 0; j < i; j++)
{
if (((IComparable<T>)array[j]).CompareTo(array[j + 1]) > 0)
{
var swapVar = array[j + 1];
array[j + 1] = array[j];
array[j] = swapVar;
}
}
}
return array;
}
}
Will infer the type of the array automatically so
BubbleSort.Sort(x);
will work

Search for an Array or List in a List

Have
List<byte> lbyte
Have
byte[] searchBytes
How can I search lbyte for not just a single byte but for the index of the searchBytes?
E.G.
Int32 index = lbyte.FirstIndexOf(searchBytes);
Here is the brute force I came up with.
Not the performance I am looking for.
public static Int32 ListIndexOfArray(List<byte> lb, byte[] sbs)
{
if (sbs == null) return -1;
if (sbs.Length == 0) return -1;
if (sbs.Length > 8) return -1;
if (sbs.Length == 1) return lb.FirstOrDefault(x => x == sbs[0]);
Int32 sbsLen = sbs.Length;
Int32 sbsCurMatch = 0;
for (int i = 0; i < lb.Count; i++)
{
if (lb[i] == sbs[sbsCurMatch])
{
sbsCurMatch++;
if (sbsCurMatch == sbsLen)
{
//int index = lb.FindIndex(e => sbs.All(f => f.Equals(e))); // fails to find a match
IndexOfArray = i - sbsLen + 1;
return;
}
}
else
{
sbsCurMatch = 0;
}
}
return -1;
}
Brute force is always an option. Although slow in comparison to some other methods, in practice it's usually not too bad. It's easy to implement and quite acceptable if lbyte isn't huge and doesn't have pathological data.
It's the same concept as brute force string searching.
You may find Boyer-Moore algorithm useful here. Convert your list to an array and search. The algorithm code is taken from this post.
static int SimpleBoyerMooreSearch(byte[] haystack, byte[] needle)
{
int[] lookup = new int[256];
for (int i = 0; i < lookup.Length; i++) { lookup[i] = needle.Length; }
for (int i = 0; i < needle.Length; i++)
{
lookup[needle[i]] = needle.Length - i - 1;
}
int index = needle.Length - 1;
var lastByte = needle.Last();
while (index < haystack.Length)
{
var checkByte = haystack[index];
if (haystack[index] == lastByte)
{
bool found = true;
for (int j = needle.Length - 2; j >= 0; j--)
{
if (haystack[index - needle.Length + j + 1] != needle[j])
{
found = false;
break;
}
}
if (found)
return index - needle.Length + 1;
else
index++;
}
else
{
index += lookup[checkByte];
}
}
return -1;
}
You can then search like this. If lbyte will remain constant after a certain time, you can just convert it to an array once and pass that.
//index is returned, or -1 if 'searchBytes' is not found
int startIndex = SimpleBoyerMooreSearch(lbyte.ToArray(), searchBytes);
Update based on comment. Here's the IList implementation which means that arrays and lists (and anything else that implements IList can be passed)
static int SimpleBoyerMooreSearch(IList<byte> haystack, IList<byte> needle)
{
int[] lookup = new int[256];
for (int i = 0; i < lookup.Length; i++) { lookup[i] = needle.Count; }
for (int i = 0; i < needle.Count; i++)
{
lookup[needle[i]] = needle.Count - i - 1;
}
int index = needle.Count - 1;
var lastByte = needle[index];
while (index < haystack.Count)
{
var checkByte = haystack[index];
if (haystack[index] == lastByte)
{
bool found = true;
for (int j = needle.Count - 2; j >= 0; j--)
{
if (haystack[index - needle.Count + j + 1] != needle[j])
{
found = false;
break;
}
}
if (found)
return index - needle.Count + 1;
else
index++;
}
else
{
index += lookup[checkByte];
}
}
return -1;
}
Since arrays and lists implement IList, there's no conversion necessary when calling it in your case.
int startIndex = SimpleBoyerMooreSearch(lbyte, searchBytes);
Another way you could do with lambda expression
int index = lbyte.FindIndex(e => searchBytes.All(i => i.Equals(e));

Categories