1 Dec 2019

  • December 01, 2019
  • Amitraj
1. Bubble Sort

-> Bubble sort is a type of sorting.
-> It is used for sorting 'n' (number of items) elements.
-> It compares all the elements one by one and sorts them based on their values.



-> In the above diagram, element 40 is greater than 10, so these values must be swapped. This operation continues until the array is sorted in ascending order.


/* C program to implement Bubble Sort */


#include<stdio.h>
int main()
{
int a[100],n,i,j,temp;
printf("Enter array  size:");
scanf("%d",&n);
printf("Enter %d numbers:",n);
for(i=0; i<n; i++)
scanf("%d",&a[i]);
for(i=0; i<n; i++)
for(j=0; j<n-1-i; j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
printf("Elements after sorting:\n");
for(i=0; i<n; i++)
printf("%d  ",a[i]);
return 0;
}

OUTPUT:

Enter array  size:5
Enter 5 numbers:40 10 20 30 50
Elements after sorting:
10  20  30  40  50

Related Posts:

  • Sorting in Data Structure | Sorting Techniques Sorting -> Arranging data elements in a meaningful order is known as sorting. -> we can arrange numbers either in ascending or descending order.                    &nb… Read More
  • Graph Data Structure Graph Data Structure -> Graph is an abstract data type. -> A Graph is a non-linear data structure, is a collection of nodes and edges. The nodes are sometimes also referred to as vertices and the edges are lines&nbs… Read More
  • Heap Sort | Sorting Techniques in Data Structure Heap Sort -> In heap sort, heap removes the largest element from the end of partially sorted array. Reconstruct the heap after removing largest element & again remove the largest element from remaining elements and p… Read More
  • Merge Sort | Sorting Techniques in Data Structure Merge Sort -  -> Merge sort is a sorting technique based on divide and conquer technique. With worst-case time complexity being Ο(n log n), it is one of the most respected algorithms. -> Merge sort first divides… Read More
  • Quick Sort | Sorting Techniques in Data Structure Quick sort -> Quick sort is an efficient sorting algorithm which is based on divide and conquer algorithm. In this sorting technique it picks a pivot value and divide the given array around the picked pivot. /* C progr… Read More

Translate

Popular Posts