0

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 tried to do that and could not get it to work. The second issue is that if you do ./find 42, and your numbers are 39 40 41 43, it still finds 42. Thanks for the help, my code is:

 /**
 * helpers.c
 *
 * Helper functions for Problem Set 3.
 */

#include <cs50.h>
#include <stdio.h>
#include "helpers.h"
#include <math.h>

/**
 * Returns true if value is in array of n values, else false.
 */
bool search(int value, int values[], int n)
{
    if (value < 0) {
        return false;
    }
    int low = values[0];
    int high = values[n - 1];
    int mid = (low + high) / 2;
    //printf("Low: %i Mid: %i High %i\n", low, mid, high);
    for (int i = 0; i < 100; i++) {

        if (mid == value) {
            return true;
        }

        else if (mid < value) {
            low = mid + 1;
            mid = (low + high) / 2;
        }

        else {
            high = mid - 1;
            mid = (low + high) / 2;
        }
    }
    //printf("Low: %i Mid: %i High %i\n", low, mid, high);
     //TODO: implement a searching algorithm
    return false;
}

/**
 * Sorts array of n values.
 */
void sort(int values[], int n)
{
    int swapcount = 1;   
    //int iterations = 0;
    //int swapcount = 1;
        /*if (n == 1) {
            return;
        }*/



            for (int i = 0; i < n-1 ; i++) {
                if (values[i] > values[i + 1]) {
                    printf("before swap %i, %i\n", values[i], values[i + 1]);
                    int temp = values[i];
                    values[i] = values[i + 1];
                    values[i + 1] = temp;
                    printf("after swap %i, %i,\n", values[i], values[i + 1]);
                    swapcount++;
                }

                /*else {
                    printf("Nothing to swap\n");
                }*/

                /*for (int j = 0; j < n; j++) {
                        printf("%i\n", values[j]);
                }*/
            }
            //return 0;
        if (swapcount == 1) {
            return;
            for (int j = 0; j < n; j++) {
                printf("Sorted array is %i", values[j]);
            }

        }

            //iterations ++;
            /*printf("%i Iterations\n", iterations);*/
            /*printf("Sorted is\n");
            for (int j = 0; j < n; j++) {
                printf("%i\n", values[j]);
            }*/
            //printf("Your swapcount is %i\n", swapcount);
            printf("%i swaps\n", swapcount);
            for (int w = 0; w < n; w++) {
                printf("array is %i\n", values[w]);
            }
            sort(values, n - 1);


    // TODO: implement a sorting algorithm
    //return;
}

1 Answer 1

0

One of the most immediate problems comes from your for loop. You should have the loop for high + low, to accommodate if there are more than 100 items in the array values[].

6
  • I noticed that, but I'm not sure how to fix it. I tried log n but it didn't work correctly
    – Joel Banks
    Commented Jul 13, 2017 at 17:21
  • what do you mean "I tried log n"
    – Nova
    Commented Jul 13, 2017 at 17:34
  • I tried putting i < log n because that is the maximum running time for a binary search, but I couldn't get it to compile correctly
    – Joel Banks
    Commented Jul 13, 2017 at 17:45
  • That is not correct syntax for c. Your program can't know what is log of n times, that is something humans can discover (to my knowledge) Let's keep up on forum maintenance if your question is solved! Good luck
    – Nova
    Commented Jul 13, 2017 at 17:57
  • So what would be the right value to put in there?
    – Joel Banks
    Commented Jul 13, 2017 at 20:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .