Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 15

DATA STRUCTURES

Bubble sort

Presented By: Joylin Pinto , 2sem


Presented to: Aswath Narayan S
HOD,BCA Department
SORTING ALGORITHMS

A Sorting Algorithm is used to rearrange a given


array or list elements according to a comparison
operator on the elements. The comparison
Types
operatorofis Sorting
used to decide the new order of
element in the respective data structure.
1. Bubble Sort
2. Selection Sort
3. Insertion Sort
4. Merge Sort
5. Quick Sort
Data Structures 6. Heap Sort
What is Bubble Sort?

• Bubble sort is a simple sorting algorithm. This sorting algorithm is


comparison-based algorithm in which each pair of adjacent elements is
compared and the elements are swapped if they are not in order.
• This algorithm is not suitable for large data sets as its average and
worst case complexity are of Ο(n2) where n is the number of items.
BUBBLE • Worst and Average Case Time Complexity:

SORT O(n*n). Worst case occurs when array is reverse sorted.


• Best Case Time Complexity: O(n).
Best case occurs when array is already sorted.
• Boundary Cases:
Bubble sort takes minimum time (Order of n) when elements are
already sorted.

Data Structures
HOW?

Data Structures
0 1 2 3 4 5
7 -1 4 0 12 2
N = 6 j=0 i=0 j<6-0-1 j<5
a[j]>a[j+1]
7> -1  swap
0 1 2 3 4 5
-1 7 4 0 12 2
N = 6 j=1 i=0 j<6-0-1 j<5
a[j]>a[j+1]
7> 4  swap
0 1 2 3 4 5
-1 4 7 0 12 2
0 1 2 3 4 5
-1 4 7 0 12 2
N = 6 j=2 i=0 j<6-0-1 j<5
a[j]>a[j+1]
7> 0  swap
0 1 2 3 4 5
-1 4 0 7 12 2
N = 6 j=3 i=0 j<6-0-1 j<5
a[j]>a[j+1]
7> 12  no swap
0 1 2 3 4 5
-1 4 0 7 12 2
0 1 2 3 4 5
-1 4 0 7 12 2
N = 6 j=4 i=0 j<6-0-1 j<5
a[j]>a[j+1]
12> 2  swap
0 1 2 3 4 5
-1 4 0 7 2 12
0 1 2 3 4 5
-1 4 0 7 2 12
12
N = 6 j=0 i=1 j<6-1-1 j<4
a[j]>a[j+1]
-1> 4  no swap
0 1 2 3 4 5
-1 4 0 7 2 12
12
N = 6 j=1 i=0 j<6-1-1 j<4
a[j]>a[j+1]
4> 0  swap
0 1 2 3 4 5
-1 0 4 7 12 122
0 1 2 3 4 5
-1 0 4 7 2 12
12
N = 6 j=2 i=1 j<6-1-1 j<4
a[j]>a[j+1]
4> 7  no swap
0 1 2 3 4 5
-1 0 4 7 2 12
12
N = 6 j=3 i=1 j<6-1-1 j<4
a[j]>a[j+1]
7> 2  swap
0 1 2 3 4 5
-1 0 4 2 7 12
12
0 1 2 3 4 5
-1 0 4 2 7 12
12
N = 6 j=0 i=2 j<6-2-1 j<3
a[j]>a[j+1]
-1> 0  no swap
0 1 2 3 4 5
-1 0 4 2 7 12
12
N = 6 j=1 i=2 j<6-2-1 j<3
a[j]>a[j+1]
0> 4 no swap
0 1 2 3 4 5
-1 0 4 2 7 12
2
0 1 2 3 4 5
-1 0 4 2 7 12
12
N = 6 j=2 i=2 j<6-2-1 j<3
a[j]>a[j+1]
4> 2  swap
0 1 2 3 4 5
-1 0 2 4 7 12
12

0 1 2 3 4 5
-1 0 2 4 7 12
//Logic
//Documentation for(i = 0; i<n; i++)
#include<stdio.h> {
#include<conio.h> for(j = 0; j<n-i-1; j++)
Int main () {
if(a[j] > a[j+1])
{
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
//Inputs }
{ }
int i, j, temp, a[50];
Printf(“Enter the number of //Output results
elements”); printf("Printing Sorted Element List ...\n");
Scanf(“%d,&n”); for(i = 0; i<n; i++)
Printf(“Enter the elements”); {
For(i=0;i<n;i++); printf("%d\n",a[i]);
Scanf(“%d”,&a[i]); }
}
THANK YOU

You might also like