Home Page class 12 @ Python

CBSE Class 12 : Data Structures

Introduction to Data Structures

CBSE Class 12 : Searching

Creation And Traversal
What is Searching?
Linear/Sequential Search
Binary Search
Insertion Of Element
Deletion Of Element

CBSE Class 12 : Sorting

What is sorting?
Bubble Sort
Selection Sort
Insertion Sort

CBSE Class 12 : Stacks

Applications of Stacks
Implementation of Stacks

CBSE Class 12 : Queues

Applications Of Queues
Implementation of Queues

Class 12: Data Structures Sorting 2

Bubble sort

The basic idea in bubble sort is to compare two successor elements and interchange them if they are not in proper order. After each pass the largest unsorted element is placed at its correct position.

If there are ā€œnā€ elements then
After the first pass the largest element is placed at (n-1) th position. After the second pass the 2nd largest element is placed at n-2 th position and so on.
All the elements will be sorted after a maximum of n-1 passes.

But it is possible that the elements may get sorted before n-1 passes. So there should be a way to detect that the array is sorted or not. And for the same we use a variable swapped.