# Questions tagged [sort]

The tag has no usage guidance.

66 questions
Filter by
Sorted by
Tagged with
32 views

### Trying to understand the insertion sort algorithm

While I was working on the PSet3, I've realized that I don't really understand the insertion sort algorithm the way it is presented in one of the short CS50 videos. So I commented out each line of ...
23 views

### How to “update” the haystack array not using pointers?

Context: In helpers.c, I can make the sort function actually sort the values[] array. Problem: However, I don't know how to re-inject this sorted array back in main without using pointers. Question: ...
41 views

### Pset 3 -> Counting Sort - need tips to find my error

What is wrong in my code? Why am I getting a segmentation fault? void sort(int values[], int n) { int aux_list[65536] = {0}; //map the count of values in the values list as the index of the aux list ...
104 views

### Help finding segmentation fault

In my code, I get a segmentation fault some, but not all of the times that I run it. Sometimes, it sorts the code and finds the needle, sometimes it doesn't sort correctly and ends with a segmentation ...
57 views

### is my selection code correct?

I used printf to check the process and final result. The final result is technically correct (the array is sorted in ascending order) but I saw the behaivior of the algorithm and it doesnt look like ...
35 views

### pset3 keeps returning 1 even when it should return 0

bool search(int value, int values[], int n) { // TODO: implement a searching algorithm if (value < 0) return false; int s = 0; int e = n - 1; int m = ((s + e) / 2); while (s <= e) { ...
132 views

### How does one print out all existing integers in an int array?

The code below generates the following error: format specifies type 'int' but the argument has type 'int *' [-Werror,-Wformat] printf("%i", array); include include int main(void) { int ...
48 views

### Check 50 for find

I have gotten most of my code to work for the search and sort, but I still have a few issues. One is i my for loop, I have it set to less than 100, though I need to make it less than log n, but I ...
44 views

### pset3 find code works, despite 'sort' failing

I'm very confused! I have spent many (MANY) hours working through this pset. I've gotten to the point where my code passes all check50 requirements for 'find', but it's failing the 'sort' ...
342 views

### I can't check sort in Helpers.c

After reading the other questions, I still don't get how to check it. I tried to use gdb on ./find, but while I ran it, I could only see what was expected. Then, while typing n, it said "in fileops.c"...
201 views

### Sort is not working on Pset3

After many hours can't figure out why the sorting and searching is not working... When running tests it I always get "Didn't find needle in haystack" whether the value is in or not in the array... ...
524 views

### CS50 Problem Set 3 Helpers.c PROBLEMS

I have been trying to solve Pset3 search and sort - looked at all possible online forums but I cannot solve it. Please help. Where am I going wrong? Is my binary search OK? What about my bubble sort? ...
233 views

### pset3: The sort function does not return my code

I am stuck with the implementation of the sort function. I have written a code, but I can't check if the code is correct. This is what I have done thus far: void sort(int values[], int n) { for (int ...
563 views

### How to use swap?

I am currently in pset3, implementing sort. I chose to do selection sort and at the end of it I need to swap the numbers, but when I try to compile, I get this message: implicit declaration of ...
110 views

### Receiving an expected expression error on pset3 (curly braces match)

Tad bit confused as to why I'm receiving an expected expression error. I've looked at other questions for help on this, but the solution seems to be the curly braces. I've actually done this, and I'm ...
134 views

### Tideman sort_pairs function

I tested this sort function separately and it sorts alright, but somehow doesn't pass the check50 test. The pairs.strength is the winning strength, recorded previously in add_pairs function. All my ...
48 views

### cs50x tideman sort not work

// Sort pairs in decreasing order by strength of victory void sort_pairs(void) { // TODO printf("=-------------------------- sort---------------pair=%d\n",pair_count); pair pairx[...
124 views

### Pset3: Tideman sort_pairs doesn't work?

I know this has been asked before but I've checked every thread and couldn't find a solution. According to check50, my code for sort_pairs() doesn't work, and I can't figure out what could be wrong ...
128 views

### Problem with plurality

when i check50 my code I get this message : :( print_winner identifies Bob as winner of election print_winner function did not print winner of election :( print_winner identifies Charlie as ...
225 views

### tideman pset3 sort pairs - code doesn't work

I know it's not efficient. But I don't know why it doesn't work...:( void sort_pairs(void) { pair sorted_pairs[pair_count]; int strength[pair_count]; for (int k = 0; k < pair_count; k+...
849 views

### Pset3: Tideman sort_pairs not work

I have been trying to implement sort_pairs function using selection sort algorithm. After testing for every case, the sort_pairs is returning right answer but check50 is marking it as wrong. for (int ...
215 views

### check50 claiming pairs are sorted incorrectly

On Problem Set 3, check50 has flagged my program as not having sorted the pairs correctly. However, the pairs array appears to be sorted in every case I've tried. I used insertion sort and had ...
4k views

### I don't understand how the worst case performance of bubble sort is O(n^2)

The course materials say the worst case of bubble sort is O(n2) ā when every item in the list is in the worst possible position. I'm having trouble wrapping my mind around this. If I have a list of ...
28 views

### PSet3 helpers.c fails one of the checks

I'm working on the PSet3 Find problem. Here's my code for the sort and search functions: bool search(int value, int values[], int n) { if (value < 0) return false; else { ...
18 views

### Find's sort: instructions vs code: O(n) or O

The docs for find's sort say, The running time of your implementation must be in O(n), where n is the arrayās size. Yes, linear! Keep in mind that 65,536 is a constant. However, the code that we'...
139 views

### Purpose of sizeof(book)/sizeof(string) in lectures' binary sort?

string book[] = { "Chen", "Kernighan", "Leitner", "Lewis", "Malan", "Muller", "Seltzer", "Shieber", "Smith" } int main(...
2k views

### pset3: selection sort and binary search

I've been trying to implement those two function whole day and something seems to not be working because the printf function I wrote in find.c outputs 9,8,7,6,5,6,7,8,9 when I input the numbers from 9 ...
190 views

### pset3 help with find, bubble-sort and search

I've been struggling for over a week with this and can't figure out how to get my program to pass check50. I'm getting very interesting mixed results from check50: :) helpers.c exists. :) helpers.c ...
239 views

### PSET3 find having trouble. Can someone help?

For some reason bubble sort is not working. Can someone help. binary search bool search (int target, int array[],int length) { int end = length-1; int start = 0; while (array[start] < ...
58 views

I actually try to get the hang of using bubble sort. So i did a little practice in a online code space with a 10 int array, but somehow it doesnĀ“t work competely right yet. #include <stdio.h> ...
514 views

### CS50 Helpers.C Find Less, No Idea What's Going On

Final Code: /** * helpers.c * * Helper functions for Problem Set 3. */ #include <cs50.h> #include "helpers.h" /** * Returns true if value is in array of n values, else false. */ bool ...
42 views

### PSET 3, there is nothing happening

My code is not working at all and I am not able to find a proper solution. UPDATE: So I just now came to know that I am not able to use ^d if needle is not found otherwise it is working. /** * ...
53 views

### Segmentation fault for pset3 sorting

Can't seem to figure out this segmentation fault and what's causing it. My sorting algo works fine for 100 generated numbers, but at some point things start to go awry. I'm adding 1 to a counter[] ...
38 views

### Concept of run time in lecture week 3

In week 3's lecture, David talked about the concept of runtime where the worst possible scenario for say insertion sort is O(n squared) and the best case scenario would be omega(n). I dont understand ...
126 views

### pset3 helpers.c (help)

:( finds 42 in {39,40,41,42} \ expected an exit code of 0, not 1 ( it meaning I missing or have a return error??? ) bool search(int value, int values[], int n) {// ...
71 views

### pset3 find: seg fault (int array won't initialize to 0s)

Ok so I made what I believe is a counting sort. I first wrote it in Java just to test it, and it works there, but when I transferred it to CS50 IDE (of course, I modified it slightly to C syntax) it ...
66 views

### pset3 - find less / sort video availability

Is anyone else having issues with the sort video for pset3, under find, less comfortable? URL is: http://docs.cs50.net/problems/find/less/find.html, which points to: https://www.youtube.com/watch?v=...
78 views

### Pset3 - Find: I can't find what's wrong with my sorting algorithm…?

I have an issue with the "find" problem. When I test my program, it seems to get stuck in an infinite loop (nothing is prompted, there is just a blank space and I have to kill the program with ctrl c)....
256 views

### Pset3 - Code sorts, but does not find the needle using generate

I have the following code for helpers.c. When I run find, if I input numbers by hand, it sorts them and finds/doesn't find the n correctly. However, when I use generate to input the numbers, I can see ...
123 views

### is this function correct? CS50 Pset3 (Find)

// TODO: implement an O(n^2) sorting algorithm int c,i,s; c = 0; for (i=0; i < n; i++) { if (values[i] > values[i + 1]) { s = values[i]; values[i] = values[i+1]; ...
145 views

### pset3: sort — Selection Sort Implementation

I have been working away at my implementation of Insertion Sort for this problem set, and I've run into a predicament. I have gotten the sort function almost functional, however I'm sure we all know ...
361 views

### pset3 binary search won't find values when at end of array

As the title says, the program doesn't pass the check when 42 is the last element of the array. I've seen a related post about this issue but it did not give me enough to fix the problem. The code is ...
64 views

### binary search code showing segmentation fault

this is my code. bool search(int value, int values[], int n) { int max = n -1; int min = 0; int mid; while(n > 0) { mid = ((min = max)/2); if(values[mid] == ...
50 views

### Pset3 - my bubble sort breaks the find function

I have tried to code bubble sort as my sort function, but when I run find it no longer exits when using ctrl d and just gives me a blank terminal line. My code is below, i'd be grateful if someone ...
68 views

### is my swap function breaking my bubble sort implementation?

I'm trying to implement the bubble sort algorithm in pset3, but it's not working! Pretty sure it's because of failure to swap. Been working on it for a while, with no success. Here's the relevant code:...
721 views

### Help with sorting function

I am trying to implement an insertion sort algorithm for pset3, but I believe the problem lies in how I swap the values at the end of the code. Any clues on how to fix this? void sort(int values[], ...
2k views

### pset3: sort — having trouble connecting all the dots

I'm not sure what to work on. The pset3 instructions say: Go ahead and implement sort so that the function actually sorts... Odds are youāll want to implement bubble sort, selection sort, or ...
45 views

### Where is sort supposed to go?

I am working on pset3 at the moment and while all problem sets so far had their challanges this one got me a little lost! I implemented search into helpers.c. Now I am supposed to implement sort. But ...