- Related Questions & Answers
- Selected Reading
Jun 09, 2014 Selection sort. Insertion Sort. Bubble Sort:-Bubble sorting is the very commonly and widely used sorting technique in C programming. It is also known as the exchange sort. It repeatedly visits the elements of an array and compares the two adjacent elements. Bubble sort is a simple sorting algorithm that works by repeatedly stepping through the list to be sorted, comparing each pair of adjacent items and swapping them if they are in the wrong order. The pass through the list is repeated until no swaps are needed, which indicates that the list is sorted.
Bubble Sort is comparison based sorting algorithm. In this algorithm adjacent elements are compared and swapped to make correct sequence. This algorithm is simpler than other algorithms, but it has some drawbacks also. This algorithm is not suitable for large number of data set. It takes much time to solve the sorting tasks.
The complexity of Bubble Sort Technique
Time Complexity: O(n) for best case, O(n2) for average and worst case
Space Complexity: O(1)
Algorithm
bubbleSort(array, size)
Input: An array of data, and the total number in the array
Output: The sorted Array
Example Code
Output
bool true and false are essentially just the same and int 0 and 1. But bool is considered better to use for cases like this as it makes it more obvious what you are trying to do. Instead of declaring it asint swap;
do Bubble Sort Code Dev C 2017
bool swap;
are set/check them as true or false instead of 0 or 1.Second, If you happen to have two input numbers that are already in sort order, then your loop will terminate prematurely due to line 52 if I'm not mistaken. |
Yea, like I've said before, I'm pretty sure just getting rid of that entire else if statement should fix the problem.