Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   Bubble sort and Headp Sort (http://www.go4expert.com/forums/bubble-sort-headp-sort-t6477/)

Peter_APIIT 22Sep2007 13:02

Bubble sort and Headp Sort
 
Hello all expert C and C++ programmer, i truly new to programming.

1. I have coded the bubble sort but i cannot sort it in ascending order.

Below is my code:

Code:

#include<iostream>

using std::cout;
using std::cin;


void bubbleSort(int [], int);


// ------------------------------------------
int main(int argc, char *argv[])
{
        int sequence[5] = { 5, 3 , 4, 2, 1};
        bubbleSort(sequence, 5);
        return 0;
}
// ------------------------------------------
void bubbleSort(int sequence[], int size)
{
        int inner_loop, outer_loop, temp;

       
        for (outer_loop=0;outer_loop<size;++outer_loop)
        {
                for (inner_loop=1;inner_loop<size;++inner_loop)
                {
                        if (sequence[outer_loop] > sequence[inner_loop])
                        {
                                temp = sequence[outer_loop];
                                sequence[outer_loop] = sequence[inner_loop];
                                sequence[inner_loop] = temp;
                        }
                }
        }

        for (inner_loop=0;inner_loop<size;++inner_loop)
        {
                cout << "The array[" << inner_loop
                        << "] : " << sequence[inner_loop] << "\n";
        }
}

Why this code is not sort the array into ascending but this give the correct result ?

Code:

#include<iostream>

using std::cout;
using std::cin;


void bubbleSort(int [], int);


// ------------------------------------------
int main(int argc, char *argv[])
{
        int sequence[5] = { 5, 3 , 4, 2, 1};
        bubbleSort(sequence, 5);
        return 0;
}
// ------------------------------------------
void bubbleSort(int sequence[], int size)
{
        int inner_loop, outer_loop, temp;

        for (inner_loop=4;inner_loop>=0;inner_loop--)
        {
                for (outer_loop=1;outer_loop<size;++outer_loop)
                {
                        if (sequence[outer_loop - 1] > sequence[outer_loop])
                        {
                                temp = sequence[outer_loop - 1];
                                sequence[outer_loop - 1] = sequence[outer_loop];
                                sequence[outer_loop] = temp;
                        }
                }
        }

        for (inner_loop=0;inner_loop<size;++inner_loop)
        {
                cout << "The array[" << inner_loop
                        << "] : " << sequence[inner_loop] << "\n";
        }
}


2. Can u explain to me the heap sort algorithm ? What is siftdown ?

Thanks for your help.

Your help is greatly appreciated by me and others.

shabbir 22Sep2007 13:14

Re: Bubble sort and Headp Sort
 
By this time you should be aware what is an article and you should not be submitting your queries as articles. As of now I have moved them in the forum for discussion.

Peter_APIIT 22Sep2007 14:15

Re: Bubble sort and Headp Sort
 
Sorry for my blind eye. Thanks shabir.

DaWei 22Sep2007 16:33

Re: Bubble sort and Headp Sort
 
Your array only has a few items. If you will go through your algorithm with pencil and paper, you will discover your problem.

A heap siftdown is the method for restoring the heapness of the tree after the root has been removed.

Peter_APIIT 29Oct2007 17:39

Re: Bubble sort and Headp Sort
 
Finally, i have understand the algorithm.

Basically, the outer_loop has enforce the inner loop to loop again and again until the array is sorted.

Below is the code:

Code:

void bubbleSort(int sequence[], int size)
{
        int inner_loop, outer_loop, temp;

        for (inner_loop=4;inner_loop>=0;inner_loop--)
        {
                for(outer_loop=1;outer_loop<size;++outer_loop)
                {
                        if (sequence[outer_loop - 1] > sequence[outer_loop])
                        {
                                temp = sequence[outer_loop - 1];
                                sequence[outer_loop - 1] = sequence[outer_loop];
                                sequence[outer_loop] = temp;
                        }
                }
        }

This algorithm used the concept of queue where one person stand in front of the queue and another person standing back of the queue, move accordingly and if not sorted, the outer_loop(Manager) enforce the inner_loop(Worker) to start again and again until it is sorted.

Imagine this case.

I hope this code and algorithm help others.


All times are GMT +5.5. The time now is 12:44.