I need to count the number of comparisons done by the following search function..
Also how to calculate the execution time..Any help? I want the count and time to be printed in the output.
// C# program for Naive Pattern Searching
using System;
class GFG {
public static void search(String txt, String pat)
{
int M = pat.Length;
int N = txt.Length;
/* A loop to slide pat one by one */
for (int i = 0; i <= N - M; i++) {
int j;
/* For current index i, check for pattern
match */
for (j = 0; j < M; j++)
if (txt[i + j] != pat[j])
break;
// if pat[0...M-1] = txt[i, i+1, ...i+M-1]
if (j == M)
Console.WriteLine("Pattern found at index " + i);
}
}
// Driver code
public static void Main()
{
String txt = "AABAACAADAABAAABAA";
String pat = "AABA";
search(txt, pat);
}
}
// This code is Contributed by Sam007
I need to count the number of comparisons done by the following search function...
You can use counter for this:
public static void search(String txt, String pat, out int counter)
{
int M = pat.Length;
int N = txt.Length;
counter = 0;
/* A loop to slide pat one by one */
for (int i = 0; i <= N - M; i++)
{
int j;
/* For current index i, check for pattern
match */
for (j = 0; j < M; j++)
{
counter++; // counter for below if statement
if (txt[i + j] != pat[j])
{
break;
}
}
// if pat[0...M-1] = txt[i, i+1, ...i+M-1]
if (j == M)
{
Console.WriteLine("Pattern found at index " + i);
}
counter++; // counter for above if statement
}
}
Also how to calculate the execution time.
You can use StopWatch class for this case:
public static async Task Main(string[] args)
{
String txt = "AABAACAADAABAAABAA";
String pat = "AABA";
var stopWatch = new Stopwatch();
stopWatch.Start();
search(txt, pat, out var counter);
stopWatch.Stop();
Console.WriteLine("--------------");
Console.WriteLine($"Count of operations: {counter}, elapsed time: {stopWatch.ElapsedMilliseconds} miliseconds");
}
Related
I am working on a project that compares the time bubble and selection sort take. I made two separate programs and combined them into one and now bubble sort is running much faster than selection sort. I checked to make sure that the code wasn't just giving me 0s because of some conversion error and was running as intended. I am using System.Diagnostics; to measure the time. I also checked that the machine was not the problem, I ran it on Replit and got similar results.
{
class Program
{
public static int s1 = 0;
public static int s2 = 0;
static decimal bubblesort(int[] arr1)
{
int n = arr1.Length;
var sw1 = Stopwatch.StartNew();
for (int i = 0; i < n - 1; i++)
{
for (int j = 0; j < n - i - 1; j++)
{
if (arr1[j] > arr1[j + 1])
{
int tmp = arr1[j];
// swap tmp and arr[i] int tmp = arr[j];
arr1[j] = arr1[j + 1];
arr1[j + 1] = tmp;
s1++;
}
}
}
sw1.Stop();
// Console.WriteLine(sw1.ElapsedMilliseconds);
decimal a = Convert.ToDecimal(sw1.ElapsedMilliseconds);
return a;
}
static decimal selectionsort(int[] arr2)
{
int n = arr2.Length;
var sw1 = Stopwatch.StartNew();
// for (int e = 0; e < 1000; e++)
// {
for (int x = 0; x < arr2.Length - 1; x++)
{
int minPos = x;
for (int y = x + 1; y < arr2.Length; y++)
{
if (arr2[y] < arr2[minPos])
minPos = y;
}
if (x != minPos && minPos < arr2.Length)
{
int temp = arr2[minPos];
arr2[minPos] = arr2[x];
arr2[x] = temp;
s2++;
}
}
// }
sw1.Stop();
// Console.WriteLine(sw1.ElapsedMilliseconds);
decimal a = Convert.ToDecimal(sw1.ElapsedMilliseconds);
return a;
}
static void Main(string[] args)
{
Console.WriteLine("Enter the size of n");
int n = Convert.ToInt32(Console.ReadLine());
Random rnd = new System.Random();
decimal bs = 0M;
decimal ss = 0M;
int s = 0;
int[] arr1 = new int[n];
int tx = 1000; //tx is a variable that I can use to adjust sample size
decimal tm = Convert.ToDecimal(tx);
for (int i = 0; i < tx; i++)
{
for (int a = 0; a < n; a++)
{
arr1[a] = rnd.Next(0, 1000000);
}
ss += selectionsort(arr1);
bs += bubblesort(arr1);
}
bs = bs / tm;
ss = ss / tm;
Console.WriteLine("Bubble Sort took " + bs + " miliseconds");
Console.WriteLine("Selection Sort took " + ss + " miliseconds");
}
}
}
What is going on? What is causing bubble sort to be fast or what is slowing down Selection sort? How can I fix this?
I found that the problem was that the Selection Sort was looping 1000 times per method run in addition to the 1000 runs for sample size, causing the method to perform significantly worse than bubble sort. Thank you guys for help and thank you TheGeneral for showing me the benchmarking tools. Also, the array that was given as a parameter was a copy instead of a reference, as running through the loop manually showed me that the bubble sort was doing it's job and not sorting an already sorted array.
To solve your initial problem you just need to copy your arrays, you can do this easily with ToArray():
Creates an array from a IEnumerable.
ss += selectionsort(arr1.ToArray());
bs += bubblesort(arr1.ToArray());
However let's learn how to do a more reliable benchmark with BenchmarkDotNet:
BenchmarkDotNet Nuget
Official Documentation
Given
public class Sort
{
public static void BubbleSort(int[] arr1)
{
int n = arr1.Length;
for (int i = 0; i < n - 1; i++)
{
for (int j = 0; j < n - i - 1; j++)
{
if (arr1[j] > arr1[j + 1])
{
int tmp = arr1[j];
// swap tmp and arr[i] int tmp = arr[j];
arr1[j] = arr1[j + 1];
arr1[j + 1] = tmp;
}
}
}
}
public static void SelectionSort(int[] arr2)
{
int n = arr2.Length;
for (int x = 0; x < arr2.Length - 1; x++)
{
int minPos = x;
for (int y = x + 1; y < arr2.Length; y++)
{
if (arr2[y] < arr2[minPos])
minPos = y;
}
if (x != minPos && minPos < arr2.Length)
{
int temp = arr2[minPos];
arr2[minPos] = arr2[x];
arr2[x] = temp;
}
}
}
}
Benchmark code
[SimpleJob(RuntimeMoniker.Net50)]
[MemoryDiagnoser()]
public class SortBenchmark
{
private int[] data;
[Params(100, 1000)]
public int N;
[GlobalSetup]
public void Setup()
{
var r = new Random(42);
data = Enumerable
.Repeat(0, N)
.Select(i => r.Next(0, N))
.ToArray();
}
[Benchmark]
public void Bubble() => Sort.BubbleSort(data.ToArray());
[Benchmark]
public void Selection() => Sort.SelectionSort(data.ToArray());
}
Usage
static void Main(string[] args)
{
BenchmarkRunner.Run<SortBenchmark>();
}
Results
Method
N
Mean
Error
StdDev
Bubble
100
8.553 us
0.0753 us
0.0704 us
Selection
100
4.757 us
0.0247 us
0.0231 us
Bubble
1000
657.760 us
7.2581 us
6.7893 us
Selection
1000
300.395 us
2.3302 us
2.1796 us
Summary
What have we learnt? Your bubble sort code is slower ¯\_(ツ)_/¯
It looks like you're passing in the sorted array into Bubble Sort. Because arrays are passed by reference, the sort that you're doing on the array is editing the same contents of the array that will be eventually passed into bubble sort.
Make a second array and pass the second array into bubble sort.
I'm unable to move the hash sign to the right to get the below shape.
My below code is working not as expected but I need to get the below shape.
Please how do I do it?
#
##
###
####
#####
######
#######
public class MyProgramTest
{
public static void StaircaseChallenge(int n)
{
for (int i = 1; i <= n; i++) {
Console.WriteLine(MySpace(i) + HashSign(i));
}
}
public static string HashSign(int n)
{
string t = "";
for (int i = 1; i <= n; i++) {
t += "#";
}
return t;
}
public static string MySpace(int n)
{
string t = "/t";
for (int i = 1; i < n; i++)
{
t += " ";
}
return t;
}
}
Try this:
public class MyProgramTest
{
public static void StaircaseChallenge(int n)
{
for (int i = 1; i <= n; i++)
{
Console.WriteLine(" ".PadLeft(n - i+1, ' ')+"#".PadLeft(i,'#'));
}
}
Or make few changes to your code:
public class MyProgramTest
{
public static void StaircaseChallenge(int n)
{
for (int i = 1; i <= n; i++)
{
Console.WriteLine(MySpace(n - i + 1) + HashSign(i));
}
}
public static string HashSign(int n)
{
string t = "";
for (int i = 1; i <= n; i++)
{
t += "#";
}
return t;
}
public static string MySpace(int n)
{
string t = string.Empty;
for (int i = 1; i < n; i++)
{
t += " ";
}
return t;
}
}
A more memory efficient way would be using the StringBuilder class.
For this situation it's not critical, but nice to know.
// define the amount of steps
int n=8;
// amount of leading whitespaces, for later usage
int padding=0;
// this one is the "working" memory, initialized by n + padding whitespaces
StringBuilder currentLine=new StringBuilder(new string(' ',n+padding));
// it counts down from the last index to the one indicated by padding
for (int i = currentLine.Length-1; i >=padding; i--)
{
// replace the char at the current index with #; (here: always the index of the last whitespace)
currentLine[i]='#';
// display a copy of the current state on the console,
Console.WriteLine(currentLine.ToString());
}
Please change only few things in your code :
public class MyProgramTest
{
public static void StaircaseChallenge(int n)
{
for (int i = 1; i <= n; i++) {
Console.WriteLine(MySpace(i, n) + HashSign(i));
}
}
public static string HashSign(int n)
{
string t = "";
for (int i = 1; i <= n; i++) {
t += "#";
}
return t;
}
public static string MySpace(int m, int n)
{
string t = "";
for (int i = 1; i <= n - m; i++)
{
t += " ";
}
return t;
}
}
You have to pass more one variable is n (number of row) in MySpace() function for leave space. When you pass number of row in MySpace() function then it will leave (number of row - 1) space. So if you enter 5 then first time it will leave 4 space and then put "#" like wise.
I am having problems with the last method of my program. I can't seem to figure out the right way to pass the parameters or maybe perhaps the way I have the method coded is wwrong. Please help! The stipulations of the method is below:
The SearchArray() method will return a bool to indicate if the number searched for was found or not. Its parameter is supposed to have: An integer that is the number to be searched for, a two-dimensional array to search, an out reference integer parameter that will represent the row index and an out reference integer parameter that will represent the column index
This method will use the first parameter and the two-dimensional array and will search the array for the number chosen by the GetNumber() method. I must Initialize the row and column parameters to -1. As the program goes through the array, if the number is found, assign the row and column parameters to the row and column index numbers where it is found and stop searching the array right away. The method is supposed to return a Boolean value to indicate whether or not the number was found.
Any help on this is greatly appreciated I have been on here for awhile trying to get this figured out completely right now if I run it it gets to the point where it asks for a number then runs into an error stating the index is outside the boundaries. Here is my code so far:
static void Main(string[] args)
{
int [,] randomNumArray = new int[3, 5];
FillArray(randomNumArray);
PrintArray(randomNumArray);
SumRows(randomNumArray);
SumCols(randomNumArray);
SumArray(randomNumArray);
int rows, cols;
int search = GetNumber();
SearchArray(search, randomNumArray, out rows, out cols);
}
public static void FillArray(int[,] randomNumbersArray)
{
Random num = new Random();
for (int r = 0; r < randomNumbersArray.GetLength(0); r++)
{
for (int c = 0; c < randomNumbersArray.GetLength(1); c++)
{
randomNumbersArray[r, c] = num.Next(15, 97);
}
}
}
public static void PrintArray(int[,] randomPrintArray)
{
for (int r = 0; r < randomPrintArray.GetLength(0); r++)
{
for (int c = 0; c < randomPrintArray.GetLength(1); c++)
{
Console.Write("{0,3:F0}", randomPrintArray[r, c]);
}
Console.WriteLine("");
}
Console.WriteLine("");
}
public static void SumRows(int[,] sumOfRowsArray)
{
int rowSum;
for (int r = 0; r < sumOfRowsArray.GetLength(0); r++)
{
rowSum = 0;
for (int c = 0; c < sumOfRowsArray.GetLength(1); c++)
{
rowSum += sumOfRowsArray[r, c];
}
Console.WriteLine("The total sum for row "+ (r + 1) + " is: " + rowSum + ".");
}
Console.WriteLine("");
}
public static void SumCols(int[,] sumOfColsArray)
{
int colsSum;
for (int c = 0; c < sumOfColsArray.GetLength(1); c++)
{
colsSum = 0;
for (int r = 0; r < sumOfColsArray.GetLength(0); r++)
{
colsSum += sumOfColsArray[r, c];
}
Console.WriteLine("The total sum for column " + (c + 1) + " is: " + colsSum + ".");
}
Console.WriteLine("");
}
public static void SumArray(int[,] sumOfAllArray)
{
int sumOfAll = 0;
for (int r = 0; r < sumOfAllArray.GetLength(0); r++)
{
for (int c = 0; c < sumOfAllArray.GetLength(1); c++)
{
sumOfAll += sumOfAllArray[r, c];
}
}
Console.WriteLine("Total for sum of the Array is: " + sumOfAll + "\n");
}
public static int GetNumber()
{
Console.Write("Please enter a number between 15 and 96: ");
int chosenNumber = int.Parse(Console.ReadLine());
while (chosenNumber > 96 || chosenNumber < 15)
{
Console.Write("Number not between 15 and 96. Try again: ");
chosenNumber = int.Parse(Console.ReadLine());
}
return chosenNumber;
}
public static bool SearchArray(int soughtOutNum, int [,] searchableArray, out int rowIndex, out int colsIndex)
{
rowIndex = -1;
colsIndex = -1;
for (int c = 0; c < searchableArray.GetLength(0); c++)
{
for (int r = 0; r < searchableArray.GetLength(1); r++)
{
if (searchableArray[r, c] == soughtOutNum)
{
rowIndex = r;
colsIndex = c;
return true;
break;
}
}
}
return false;
}
You can use LINQ for your SearchArray.
public static bool SearchArray(int soughtOutNum, int [,] searchableArray, out int rowIndex, out int colsIndex)
{
if(searchableArray.Any(x => soughtOutNum))
{
colsIndex = searchableArray.FirstOrDefault(x => x == soughtOutNum).GetLength(0);
rowIndex= searchableArray.FirstOrDefault(x => x == soughtOutNum).GetLength(1);
}
else
{
return false;
}
}
This returns your first hit's column and row.
I have a program here that need some improvements. This Program inputs 5 elements in an Array and Removes if any duplicates. It works but the problem is that it sets every duplicate to zero. I don't want to display zero. I want it completely destroyed and eliminated. I don't want that duplicate element to appear. This is what I have so Far! Could Use some help. Thank You.
// Gurpreet Singh
// Duplicate Program
using System;
class duplicate
{
static void Main()
{
const int Array_Size = 5;
int [] number = new int [Array_Size];
int i;
for ( i = 0; i < Array_Size; i++)
{
Console.Write("Element " + i + ": ");
number[i] = Int32.Parse(Console.ReadLine());
if (number[i] < 9 || number[i] > 101)
{
Console.WriteLine("Enter Number between 10 - 100");
number[i] = Int32.Parse(Console.ReadLine());
}
}
for (i = 0; i < Array_Size; i++)
{
for (int j = 0; j < Array_Size; j++)
{
if (i != j)
{
if (number[j] == number[i])
number[j] = 0;
}
}
}
Console.WriteLine("Duplicate Removed:");
for (i = 0; i < Array_Size; i++)
{
Console.WriteLine("Element " + i + " " + number[i]);
}
Console.ReadLine();
}
}
The easiest way is to use Linq's Distinct method:
number = number.Distinct().ToArray();
This will return a new array without any duplicates.
The duplicate is displayed as zero, since you assign the value of the duplicate to be zero, in the line,
if(number[j]==number[i])
number[j]=0
to delete the element from the array, use the following code:
if(number[j]==number[i])
{
int k=j;
while(k<Array_Size-1)
{
number[k]=number[k+1];
k++;
}
Array_Size--;
}
the statement Array_Size--; is done so that the last element is not repeated twice
This is my complete code in which I put some double-for-loop statement to
prevent it from inserting the duplicated integers in an array.
Have a look.
class Program
{
static void Main(string[] args)
{
const int ARRAY_SIZE = 5;
int[] ArrayTable = new int[ARRAY_SIZE];
int Element=0;
int a;
for(a=0; a<ArrayTable.Length;a++)
{
Console.Write("Please Enter an integer (between 10-100): ");
Element = Int32.Parse(Console.ReadLine());
while (Element < 10 || Element > 100)
{
Console.Write("Try again (between 10-100): ");
Element = Int32.Parse(Console.ReadLine());
}
ArrayTable[a] = Element;
for (int b = 0; b < a; b++)
{
while (ArrayTable[a] == ArrayTable[b])
{
Console.Write("Integer Duplicated!\nTry again: ");
Element = Int32.Parse(Console.ReadLine());
ArrayTable[a] = Element;
Console.WriteLine();
while (Element < 10 || Element > 100)
{
Console.Write("Try again (between 10-100): ");
Element = Int32.Parse(Console.ReadLine());
ArrayTable[a] = Element;
}
}
}
}
for (int c = 0; c < ArrayTable.Length; c++)
{
Console.Write("{0} ", ArrayTable[c]);
}
}
I am trying to get a count of all the times a byte sequences occurs in another byte sequences. It cannot however re-use a bytes if it already counted them. For example given the string
k.k.k.k.k.k. let's assume the byte sequence was k.k it would then find only 3 occurrences rather than 5 because they would be broke down like: [k.k].[k.k].[k.k]. and not like [k.[k].[k].[k].[k].k] where they over lap and essentially just shift 2 to the right.
Ideally the idea is to get an idea how a compression dictionary or run time encoding might look. so the goal would be to get
k.k.k.k.k.k. down to just 2 parts, as (k.k.k.) is the biggest and best symbol you can have.
Here is source so far:
using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.IO;
static class Compression
{
static int Main(string[] args)
{
List<byte> bytes = File.ReadAllBytes("ok.txt").ToList();
List<List<int>> list = new List<List<int>>();
// Starting Numbers of bytes - This can be changed manually.
int StartingNumBytes = bytes.Count;
for (int i = StartingNumBytes; i > 0; i--)
{
Console.WriteLine("i: " + i);
for (int ii = 0; ii < bytes.Count - i; ii++)
{
Console.WriteLine("ii: " + i);
// New pattern comes with refresh data.
List<byte> pattern = new List<byte>();
for (int iii = 0; iii < i; iii++)
{
pattern.Add(bytes[ii + iii]);
}
DisplayBinary(bytes, "red");
DisplayBinary(pattern, "green");
int matches = 0;
// foreach (var position in bytes.ToArray().Locate(pattern.ToArray()))
for (int position = 0; position < bytes.Count; position++) {
if (pattern.Count > (bytes.Count - position))
{
continue;
}
for (int iiii = 0; iiii < pattern.Count; iiii++)
{
if (bytes[position + iiii] != pattern[iiii])
{
//Have to use goto because C# doesn't support continue <level>
goto outer;
}
}
// If it made it this far, it has found a match.
matches++;
Console.WriteLine("Matches: " + matches + " Orig Count: " + bytes.Count + " POS: " + position);
if (matches > 1)
{
int numBytesToRemove = pattern.Count;
for (int ra = 0; ra < numBytesToRemove; ra++)
{
// Remove it at the position it was found at, once it
// deletes the first one, the list will shift left and you'll need to be here again.
bytes.RemoveAt(position);
}
DisplayBinary(bytes, "red");
Console.WriteLine(pattern.Count + " Bytes removed.");
// Since you deleted some bytes, set the position less because you will need to redo the pos.
position = position - 1;
}
outer:
continue;
}
List<int> sublist = new List<int>();
sublist.Add(matches);
sublist.Add(pattern.Count);
// Some sort of calculation to determine how good the symbol was
sublist.Add(bytes.Count-((matches * pattern.Count)-matches));
list.Add(sublist);
}
}
Display(list);
Console.Read();
return 0;
}
static void DisplayBinary(List<byte> bytes, string color="white")
{
switch(color){
case "green":
Console.ForegroundColor = ConsoleColor.Green;
break;
case "red":
Console.ForegroundColor = ConsoleColor.Red;
break;
default:
break;
}
for (int i=0; i<bytes.Count; i++)
{
if (i % 8 ==0)
Console.WriteLine();
Console.Write(GetIntBinaryString(bytes[i]) + " ");
}
Console.WriteLine();
Console.ResetColor();
}
static string GetIntBinaryString(int n)
{
char[] b = new char[8];
int pos = 7;
int i = 0;
while (i < 8)
{
if ((n & (1 << i)) != 0)
{
b[pos] = '1';
}
else
{
b[pos] = '0';
}
pos--;
i++;
}
//return new string(b).TrimStart('0');
return new string(b);
}
static void Display(List<List<int>> list)
{
//
// Display everything in the List.
//
Console.WriteLine("Elements:");
foreach (var sublist in list)
{
foreach (var value in sublist)
{
Console.Write("{0,4}", value);
}
Console.WriteLine();
}
//
// Display total count.
//
int count = 0;
foreach (var sublist in list)
{
count += sublist.Count;
}
Console.WriteLine("Count:");
Console.WriteLine(count);
}
static public int SearchBytePattern(byte[] pattern, byte[] bytes)
{
int matches = 0;
// precomputing this shaves some seconds from the loop execution
int maxloop = bytes.Length - pattern.Length;
for (int i = 0; i < maxloop; i++)
{
if (pattern[0] == bytes[i])
{
bool ismatch = true;
for (int j = 1; j < pattern.Length; j++)
{
if (bytes[i + j] != pattern[j])
{
ismatch = false;
break;
}
}
if (ismatch)
{
matches++;
i += pattern.Length - 1;
}
}
}
return matches;
}
}
Refer to the post to get the non binary of the file should be, here is the binary data:
011010110010111001101011001011100110101100101110011010110010111001101011001011100110101100101110 I am hope to have it smaller than how it started.
private static int CountOccurences(byte[] target, byte[] pattern)
{
var targetString = BitConverter.ToString(target);
var patternString = BitConverter.ToString(pattern);
return new Regex(patternString).Matches(targetString).Count;
}
With this solution you'd have access to the individual indexes that matched (while enumerating) or you could call Count() on the result to see how many matches there were:
public static IEnumerable<int> Find<T>(T[] pattern, T[] sequence, bool overlap)
{
int i = 0;
while (i < sequence.Length - pattern.Length + 1)
{
if (pattern.SequenceEqual(sequence.Skip(i).Take(pattern.Length)))
{
yield return i;
i += overlap ? 1 : pattern.Length;
}
else
{
i++;
}
}
}
Call it with overlap: false to solve your problem or overlap: true to see the overlapped matches (if you're interested.)
I have a couple of other methods with slightly different API (along with better performance) here, including one that work directly on streams of bytes.
quick and dirty with no regex. although i'm not sure if it answers the intent of the question, it should be relatively fast. i think i am going to run some timing tests against regex to see for sure the relative speeds:
private int CountOccurrences(string TestString, string TestPattern)
{
int PatternCount = 0;
int SearchIndex = 0;
if (TestPattern.Length == 0)
throw new ApplicationException("CountOccurrences: Unable to process because TestPattern has zero length.");
if (TestString.Length == 0)
return 0;
do
{
SearchIndex = TestString.IndexOf(TestPattern, SearchIndex);
if (SearchIndex >= 0)
{
++PatternCount;
SearchIndex += TestPattern.Length;
}
}
while ((SearchIndex >= 0) && (SearchIndex < TestString.Length));
return PatternCount;
}
private void btnTest_Click(object sender, EventArgs e)
{
string TestString1 = "k.k.k.k.k.k.k.k.k.k.k.k";
string TestPattern1 = "k.k";
System.Console.WriteLine(CountOccurrences(TestString1, TestPattern1).ToString()); // outputs 6
System.Console.WriteLine(CountOccurrences(TestString1 + ".k", TestPattern1).ToString()); // still 6
System.Console.WriteLine(CountOccurrences(TestString1, TestPattern1 + ".").ToString()); // only 5
}