❔ Hello everyone, can someone help me out understand this part!

Search and sort algorithm
Searching and sorting are important tools in programming. We will study some
search and sorting algorithms.
Search
Searching is the process of looking for a particular value in a data set. The searched values are called
keys.
Linear search
Linear search is a simple sorting method used when performing the search in a
unsorted array. The algorithm goes through the entire array for index i = 0, 1, 2, ...,
arr.length-1. If key = arr[i] then index = i, the loop is terminated and index is returned.
If the key is not found in the entire array, -1 is returned. Average complexity for
the linear search algorithm is O(n/2) and in the worst case the complexity is O(n)
Search and sort algorithm
Searching and sorting are important tools in programming. We will study some
search and sorting algorithms.
Search
Searching is the process of looking for a particular value in a data set. The searched values are called
keys.
Linear search
Linear search is a simple sorting method used when performing the search in a
unsorted array. The algorithm goes through the entire array for index i = 0, 1, 2, ...,
arr.length-1. If key = arr[i] then index = i, the loop is terminated and index is returned.
If the key is not found in the entire array, -1 is returned. Average complexity for
the linear search algorithm is O(n/2) and in the worst case the complexity is O(n)
74 Replies
Anu6is
Anu6isβ€’2y ago
That's a lot of text, understand what exactly?
The king of kings
The king of kingsOPβ€’2y ago
The bottom line is that I'm trying to learn Search and sort algorithm do u have any tips or resources to learn this? Is this feel unclear? I'm just trying to learn how to use this concept πŸ™‚
blinkbat
blinkbatβ€’2y ago
google them separately "searching algorithms"/"lookup algorithms" and "sorting algorithms" https://algorithm-visualizer.org/brute-force/bubble-sort and there are sites like this that let you play a visualization of the operations
The king of kings
The king of kingsOPβ€’2y ago
Wow, what a great tips & resources πŸ˜ƒ thanks a lot bro. I will definitely look them up πŸ‘ πŸ™‚ Is this related to databases in any sense? Is it complicated to learn? Just getting an idea from your experience πŸ™‚ I did visit the link that you attached, but I don't really know what to do with it πŸ€·πŸ»β€β™‚οΈ it's not even c# code
TheBoxyBear
TheBoxyBearβ€’2y ago
Can be related to databases depending on the use case. Sorting algorithms work with data from an arbitrary source
The king of kings
The king of kingsOPβ€’2y ago
Ok! Well! Since the assignment from my course requires me to build a weather station console application project and I'm gonna need to use this concept in the project. NOTE: You should not use the compiler predefined methods/functions. This means that you must write the code yourself for all methods/functions you call in the program. Task 4-1- Compulsory. Grading scale: F-C Create a class city that contains two attributes. The city class contains two member variables/attributes: string name; int temp; These store the city name and temperature, as well as the ToString function/method public string ToString() They return a string containing values in the name and temp attributes. Note that the ToString() member function/method does not generate any output to the console. Task 4-2- Compulsory. Grading scale: F-C Write a function/method for linear search in a field (array, vector, list) cities with city objects. int lensesok(field cities, int n, int searchtemp) The function/method should search a field cities for a city with a specific temperature (temp) and return the index for that city. If no city with the temperature is found -1 is returned. The variable is the number of elements in the field. Write pseudo code and draw flowchart for search method/function. Also write a function/method that sorts a city field by temperature (coldest first). void bubble sort(field cities, int n) Software that can be used to draw diagrams Test the class Make a main program to test your class and functions/methods. Declare a field cities with four cities that should contain temperature measurements from four different cities. The values are entered by the user when the program is run. The program should then sort the cities field and print the cities' names and temperatures in order of temperature with the coldest city first. Finally, the function/method lens yoke must be called and search for whether any city has a certain temperature. You can choose whether this temperature is fixed in the code or entered by the user.
mtreit
mtreitβ€’2y ago
I got lost at "Linear search is a simple sorting method" since...it's not
The king of kings
The king of kingsOPβ€’2y ago
Oh! Really, is it because of the complexity?
blinkbat
blinkbatβ€’2y ago
it's not a sorting method it's a search as the name implies
Accord
Accordβ€’2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
The king of kings
The king of kingsOPβ€’2y ago
Based on the assignment above, which one should I use, searching or sorting? Any tips would be appreciated πŸ€— I have watched and googled this concept so far and I got an idea theoretically, but when it comes to putting it in code I just don't know how to implement it πŸ™†πŸ»β€β™‚οΈ
blinkbat
blinkbatβ€’2y ago
idk why you'd implement your own searching or sorting c# has native implementations that i assume are the fastest for their flexibility
The king of kings
The king of kingsOPβ€’2y ago
Well! This is what is required for me to implement in the weather station project based on the assignment.
The king of kings
The king of kingsOPβ€’2y ago
Ok! That's interesting to know. Could u give me an example of what you just mentioned? Do u mean like this: https://www.youtube.com/watch?v=NJ5ghiutzfY&t=4s
dotnet
YouTube
Sort, Search, and Index Lists | C# 101 [13 of 19]
Once you have a list of something, how hard is it to Sort a List with C#? Can we search for items with a list or collection? Let's also examine indexing and how Lists can be explored from multiple direction in C# and .NET. πŸ‘©β€πŸ’» Get the .NET Interactive Notebooks and follow along at home: https://aka.ms/csharp101notebooks 🏫 Free self-guided lear...
blinkbat
blinkbatβ€’2y ago
LINQ has Find and OrderBy, etc if you haven't learned LINQ now is a good time basically, if your goal is to do the assignment in a way that a typical dotnet pro would, use LINQ if your goal is to learn specific algorithms, study and implement those algorithms
The king of kings
The king of kingsOPβ€’2y ago
Oh! You're right. Everyone says LINQ is a great tool for implementing various stuff in projects πŸ˜ƒ I will definitely start to learn it. These are such great tips πŸ€— I will try to learn both. Although searching & sorting algorithms are a bit complicated I guess πŸ’πŸ»β€β™‚οΈ
Accord
Accordβ€’2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
The king of kings
The king of kingsOPβ€’2y ago
This website looks like a good one for practicing purposes, but sadly this site only provides two different languages Java & c++ but not c# πŸ’πŸ»β€β™‚οΈ
blinkbat
blinkbatβ€’2y ago
consider it practice to translate the code to c#
The king of kings
The king of kingsOPβ€’2y ago
Do u mean I should practice in one of the offered languages and then use the same methodology in c#?
ero
eroβ€’2y ago
the languages aren't that different just translate it directly
The king of kings
The king of kingsOPβ€’2y ago
Ah! Ok! Now I got the idea πŸ˜‰ How was your experience in learning this concept? Any tips would be appreciated πŸ€—
ero
eroβ€’2y ago
i didn't i use code other people already wrote i don't need to know sorting or Big O or anything like that it's already been done by many people many times so i just don't care and use what they made
The king of kings
The king of kingsOPβ€’2y ago
Oh! Really. So u didn't go through too many headaches by searching and learning etc... Maybe I should think about using the same method. So u mean I should use Google to find a similar code that will fit my project!
ero
eroβ€’2y ago
sure i don't know if this is an assignment or you're just learning for yourself but i generally don't really care at all about learning trivial stuff like this when it's already been solved by millions of people
The king of kings
The king of kingsOPβ€’2y ago
Ok! I get your point and I totally agree. U get to save a lot of time. For ex: I got this assignment from my course where they want me to build a weather station project and they want me to use searching & sorting algorithms. I've been what 7 days in a row trying to figure out what to do πŸ€·πŸ»β€β™‚οΈ Have I asked someone like u and gotten this tip, I wouldn't waste all these days just circling around and looking for a way to write this program πŸ™†πŸ»β€β™‚οΈ But at least, I learned something from my mistake.
ero
eroβ€’2y ago
well, if it's a course you're making yourself go through, and you don't really know how to implement something like this, it's at least worth trying to understand how the code you take from other people work like, i can at least whip a sorting algorithm up if i was asked to so it's a crucial skill to understand what you have at your disposal and how to properly put it to use. logical thinking is one of the biggest skills you need in programming the rest is being good at googling
// returns the index the value was found at
int LinearSearch(int[] array, int valueToFind)
{
for (int i = 0; i < array.Length; i++)
{
if (array[i] == valueToFind)
{
return i;
}
}

return -1;
}
// returns the index the value was found at
int LinearSearch(int[] array, int valueToFind)
{
for (int i = 0; i < array.Length; i++)
{
if (array[i] == valueToFind)
{
return i;
}
}

return -1;
}
The king of kings
The king of kingsOPβ€’2y ago
Ok! You're certainly right about everything u mentioned. Well! I tried to do some searching and find out how the logic of this concept works theoretically but just couldn't put it in a code logically. That's why it took me some days not to end up with an actual solution for the problem. Wow! This code looks good.
The king of kings
The king of kingsOPβ€’2y ago
Currently! I'm watching these shorts offered by CS50 https://www.youtube.com/watch?v=ktWL3nN38ZA
The king of kings
The king of kingsOPβ€’2y ago
Is this code considered a leaner search?
mtreit
mtreitβ€’2y ago
Linear? If you meant linear (not leaner), yes that is pretty much the definition of a linear search
The king of kings
The king of kingsOPβ€’2y ago
Yes, sorry! I meant linear search. Ok! Great. I will try to implement the project using this code snippet. Guys I need help with this step Create a class city that contains two attributes. The city class contains two member variables/attributes: string name; int temp; These store the city name and temperature, as well as the ToString function/method public string ToString() They return a string containing values in the name and temp attributes. Note that the ToString() member function/method does not generate any output to the console. Task 4-2- Compulsory. Grading scale: F-C Write a function/method for linear search in a field (array, vector, list) cities with city objects. int lensesok(field cities, int n, int searchtemp) The function/method should search a field cities for a city with a specific temperature (temp) and return the index for that city. If no city with that temperature is found -1 is returned. The variable n is the number of elements in the array.
The king of kings
The king of kingsOPβ€’2y ago
The king of kings
The king of kingsOPβ€’2y ago
It says create a function where it search for every city's temperature if it doesn't exist return -1 My question is how do I add/ assign temp values for those cities in the array so I can loop through them? as far as I know usually when using array we define two separated arrays that contains different elements and then we get the index of each list of array. Hello there
_giuraemanuel
_giuraemanuelβ€’2y ago
Hello I'm not a pro at programming, but i could give you some directions.
The king of kings
The king of kingsOPβ€’2y ago
Ok! That would be great. None of us are perfect, you're not alone.
_giuraemanuel
_giuraemanuelβ€’2y ago
I assume you want to return the city in this method if it matches valueToFind. However in this ss, you are returning the index, not the actual value. So instead of return i; you need to have return array[i]; since array[i]; represents the city returned which passes your array[i] == valueToFind condition.
how do I add/ assign temp values for those
If im assuming correctly, you want to add something to the array. You cannot add an item/city, to an array once declared since arrays have fixed length. Thats why you always, initialize an array either with a size, or initialize it directly with values. If you want to add items, you need to use List<T>. You can google C# lists, and you can find all the info that you need there. Tons of good sources to learn from.
The king of kings
The king of kingsOPβ€’2y ago
Wow, it makes very sense bro πŸ˜ƒ I haven't notice that.
_giuraemanuel
_giuraemanuelβ€’2y ago
Indeed. It's easy for small details like this to slip your grasp, especially when you're new to programming. I too went through this so i can relate πŸ™‚
_giuraemanuel
_giuraemanuelβ€’2y ago
public override string ToString()
{
return $"Name {Name} - Temp {Temp}";
}
public override string ToString()
{
return $"Name {Name} - Temp {Temp}";
}
A more cleaner way of implement ToString() If you're not familiar with this technique then you should definitely check this link https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/tokens/interpolated It's called string interpolation. It's a better way concatenating strings and variables into sentences. So to speak.
$ - string interpolation - format string output
String interpolation using the $ token provides a more readable and convenient syntax to format string output than traditional string composite formatting.
The king of kings
The king of kingsOPβ€’2y ago
Ok! Great. You're right. I have watched this tutorial and they do exactly as u described.
_giuraemanuel
_giuraemanuelβ€’2y ago
I mean, you could do it the default way until later when stuff like this becomes more relevant, but if you can learn to do it right from the start then why not? πŸ™‚ It's not complicated and people will notice this which will certain highlight your effort to learn and write quality code which won't go unappreciated!
The king of kings
The king of kingsOPβ€’2y ago
Ok! You're right. We can't use a two separated list of array and try to emerge them altogether.
_giuraemanuel
_giuraemanuelβ€’2y ago
Im not sure that's possible but i think it may be. I never done anything remotely related, but even if it is, it would overkill for the purpose of this exercise.
The king of kings
The king of kingsOPβ€’2y ago
I'm just trying to review and understand all the content that u mentioned, that's why I might seem a bit slow πŸ€—
_giuraemanuel
_giuraemanuelβ€’2y ago
Don't worry, no one is rushing you πŸ™‚ Take your time, study them as much as you need to. It's not a race! In the end, it doesn't necessarily matter how much we know, but rather, how much we understand from everything that we know.
The king of kings
The king of kingsOPβ€’2y ago
Ok! Let's follow the pattern that u mentioned, which's creating a list under the cities array. Well! Here is the question remains! How do I go loop through both the array and the list in order to search using the linear search concept? That's very wise thinking and I totally agree. You're right. I have implemented this pattern of overriding in other projects and just forgot it, Thanks a lot πŸ™
_giuraemanuel
_giuraemanuelβ€’2y ago
You have an array of cities as of now. And you are only returning the city that matches the value in valueToFind variable. So you are returning only one value. Every single time. You loop through the entire array of city and if you find the city that matches the city you're searching for in the array then you return the city found at that index. I made a mistake here. I noticed that in fact your method returns and int which is the index of the city that matched the condition. So yes, you should return i;. You could also return the city itself like i mentioned initially, but that may be outside the scope for this exercise. I meant that you could use a list if you prefer here instead of an array. It wouldn't make much difference. The thing with lists is that they are often used in conjuction with the foreach loop. In truth it's always more preferable to use lists instead of arrays. But that depends on the context. You could have a list and use for loop on it. The idea is that lists almost always go with foreach as long as you dont need the index of the item. But in your case, you need to return the index of the found city (city that matched your condition) so both array and list can work here just fine. The main difference between arrays and lists is that lists are dynamic in size. Meaning they can adapt to new lengths if needed. While an array is size bound. Meaning that an array has a fixed size that cannot be changed. Internally a list is based on the array. If you happen to add more items than the size you've declared for a list then the list will automatically expand itself to adjust to fit in the newly added elements. TLDR: You can use both a list or an array with for loop. Only use for if you need the index of an item. If you don't then always prefer lists with the foreach loop.
The king of kings
The king of kingsOPβ€’2y ago
Ok! What a great knowledge you're distinguished with bro. Your tips are very rewarding and are even more explanatory than the docs. It's mentioned in the assignment that I have a multiple options to use either (array, vector, list) although I've been using lists for all the projects I built, but not the arrays and I would prefer practice using them, because I might need use them for career purposes in the future.
_giuraemanuel
_giuraemanuelβ€’2y ago
Write a function/method for linear search in a field (array, vector, list) cities with city objects.
Im assuming this means that instead of valueToFind you need to pass in a list of values to find. If they are found. Then you return their index. In truth, you won't be returning a single index, but a list of indexes. Since you would be returning more than one index.
The king of kings
The king of kingsOPβ€’2y ago
The cities of array is not the problem, but what confuses me most is the values or the temperatures that needs to be assigned to the cities. If you review the assignment requirements, you'll notice a bit confusion when it comes to temperatures πŸ€”
_giuraemanuel
_giuraemanuelβ€’2y ago
Maybe ask for some clarifications from whomever gave you the task? Or is it something you found and decided to practice on?
The king of kings
The king of kingsOPβ€’2y ago
Oh! Really is that how it goes!
_giuraemanuel
_giuraemanuelβ€’2y ago
Write a function/method for linear search in a field (array, vector, list) cities with city objects
Also, im not sure i understand anymore. So write a method that can take either an array, vector or list? Or to contain all 3? I cant exactly offer much help if im not sure what's being asked. I think it's asking you to have a field that is either an array/list. Since vector is what youd call an array in C# anyway. And that field contains the cities. Then you search for the item you're looking for and if you find it then you return it's position/index
but not the arrays and I would prefer practice using them, because I might need use them for career purposes in the future
Fair enough, but most of the time you use lists with foreach or for if you need the index. Tho it's pretty rare to see lists with for. But yes, there's nothing wrong with practicing them. Just a little piece of advice tho. Don't be hellbent on using something just because you like it too much, or you need to practice it. What im saying is, use the appropriate tool. You may prefer idk, arrays over lists, but there are times when a list is 10 times more efficient than a list, for example. It depends on the context. So don't get too attached to something, that's all im saying. It's one thing to do the job but it's an entirely different story to do it right. So from what i'm seeing the linear search part looks done to me. I think it's time to move on to the next method. Or maybe not. I only now noticed it. But since a city needs to have a temperature that means you need to change your array data structure to something else. I'll give you a hint. Key/Value - google for a structure that stores key/value pairs
The king of kings
The king of kingsOPβ€’2y ago
This is an assignment from a course that I'm learning with here in my district in Sweden. Unfortunately my teacher is not very interactive.
_giuraemanuel
_giuraemanuelβ€’2y ago
I see. I'm sorry your teacher ain't doing his job like he should. I left you a hint, something to work with. Figure out what structure to use then use that instead of your array.
The king of kings
The king of kingsOPβ€’2y ago
So you mean I can't use an array at all? Ah! You mean I just need to change pattern of those city elements.
_giuraemanuel
_giuraemanuelβ€’2y ago
No. I mean, use something else than an array. You need to have both the City and it's assigned temperature together. So an array won't cut it here. ^ search for this then try to change your code accordingly
The king of kings
The king of kingsOPβ€’2y ago
They have provided me with this example.
The king of kings
The king of kingsOPβ€’2y ago
Ok! That's a bit weird πŸ€” why the hell they confuse me for a week using array if it's not the right option to implement the program πŸ€·πŸ»β€β™‚οΈ
_giuraemanuel
_giuraemanuelβ€’2y ago
int lensesok(field cities, int n, int searchtemp) - for this method another data structure would make more sense. Since you are supposed to return a city based on the temperature. You cannot store both the city and its designated temp inside an array. You need to use a dictionary. I suppose can use array for the first method and a dictionary for the second method tho im not sure it would make more sense.
The king of kings
The king of kingsOPβ€’2y ago
What you just mentioned makes very sense, but still confuse me.
_giuraemanuel
_giuraemanuelβ€’2y ago
I'm a junior myself, someone who started this career path on the wrong foot, struggled quite a bit, and i still have some knowledge gaps here and there. But the suggestions i make are the ones i'd try to solve the problem. It's up to you how you want to solve the problem. But for me, it makes sense to use a dictionary. If you're unfamiliar with the structure then read up on it. There are plenty of good sources to get familiar with it. Then im sure you will understand even better why i suggested this approach. It's an option. You decide how to get it done. Two things make a programmer truly great : logical thinking and google searching skills. And you need to practice these skills constantly. Its the only way to grow and get better πŸ™‚ I've gotta dash. Hit me up in private if you're still having issues. I'll try to point you in the right direction as much as i can. Good luck!
The king of kings
The king of kingsOPβ€’2y ago
I think this course is just ballshit, excuse me for saying that, the last assignment they did the same to me, I lost a whole bunch of time on a project and then after asking many members in this channel, they told me the same, you are either going to use a list only or arrays + dictionary which u would write a lot of code. I'm not going to continue with this course as soon as I finish it.
_giuraemanuel
_giuraemanuelβ€’2y ago
Yes. Chances are good that the course is a sham. You could watch Tim Correy tutorials on YouTube. They are very good for free content. Well explained and easy to grasp. He also has his own website where he sells specialized content. I learned quite a few things from this guy. You can give it a try and see for yourself. Later! In truth there are always at least 2 different approaches to solve a problem. Some fit the bill better than others.
The king of kings
The king of kingsOPβ€’2y ago
Ok! Following this guy's tutorial sounds a good idea. So we came on agreement that I either use the current array with a list or a dictionary, right?
The king of kings
The king of kingsOPβ€’2y ago
Based on your recommendations, is that mean I won't be using this code either for a linear search purposes!
Anu6is
Anu6isβ€’2y ago
is this still what you are attempting to do? or maybe I should ask, what is your actual assignment the city class contains the name and temp of the city. You don't need a dictionary
You cannot store both the city and its designated temp inside an array.
that statement ^ is misleading, you are just storing the city object (singular) not the city and its temp (they aren't separate things) ... the city object contains the temp your 4-1
Task 4-1- Compulsory. Grading scale: F-C Create a class city that contains two attributes. The city class contains two member variables/attributes: string name; int temp; These store the city name and temperature, as well as the ToString function/method public string ToString() They return a string containing values in the name and temp attributes. Note that the ToString() member function/method does not generate any output to the console.
public class City
{
public string name;
public int temp;

public City(string name, int temp)
{
this.name = name;
this.temp = temp;
}

public override string ToString()
{
return $"{name}: {temp} degrees";
}
}
public class City
{
public string name;
public int temp;

public City(string name, int temp)
{
this.name = name;
this.temp = temp;
}

public override string ToString()
{
return $"{name}: {temp} degrees";
}
}
This class creates a City object and holds to values name and temp the other part of the exercise asks you to search by temp... once you have an instance of a city, you can reference the temp by referencing yourCityInstance.temp
Task 4-2- Compulsory. Grading scale: F-C Write a function/method for linear search in a field (array, vector, list) cities with city objects. int lensesok(field cities, int n, int searchtemp) The function/method should search a field cities for a city with a specific temperature (temp) and return the index for that city. If no city with the temperature is found -1 is returned. The variable is the number of elements in the field.
public static int lensesok(City[] cities, int n, int searchtemp)
{
for (int i = 0; i < n; i++)
{
if (cities[i].temp == searchtemp)
{
return i;
}
}

return -1;
}
public static int lensesok(City[] cities, int n, int searchtemp)
{
for (int i = 0; i < n; i++)
{
if (cities[i].temp == searchtemp)
{
return i;
}
}

return -1;
}
The king of kings
The king of kingsOPβ€’2y ago
I really appreciate for jumping in and offering help for me. Although it’s 00:15 here in Sweden. So I need to sleep. But I will definitely review all your messages tomorrow πŸ˜‡ good night everyone 😴 Hey again man Are u online?
Anu6is
Anu6isβ€’2y ago
are u?
The king of kings
The king of kingsOPβ€’2y ago
Yes, sir. Great to see you again bro πŸ€— I feel very distracted and I find it hard to write this project, can I call you?
Anu6is
Anu6isβ€’2y ago
ok
The king of kings
The king of kingsOPβ€’2y ago
Great. Thanks a lot.
Accord
Accordβ€’2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server