c# - Binary search algorithm random generated array items not working - c#

I have implemented binary search algorithm in a console window application in C#. I am generating random values to the array and sorting them using Random() and Array.Sort() functions respectively.
The Problem - No matter what Key(item to be searched in the array) I give, the program is returning Key not found when the array items are generated using Random function
This does not happen if I enter the array elements manually using Console.ReadLine().
TLDR: Binary Search algorithm works fine when array items are entered manually, but does not work when array items are generated using Random function.
Can anyone point out what is the mistake I am doing?
My code - Random Generated array items.
namespace BSA
{
class Program
{
static void Main(string[] args)
{
var arr = new int[10];
Random rnd = new Random();
for (int i = 0; i < arr.Length; i++)
{
arr[i] = rnd.Next(1, 1000);
}
Array.Sort(arr);
for (int i = 0; i < arr.Length; i++)
{
Console.Write("{0}\n", i);
}
while (true)
{
Console.WriteLine("Enter the number to be searched in the array.");
var searchItem = Convert.ToInt32(Console.ReadLine());
var foundPos = Search(arr, searchItem);
if (foundPos > 0)
{
Console.WriteLine("Key {0} found at position {1}", searchItem, foundPos);
}
else
{
Console.WriteLine("Key {0} not found", searchItem);
}
}
}
public static int Search(int[] arr, int item)
{
var min = 0;
var N = arr.Length;
var max = N - 1;
int basicOperations = 0;
basicOperations++;
do
{
var mid = (min + max)/2;
if (arr[mid] == item)
return mid;
if (item < arr[mid])
max = mid - 1;
else
min = mid + 1;
basicOperations++;
} while (min <= max);
return basicOperations;
}
}
}
Please let me know if I am doing any silly mistake or I am committing a blunder in the above code. Any help would be really helpful.

Your search code works fine as far as I can see. However when you list the contents of the random array, you should write arr[i] rather than i to see what's in the array so you can pick a search value in it. Alternatively, pass arr[x] as the search item. It should return x.

Your code works correctly. You're just not looking for the right keys. The function that prints the values generated into the array prints the loop counter instead:
for (int i = 0; i < arr.Length; i++)
{
Console.Write("{0}\n", i);
}
You need to change it to:
for (int i = 0; i < arr.Length; i++)
{
Console.Write("{0}\n", arr[i]);
}
This will show you the values actually generated.

Comment too short for this so added answer to show how to set basicOperations and still return search position. You declare basicOperations as an out parameter which means the method can change it so the caller can see it when method returns.
public static void Main(string[] args)
{
... ... ...
int basicOperations;
int searchPos = IntArrayBinarySearch(arr, arr[5], out basicOperations);
Console.WriteLine("Found at {0} basic ops={1}", searchPos, basicOperations);
}
public static int IntArrayBinarySearch(int[] data, int item, out int basicOperations)
{
var min = 0;
var N = data.Length;
var max = N - 1;
basicOperations = 0;
basicOperations++;
and at bottom, you don't need to return out parameters, just return -1 to indicate failure as you did before
return -1;

Related

Hello, I am trying to arrange an array from the small value to big value, but its not working

I am trying to arrange an array from the small value to grow, and for some reason this function does not work (SmallToGrow), the rest is excellent. Thanks.
There are auxiliary functions that I use and are excellent facts, only the (SmallToGrow) function does not work for me and I cannot understand why. I'd love anyone who can help. Thanks
enter code here
//this check if all cells equals
public bool EverybodyAreEqual(int [] array)
{
for (int i = 0; i < array.Length - 1; i++)
if (array[i] != array[i + 1])
return false;
return true;
}
//This function changes all the values ​​in the array that contain the -numInArray- value you entered,
// to the -numChanged- value you entered
public void ChangNumWhere(ref int [] array,int numInArray,int numChanged)
{
for (int i = 0; i < array.Length; i++)
if (array[i] == numInArray)
array[i] = numChanged;
}
//A function that returns the number of values ​​that are not equal in the array
public int NumDifferentArray(int [] array)
{
int[] arr = new int[array.Length];
for (int i = 0; i < arr.Length; i++)
arr[i] = array[i];
bool con = true;
int contain = 0;
int index = 0;
for(int i=0; con;i++)
{
if (!arr.Contains(i))
{
contain = i;
con = false;
}
}
while(!this.EverybodyAreEqual(arr))
{
for (int i = 0; i < arr.Length; i++)
if (arr[i] != contain)
{
this.ChangNumWhere(ref arr, arr[i], contain);
index++;
}
}
return index;
}
public int HowTimesExsist(int [] array,int num)
{
int index = 0;
for(int i=0;i<array.Length;i++)
{
if (array[i] == num)
index++;
}
return index;
}
/// This function returns a minimum value as required,
/// for example if you requested 0 the smallest value is returned,
/// if 1 is returned one above it and so on,
/// if the index is greater than the length of the array the largest number is returned
public int MinBottom(int[] array, int num)
{
if (num < 0)
throw new Exception("num canot be less then 0");
int max = this.MaxArray(array);
while (num > 0)
{
int min = this.MinArray(array);
for (int i = 0; i < array.Length; i++)
if(array[i]==min)
array[i] = max;
num--;
}
return this.MinArray(array);
}
public int[] SmallToGrow(int [] array)
{
int i = 0;
int[] arr = new int[array.Length];
for (int j = 0; j < this.NumDifferentArray(array); j++)
for (int b = 0; b < this.HowTimesExsist(array, MinBottom(array, j)); i++, b++)
arr[i] = this.MinBottom(array, j);
return arr;
}
Why don't you use a list instead? They have a method attached to it that sorts it all for you.
List<int> sortInt = new List<int>() { 2, 5, 1, 50, 258, 87, 63, 52, 100, 85, 21 };
sortInt.Sort();
Returned them in numerical order with [0] being 1 and [10] being 258.
you can then turn the list to an array with sortint.ToArray();.
Edit
As dymanoid mentioned you can just use your array and just run the array.Sort() method with it. Learn something new every day.
It looks like you are in large part re-engineering some common functionality in System.Collections.Generic and System.Linq -
public bool EverybodyAreEqual(int[] array)
{
// If all items are the same,
// there should only be one distinct item in the collection
return array.Distinct().Length == 1;
}
public int NumDifferentArray(int[] array)
{
// Group the numbers in the array and
// count the number of groups with only one item
return array.GroupBy(number => number).Where(g => g.Count() == 1);
}
public int HowTimesExsist(int[] array, int num)
{
// Count the number of times a number appears in the array
return array.Count(n => n == num);
}
/// This function returns a minimum value as required,
/// for example if you requested 0 the smallest value is returned,
/// if 1 is returned one above it and so on,
/// if the index is greater than the length of the array the largest number is returned
public int MinBottom(int[] array, int num)
{
if (num < 0)
{
// Be specific about the type of exception you are throwing
throw new ArgumentOutOfRangeException(nameof(num));
}
// Sort a copy of your array
var sorted = Array.Copy(array);
Array.Sort(sorted);
// If there are any items over the specified minimum, return those
// otherwise, return the highest number in the array
// Using LastOrDefault for the maximum will return 0 if the initial array is empty
var itemsOverMinimum = sorted.Where(n => n >= num);
return itemsOverMinimum.Any() ? itemsOverMinimum.First() : sorted.LastOrDefault();
}
public int[] SmallToGrow(int[] array)
{
// Because you are returning an array, that implies that the original array should not change
// Copy the array and sort it
var copy = Array.Copy(array);
Array.Sort(copy);
return copy;
}
I saw that you mentioned that you are trying to find alternative ways to accomplish some of these things, and I want to give you some advice about that.
I think it's pretty cool that you want to challenge yourself. However, this specific functionality is part of System libraries. One of the best parts about working in C# is how much of this sort of thing is already written for you, and this functionality being added to System means that Microsoft believes these pieces are the core (pun intended) building blocks for working in .NET.
Unless your project is to specifically write a better sorting algorithm, you are not going to write this better than it is in those libraries. I've been doing this for a while, and I'm not going to be able to either.
But that doesn't mean you should stop learning. Instead, I would encourage you to look at the github source for the methods I used in my snippets above. I think that will probably be a lot more helpful than re-engineering this stuff from scratch.
https://github.com/dotnet/runtime/blob/master/src/libraries/System.Linq/src/System/Linq/Distinct.cs
https://github.com/dotnet/runtime/blob/master/src/libraries/System.Linq/src/System/Linq/Grouping.cs
https://github.com/dotnet/runtime/blob/master/src/libraries/System.Linq/src/System/Linq/Where.cs
https://github.com/dotnet/runtime/blob/master/src/libraries/System.Linq/src/System/Linq/Count.cs

c# implementing a bucket sort algorithm

Good evening everyone here! I created a bucket sort algorithm, but it throws me an error that index is out of range. Could you please tell me where is the problem? I can't find the solution by myself, that's why I'm asking for your help
public int[] Sort(int[] unsortedSequence)
{
List<List<int>> buckets = new List<List<int>>();
InitializeBuckets(buckets);
Scatter(unsortedSequence, buckets);
int i = 0;
foreach (List<int> bucket in buckets)
{
int[] arr = bucket.ToArray();
InsertionSort(arr);
foreach (int d in arr)
{
unsortedSequence[i++] = d;
}
}
return unsortedSequence;
}
private static void Scatter(int[] array, List<List<int>> buckets)
{
foreach (int value in array)
{
int bucketNumber = GetBucketNumber(value);
buckets[bucketNumber].Add(value);
}
}
private static void InsertionSort(int[] array)
{
int j;
int temp;
for (int i = 1; i < array.Length; i++)
{
j = i;
while (j > 0 && array[j] < array[j - 1])
{
temp = array[j];
array[j] = array[j - 1];
array[j - 1] = temp;
j--;
}
}
}
private static int GetBucketNumber(int value)
{
int val = value * 10;
return val;
}
private static void InitializeBuckets(List<List<int>> buckets)
{
for (int i = 0; i < 10; i++)
{
List<int> a = new List<int>();
buckets.Add(a);
}
}
I am not sure what logic you did for sorting this but I got why you are getting index out of range error.
Error in Code
You are creating 10 buckets and now you are trying to generate bucket number by multiplying current value or array with 10.
For example, if your current value of array is 2 then generated bucket number will be 20. You got only 10 buckets so Scatter() method will give you error.
private static void Scatter(int[] array, List<List<int>> buckets)
{
foreach (int value in array)
{
int bucketNumber = GetBucketNumber(value);
buckets[bucketNumber].Add(value); // ERROR HERE
}
}
SOLUTION
Actually, there is problem with GetBucketNumber() method. You should use remainder not multiplication. Change method with following.
private static int GetBucketNumber(int value)
{
int val = value % 10;
return val;
}
You must do
Try to solve your problem with hard attempts before you ask for help. Run your program on paper first I mean confirm your logic before you start coding. Have faith in you and give enough time to your attempts. Enjoy coding.
Before I answer, I want to strongly suggest that you always make a good faith attempt at solving a problem first before seeking outside help. I don't necessarily think you didn't try, but this problem was easily identified by stepping through the debugger.
If that's an aspect of coding you're not too familiar with, I strongly recommend making it a priority to learn - it will only make you a better developer in the long run.
The problem is occurring in the Scatter method at this point:
int bucketNumber = GetBucketNumber(value);
buckets[bucketNumber].Add(value);
The exception occurs because GetBucketNumber multiplies the input by 10, but you're using that multiplied value as the index for buckets.

creating an array of indexes that has the size of the loop that fills the data

This is the method im having issues with
this is the code with isssues
this is code im having issues with
static void FindDuplicates(int maxValue, int[]totalOfScores) {
int finalWinner = 0;
int i = 0;
int[] WinnerIndex = new int [totalOfScores.Length];
for (int g = 0; g < totalOfScores.Length; g++) {
if (totalOfScores[g] == maxValue)
for (i = 0; i < WinnerIndex.Length; i++) {
WinnerIndex[i] = g;
finalWinner = WinnerIndex[i];
Console.WriteLine("\n\nThe Highest Scoring comp was comp Number {0} With a total score of {1}", finalWinner + 1, maxValue);
}
}
}
If your question is on the dynamic array size, do not use array, use List instead. Or use Array.Resize(). But if I understand your goal correctly, you are trying to list all competitors that have the highest score, in this example competitors 3 and 4 you don't really need array for the results at all. Instead do:
static void FindDuplicates(int maxValue, int[] totalOfScores){
for (int i = 0; i < totalOfScores.Length; i++) {
if (totalOfScores[i] == maxValue){
Console.WriteLine("\n\nThe Highest Scoring comp was comp Number {0} With a total score of {1}", i+1, maxValue);
}
}
}
By the way, I would rename FindDuplicates as FindAllWinners (or then I have misunderstood your intentions).

Why I get index out of range?

So I have a List with 9 elements inside but I get an index oute of range error when I am trying to Add the array[4] element in the "right" List. Can someone tell me what's wrong?
public static int dosomething(ref List<int> array, int n)
{
List<int> left = new List<int>();
List<int> right = new List<int>();
for (int i = 0; i < n; i++)
{
if (i < n/2)
{
left.Add(array[i]);
Console.WriteLine("Left[{0}] = {1}", i, left[i]);
}
else
{
Console.WriteLine("i = {0}", i);
right.Add(array[i]);
Console.WriteLine("Right[{0}] = {1}", i, right[i]);
}
}
}
You're using the wrong index for left and right. Since you're iterating through the array, and adding to left some of the time and right the rest of the time, you can't use i for the index into those to retrieve values. The error is actually happening in your Console.WriteLine() calls.
Instead, you can use:
left[left.Count - 1];
or:
right[right.Count - 1];

Displaying Array within Method

This relates to another code I posted earlier but since this is a different question I decided to make a new post. I'm currently stuck with this code, I'm a c# beginner so this look complicated to me. I've been working on this code that is supposed to get an array from the user, calculate its average then display the results inside show(). I got this working to show the average of the array but now I need to actually display the array each value individually i.e. The 1st value you entered was : 12
The 2nd value you enteres was : 32
Thank guys!
private static int Array()
{
string inValue;
int[] score = new int[10];
int total = 0;
for (int i = 0; i < score.Length; i++)
{
Console.Write("Enter Value {0}: ", i + 1);
inValue = Console.ReadLine();
score[i] = Convert.ToInt32(inValue);
}
for (int i = 0; i < score.Length; i++)
{
total += score[i];
}
return total;
}
Change your GetValues() function to actually return the array of integers, then use the return value in your other functions.
i.e. change GetValues() to:
private static int[] GetValues()
{
string inValue;
int[] score = new int[5];
int total = 0;
for (int i = 0; i < score.Length; i++)
{
Console.Write("Enter Value {0}: ", i + 1);
inValue = Console.ReadLine();
score[i] = Convert.ToInt32(inValue);
}
return score;
}
EDIT: Here is how to use the GetValues() function above in a function to print out all the values. You should be able to work out the rest from here:
private static void PrintArray(int[] scoreArray)
{
for (int i = 0; i < scoreArray.Length; i++)
{
Console.WriteLine("Value #{0}: {1}", i + 1, scoreArray[i]);
}
}
Note how the scoreArray is passed in, as well as how each value is accessed, using scoreArray[i] (where i is a number from 0 to 4 inclusive).
Move int[] score out of GetValues and declare it at the class level making it static:
static int[] score = new int[5];
My next recommendation is that you don't do inside your functions more than what they claim to do from their name; for example, GetValues() should only get the values from user; not calculate totals (as you are doing) because it's misleading; it forces you to look at the implementation to know exactly what it does. Similarly for Show(); if the purpose is to show values entered by user, then call it ShowValues(); If the purpose is to show values entered by user as well as calculate the average, then name it something along the lines of ShowValuesAndAverage()
Here's a complete implementation of your program with my recommendations:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace testscores
{
class Program
{
static int[] score = new int[5];
//Get Values
private static void GetValues()
{
string inValue;
for (int i = 0; i < score.Length; i++)
{
Console.Write("Enter Value {0}: ", i + 1);
inValue = Console.ReadLine();
score[i] = Convert.ToInt32(inValue);
}
}
//FIND AVERAGE
private static double FindAverage()
{
double total = 0.0;
for (int i = 0; i < score.Length; i++)
{
total += score[i];
}
double average = total / 5.0;
return average;
}
//Show
static void ShowValuesAndAverage()
{
Console.WriteLine("The values are:");
for (int i = 0; i < score.Length; i++)
{
Console.WriteLine(string.Format("The {0} value you entered was {1}", i + 1, score[i]));
}
Console.WriteLine("The average is: {0}", FindAverage());
}
//Main
static void Main()
{
GetValues();
ShowValuesAndAverage();
Console.ReadKey();
}
}
}
Make one function for GetValues() and return the array. Then pass the array into a AddValues() function, and into Show().
Either that or read up on how to use pointers, but that might be overcomplicating things a little.

Categories

Resources