/* ellipses (...) used to represent tabs for clarity */
int array[N]; /* N must be contant literal in this example */
bubblesort (int *a, int n) {
... int swap = 1;
... while (swap) {
... ... int i;
... ... int temp;
... ... swap = 0;
... ... for (i=0; i < n-2; i++) {
... ... ... if (a[i] > a[i+1]) {
... ... ... ... swap = 1;
... ... ... ... temp = a[i];
... ... ... ... a[i] = a[i+1];
... ... ... ... a[i+1] = temp;
... ... ... }
... ... }
... ... n--;
... }
}
/* fill the array with some data */
bubblesort (a, N);
n-1 times
In programming this could be achieved using a numeric bubble sort. Excel allows this type of sorting to be used.
rfsghdfhdh
Bubble sort is also known as sinking sort.
There are several methods available to sort numbers. A simple way to program sorting is the so-called "bubble-sort". This is inefficient for larger lists of numbers; in which case it is more convenient to use one of the faster algorithms, for example, "quick-sort".
/* PROGRAM TO SORT ARRAY ELEMENTS USING BUBBLE SORT*/ #include #include void main() { int i,j,n,t,a[50]; clrscr(); printf("ENTER THE ARRAY SIZE:\n"); scanf("%d",&n); printf("ENTER THE ARRAY ELEMENTS:\n"); for(i=0;i
Yes, bubble sort is a stable sorting algorithm.
123
Selection sort is more efficient for small datasets compared to bubble sort.
Bubble Sort is considered to have a time complexity of O(n2) because it compares each element in the list with every other element, resulting in a nested loop structure that requires n iterations for each of the n elements in the list, leading to a quadratic time complexity.
Because it is so simple. It is also inefficient, but for small data sets there is no need to be more complex, unless you are doing it many, many times in a tight loop.
ramesh