0

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 insertion sort, if only because we discussed them in Week 3.

Where do I implement that? In helpers.c? helpers.h?

I've watched the Week 3 section video twice. They say they're going to show how to do a bubble sort, but they don't have the time to show you how to do it. They go through how to do a selection sort, but at the end Jason says it's not right.

A user on Reddit was a huge help by pointing out the downloads available for the section, included the correct version of the selection sort.

Another thing that's throwing me off is up until now, there's been very little given away in the way of code. Why are they giving away the selection sort instead of letting us figure it out? Also, many of the pseudocode steps for the other types of sort give the instructions "swap them." In David's "Noswap" video, he says that you can't just swap variables around.

My question is where do I go from here?

7

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 available for you to use. These resources include functions, variables, structs, type definitions, etc (these, together, are commonly referred to as the interface).

A library's .c file is an implementation file. It, on the other hand, is the one that contains the definitions (aka implementations).

Usually, header files and implementation files go hand in hand. So if you've declared a bunch of string functions in mystring.h, you define these functions in mystring.c.

Why are they giving away the selection sort instead of letting us figure it out?

Well, the sections are optional to watch. You could have skipped watching them if you want to. Another good idea is that you could have also paused the video and tried to figure that out by yourself! Or skipped the part in which they solve the problem and watched the rest of the section. There were many solutions! :)

In David's "Noswap" video, he says that you can't just swap variables around.

I'm sure the professor didn't say that because surely we can. I believe in the world of computer science and programming, everything is possible.

As far as I remember, what the professor said is that you can't swap variables in a specific case. This case is in which you pass these variables by values to a function. We'll get to that in a minute, but please consider the following function

void swap(int a, int b)
{
    int temp = a;
    a = b;
    b = temp;
}

This simple function is supposed to be swapping two variables -- a and b. However, when we use this function

int x = 10;
int y = 20;
printf("Before swapping:\nx: %d\ny: %d\n\n", x, y);

swap(x, y);
printf("After swapping:\nx: %d\ny: %d\n", x, y);

Output:

Before swapping:
x: 10
y: 20

After swapping:
x: 10
y: 20

We clearly don't see any swapping at all. So what happened?

Well, when we passed x and y to swap(), we actually passed copies of their values. These copies are stored in a and b (swap()'s parameters). We then swap the values in a and b (the copies) and nothing really happens to the original values in x and y. That's why we don't see any swapping.

But how can we fix that?

We simply code swap like this

void swap(int *a, int *b)
{
    int temp = *a;
    *a = *b;
    *b = temp;
}

Now, when we use the new version of swap()

int x = 10;
int y = 20;
printf("Before swapping:\nx: %d\ny: %d\n\n", x, y);

swap(x, y);
printf("After swapping:\nx: %d\ny: %d\n", x, y);

Output:

Before swapping:
x: 10
y: 20

After swapping:
x: 20
y: 10

Okay what happened here?

Well, these little asterisks (i.e., *) have a meaning (you'll know more about them when you get to pointers). But, for the sake of simplicity, this way, we didn't really pass copies of the values in x and y, but instead, we passed their addresses of the original values in memory -- where they live in memory. We then coded swap() in such a way to go to these addresses and play with the values that live there. And that explains why we can see swapping this time.

| improve this answer | |
  • 1
    In the first swap function you should have b = temp; right? Please correct the code. Thanks. – vikrantnegi007 May 16 '16 at 5:50
  • @vikrantnegi007 yes, indeed. thanks for the heads-up! edited. – Kareem May 16 '16 at 9:36
-1

Programming means that you have to find your way to solve a problem, sometimes they'll give you the direct answer, sometimes they give you the way and you have to find how to implement it.

Look closely and you'll find answers: Although you may not alter our declaration of sort, you’re welcome to define your own function(s) in helpers.c that sort itself may then call.

And so on.

Bubble sort:

Loop (i) through the list
{
    Lets assume that the current index(i) is the smallest_index
    Loop (j) from the next index[i+1] in the list to the end
    {
        if values[j] is smaller than values[smallest_index]
        {
            assign the index (j) as the smallest_index
        }
    }
    swap values;
}
| improve this answer | |

You must log in to answer this question.

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