1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Bubble sort and Headp Sort

Discussion in 'C' started by Peter_APIIT, Sep 22, 2007.

Thread Status:
Not open for further replies.
  1. Peter_APIIT

    Peter_APIIT New Member

    Joined:
    Apr 11, 2007
    Messages:
    92
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Student
    Location:
    Malaysia
    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.
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,285
    Likes Received:
    364
    Trophy Points:
    83
Thread Status:
Not open for further replies.

Share This Page