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.
Here comes one more as an article to your other duplicate thread - Bubble sort and Headp Sort. Thread closed.