Bubble sort, also known as sinking 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. The algorithm gets its name from the way smaller elements "bubble" to the top of the list. Because it only uses comparisons to operate on elements, it is a comparison sort. Although the algorithm is simple, it is not efficient for sorting large lists; other algorithms are better.
#include <stdio.h>
int main()
{
int array[100], n, i, j, temp;
printf("Enter number of elements\n");
scanf("%d", &n);
printf("Enter %d integers\n", n);
for (i = 0; i < n; i++)
scanf("%d", &array[i]);
for (i = 0 ; i < ( n - 1 ); i++)
{
for (j = 0 ; j < n - i - 1; j++)
{
if (array[j] > array[j+1]) /* For decreasing order use < */
{
temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
}
printf("Sorted list in ascending order:\n");
for ( i = 0 ; i < n ; i++ )
printf("%d\n", array[i]);
return 0;
}
Theory of Algorithms
15-01-2004
Heapsort - Sebastian Streich, Enric Meinhardt 8
Subroutines for Heaps
Heapify(A,i)
l := largest node of i and its children
if ( i ≠ l )
exchange A[i] with A[l]
Heapify(A, l)
A real implementation should not be recursive!
(overhead when passing the function's arguments)
types of sorting in c language are: insertion sort selection sort bubble sort merge sort two way merge sort heap sort quick sort
Bubble sort is also known as sinking sort.
ramesh
Never. Bubble sort is often cited as an example of how not to write a sorting algorithm and is used purely as a programming exercise. It is never used in production code. Although reasonably efficient when sorting small lists, an insertion sort performs better on average. But for larger lists it has no practical uses. A merge sort is better for large lists, but if stability isn't an issue a quick sort is even better. Hybrid sorts typically use quick sort until a partition is small enough for an insertion sort to complete the job.
create spiral in c programming?
types of sorting in c language are: insertion sort selection sort bubble sort merge sort two way merge sort heap sort quick sort
Bubble sort is also known as sinking sort.
There is a built-in qsort function, see stdlib.h
In programming this could be achieved using a numeric bubble sort. Excel allows this type of sorting to be used.
quick sort is a divide and conquer method , it is not dynamic programming
C and C++ are both high-level programming languages.
ramesh
Never. Bubble sort is often cited as an example of how not to write a sorting algorithm and is used purely as a programming exercise. It is never used in production code. Although reasonably efficient when sorting small lists, an insertion sort performs better on average. But for larger lists it has no practical uses. A merge sort is better for large lists, but if stability isn't an issue a quick sort is even better. Hybrid sorts typically use quick sort until a partition is small enough for an insertion sort to complete the job.
Write your own C++ functions for the following problems:o Sort a book list in a library based on the disciplineo Print the sorted output on the console
create spiral in c programming?
Bubble sort is an "in place" algorithm. Other than a temporary "switch" variable, no extra space is required.
Robert Lafore has written: 'Object-oriented programming in Microsoft C++' -- subject(s): Object-oriented programming (Computer science), C++ (Computer program language), C. 'Microsoft C. Programming for the I.B.M.Personal Computer' 'Object-oriented programming in C++' 'Windows Programming Made Ridiculously Easy Book' 'The Waite Group's C Programming Using Turbo C++ (The Waite Group)' 'Microsoft C programming for the IBM' 'The Waite Group's Microsoft C programming for the PC' -- subject(s): C (Computer program language), IBM microcomputers, Microsoft C (Computer program), Microsoft C., Programming