For a assignment I was to write two functions to sort a array. One using selection sort and other using insertion sort. The thing I was stuck on was the part that said, "// insert code to display intermediate permutations". I assuming that wants me to write a code to show the changes that are taking place when sorting. But, I'm not sure if that's what is expected. How do I go about showing "permutations"?

Here is the code I have for both type of sorting.\

int listMax(int list[], int begin, int end)
{
    for(int i = size - 1; i >= 1; i--)
    {
    int fMax = list[0];
    int maxIndex = 0;

    for(int j = 1; j <= ; j++)
    {
        if(fMax < list[j])
        {
            fMax = list[j];
            maxIndex = j;
        }
    }
}
}



void swap(int list[], int i, int j);
{
    if(maxIndex != i)
    {
        list[maxIndex] = list[i];
        list[i] = fMax;
    }
}


void insertSort(int list[], int size);
{
    for(int i = 1; i < size; i++)
    {
        int cElement = list[i];
        int k;
        for(k = i - 1; k >= 0 && list[k] > cElement; k--)
        {
            list[k + 1] = list[k];
        }

        list[k + 1] = cElement;
    }
}

void selectSort(int list[], int size)
{
    int maxIndex,  end = size - 1;

    while (end > 0)
    {
        maxIndex = listMax(list, 0, end);

        if( maxIndex != end )
            swap(list, end, maxIndex);

        // insert code to display intermediate permutations



        end --;
    }
}

Recommended Answers

All 6 Replies

at the line number 57 of ur s or where ever the operation on array is happening (usually at the iterations ) . write a print array function and pass the array to the function . thus after each operation the array manipulations will be displayed , thus enlightening the user about each iteration.

So, I get the printing part. I just had a question about finding the index of the current max number, I have to use the following parameters to find it, it's confusing as I've never done it that way.

void listMax(int list[], int begin, int end)

begin = beginning index
end = ending index

int listMax(int list[], int begin, int end)
	{
	for(int end = size - 1; end >= 1; end--)
		{
		int cMax = list[0];
		int cMaxIndex = 0;

		for(int

So, I get the printing part. I just had a question about finding the index of the current max number, I have to use the following parameters to find it, it's confusing as I've never done it that way.

void listMax(int list[], int begin, int end)

begin = beginning index
end = ending index

int listMax(int list[], int begin, int end)
	{
	for(int end = size - 1; end >= 1; end--)
		{
		int cMax = list[0];
		int cMaxIndex = 0;

		for(int

What is the purpose of this function? What does it do? If it's supposed to find the array index of the array element with the largest value, what does it do with it and is it supposed to change the array at all?

What is the purpose of this function? What does it do? If it's supposed to find the array index of the array element with the largest value, what does it do with it and is it supposed to change the array at all?

You're right it's a function to find the index of the largest value. I just have it like that because the teacher gave us that to find it. I guess I'm better off not using that.

int listMax(int list[], int begin, int end)
{
    for(int i = size - 1; i >= 1; i--)
    {
    int fMax = list[0];
    int maxIndex = 0;

    for(int j = 1; j <= ; j++)
    {
        if(fMax < list[j])
        {
            fMax = list[j];
            maxIndex = j;
        }
    }
}
}

The function is supposed to return an int, so you need to make sure it does that.

What does size represent? You don't need two for-loops. You only need one. You need to keep track of one variable: the index of the maximum. You could have an fMax variable, but I wouldn't bother since you have the array itself. Regardless, don't initialize anything to 0 or list[0]. For all you know, 0 is not between begin and end. You index must be in that range, so initialize the index to begin.

Here's a skeleton.

int listMax(int list[], int begin, int end)
{
    int largestIndex = begin;

    for (int i = begin + 1; i <= end; i++)
    {
        if (/* compare two values */)
        {
             // change value of largestIndex
        }
    }

    return largestIndex;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.