7
votes
pset3: sort -- having trouble connecting all the dots
Where do I implement that? In helpers.c? helpers.h?
A library's .h file is a header file. It does NOT contain definitions. Instead, it contains code that declares the library's resources that are ...
3
votes
Accepted
Pset3 Segmentation Fault
One of the skills that are indirectly taught in this class, as well as being one of the most important skills you must learn as a programmer is how to debug a program. You will run into seg faults ...
3
votes
Accepted
Pset3 Search/Find problems
I don't want to sound cruel here, but your sort routine has serious problems. It is incomplete, it has code that appears to serve no purpose ( an if statement with no code to process if true), and ...
3
votes
pset3 --> bubble sort
The problem is that you are swapping values[i] with values[i+1] while also allowing i to go to a maximum of n-1.
Let's say that n=10, so the array has 10 elements, 0 through 9. When the code sorts ...
3
votes
Accepted
Bubble sort - omega or lower bound
In Big O notation and Omega notation the constants are not computed. Say you have an array with 1000 places. It's not that different to say 1000 than 999. So the n-1 becomes n. Also let's say that you ...
3
votes
I don't understand how the worst case performance of bubble sort is O(n^2)
Well, the number of iterations you did was basically
n + (n - 1) + (n - 2) + ... + 1
Mathematically, this is equal to n (n + 1) / 2 which is equal to n^2 + n / 2 and since we don't care about small ...
3
votes
Accepted
Pset3: Tideman sort_pairs doesn't work?
preferences[pairs[i].winner][pairs[i].loser] > preferences[pairs[i+1].winner][pairs[i+1].loser]
This condition swaps the elements in ascending order while check50 checks for the pairs array for ...
2
votes
Accepted
<value optimized> GDB self optimizing everything
"This is a bug in clang, which has been fixed in version 3.5. The CS50 team is working on getting an update out with update50." -see link
Variable optimized out eventhough it will be used later
2
votes
Selection sort does not sort the unsorted portion once no swapping position is reached?
The reason why 100 is ignored is because you're comparing values[min] with values[j] only. And since j is initially set to i + 1 which is 1, values[0] is totally ignored.
The role of the nested loop ...
2
votes
Accepted
pset 3 GDB: "no executable file specified"
To debug "find" with gdb, enter the following command:
gdb find
You need to enter the filename of the executable as a parameter following gdb on the command line. Don't use ./ or .c when you enter ...
2
votes
pset3 find: seg fault (int array won't initialize to 0s)
I used the debugger on it, and it looked perfectly fine. temp[0] to temp[65535] were initially 0.
Have you tried using debug50 on it, like ./generate 1000 50|debug50 ./find 127? Maybe the ...
2
votes
Accepted
CS50x helpers.c help with looping
Your sort is essentially correct. It does restart at the beginning of the list each time. The only problem is that it is going beyond the end of the array.
The array has n elements, numbered from ...
1
vote
Pset3 - Code sorts, but does not find the needle using generate
Your sort is flawed, causing it to eject the largest value and add 0 to the list. Look at the printout of the list to confirm.
In the first pass through the outer for loop, k=0. This allows i to ...
1
vote
PSET3 find having trouble. Can someone help?
The
array[start] < array[end]
is the wrong condition. You want to keep looping as long as the search interval has elements, so use
start <= end
instead (<= as you chose both start and ...
1
vote
Accepted
CS50 Helpers.C Find Less, No Idea What's Going On
Both search and sort have problems. First, sort isn't complete. It only makes one pass through the list, so it will only bubble sort the largest value to the last position. I suggest a review of the ...
1
vote
PSET 3, there is nothing happening
Your do..while loop really should be a regular while loop, but it would blow up only if your array to search was empty (then you're accessing values[-1] within the loop).
Your condition is wrong. ...
1
vote
Accepted
Segmentation fault for pset3 sorting
The code creates the counter array:
int counter[65535];
but doesn't initialize it. What is in the array? Try printing out the contents of the array (or maybe the first 20 elements) ...
1
vote
Accepted
pset3 helpers.c (help)
You are comparing to element values[(n+1)/2], but your recursive calls look like you meant values[n/2] instead, which makes a lot more sense, as n/2 is always the middle element's index, or the higher ...
1
vote
Accepted
Pset 3 sort function won't swap
apparently, you're trying to implement selection sort. the problem appears to be with this condition
if (values[j] < values [i])
{
min = j;
}
are you sure you wanna update min every time ...
1
vote
Accepted
'redefinition of i' compilation error pset 3 sort
The syntax for loop consists of ; and not ,.
for (int i = 0 ; i < n ; i++)
{
// your stuff here
}
1
vote
Accepted
Why doesn't my sort algorithm work
Your problem is in your sorting. Check the Wikipedia article about bubble sort implementation: https://en.wikipedia.org/wiki/Bubble_sort#Implementation. In the most non-optimized case, the pseudo-code ...
1
vote
Accepted
pset3 - find less / sort video availability
The URL should be updated in the spec shortly. Here it is in the meantime:
https://www.youtube.com/watch?v=TYnkJckP1Ss
1
vote
Accepted
Pset3 - Find: I can't find what's wrong with my sorting algorithm...?
In search, move the calculation of mid into the loop, and make sure the /2 is outside the parentheses.
The sorting algorithm has no potential for an infinite loop, and looks correct.
Also, it's ...
1
vote
Accepted
is this function correct? CS50 Pset3 (Find)
There are problems here. First, it only makes one pass through the array, so all it will do is push the largest value to the end. The rest of the list will likely not sort. Next, since i can increase ...
1
vote
Accepted
pset3: sort -- Selection Sort Implementation
Looks like some kind of select sort. For that to work, in the outer loop, before the inner loop, set min to i ("smallest non-sorted element so far is at index i"), and in the inner loop (starting at i+...
1
vote
pset3: selection sort and binary search
I figured it out. It seems like I didn't manage to swap values[i] and values[min] in selection sort the right way, so I did a bit of researching and saw that I need a third integer to do that. Here is ...
1
vote
Pset3 - my bubble sort breaks the find function
If the list is not already sorted, you have an infinite loop. the counter variable will be incremented at some point, meaning that the while condition is true. Since counter is only incremented and ...
1
vote
Accepted
is my swap function breaking my bubble sort implementation?
The problem here is that the code only makes one pass through the list. It will bubble the largest element to the end of the list, but then stops. It needs to come back to the beginning and repeat ...
1
vote
Accepted
Help with sorting function
The above code is partially correct. Although the logic seems to be appropriate, it is not efficient. All you have to do is set the value of i as minimum every time you enter the 'i' loop, and then ...
1
vote
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
sort × 68pset3 × 55
find × 19
search × 13
helpers × 6
bubble-sort × 5
cs50x × 4
c × 4
binary-search × 4
tideman × 4
bubble × 4
segmentation-fault × 3
gdb × 3
binary × 3
selectionsort × 3
check50 × 2
week3 × 2
check × 1
array × 1
runoff × 1
help × 1
plurality × 1
structs × 1
lectures × 1
sorting × 1