SlideShare a Scribd company logo
BCAC 232:
Data Structures
SORTING
 Sorting is process of arranging the elements in a particular order. The
order may be ascending order or descending order. The advantage of
sorting is effective data accessing.
 Types of sorting: There are 2 types of sorting.
1. Internal sorting
2. External sorting
 Internal sorting: If all the elements (records) to be sorted are in the
main memory then such a sorting is called internal sorting.
 External sorting: If some of the elements (records) to be sorted are in
the secondary storage or disk such a sorting is called External
sorting.
Types of sorting techniques:
1. Bubble sort (Exchange sort or Sinking sort).
2. Selection sort.
3. Insertion sort.
4. Quick sort.
5. Merge sort.
6. Heap sort.
7. Shell sort
8. Radix sort.
9. Address calculation sort.
Efficiency of a sorting technique:
 How to select a sorting technique for a given set of elements?
 There are number of sorting techniques available to sort a given array
of data items. Each sorting technique has its own advantages and
disadvantages. Different techniques are useful in different applications.
 There are 3 most important factors are counted while selecting a
sorting technique, which are.
1. Coding time: The amount of time invested in writing full length
sorting program.
2. Execution time (Time complicity): The amounts of time required
execute the sorting program. This normally frequency of execution of
statements in a program i.e. number of times statements are
executed.
3. Memory requirement (Space complicity): The amount of memory
required to store the entire sorting program in main memory while
execution.
Analysis of a sorting technique:
 Analysis of a sorting technique depends of three factors, which are code
time, time complicity and space complicity. Among these 3 factors while
analyzing a sorting technique we mainly concentrate more on the time
complicity.
 The time complicity is amount of time required to execute the sorting
program. Which is analyzed in terms of 3 cases
1. Best case
2. Worst case
3. Average case
BUBBLE SORT
 It is most popular sorting technique among all other techniques
because is very simple to understand and implement. It is also called
exchange or sinking sort
 Working of Bubble Sort
 The algorithm begins by comparing the element at the bottom of the
array with next element. If the first element is grater the second
element, then are swapped or exchanged.
 This process in then repeated for next two elements i.e. for second and
third element. After n-1 comparisons the largest of all data items
bubbles up to the top of the array.
 The first n-1 comparisons constitute first pass. During second pass
number of comparison is one les than previous pass i.e. there are n-2
comparisons in the second pass. During second pass second largest
element bubbles up to the last but one position.
 Consider following array A of elements.
A
A[0] A[1] A[2] A[3] A[4]
Begin the sort by comparing first two elements
Compare A[0] and A[1]. Since 30>10, interchange
Compare A[1] and A[2]. Since 30>5, interchange Pass 1
Compare A[2] and A[3]. Since 30>20, interchange
Compare A[3] and A[4]. Since 30>15, interchange
Largest element 30 has bubble up to last position
30 10 5 20 15
30 10 5 20 15
10 30 5 20 15
10 5 30 20 15
10 5 20 30 15
10 5 20 15 30
Compare A[0] and A[1]. Since 10>5, interchange
Compare A[1] and A[2]. Since 10<20, no interchange Pass 2
Compare A[2] and A[3]. Since 20>15, interchange
Second largest element bubbles up to the position last but one.
Compare A[0] and A[1]. Since 5<10, no interchange
Pass 3
Compare A[1] and A[2]. Since 10<15, no interchange
Third largest element is in its right position.
Compare A[0] and A[2]. Since 5>10, no interchange Pass 4
Final sorted array after n-1 passes.
10 5 20 15 30
5 10 20 15 30
5 10 20 15 30
5 10 15 20 30
5 10 15 20 30
5 10 15 20 30
5 10 15 20 30
5 10 15 20 30
5 10 15 20 30
Algorithm:
Algorithm: BUBBLE_SORT(A, n) This algorithm sort a given array A[n] using bubble sort technique. Variables I and J are
used to index the array and temp is a temporary variable.
Step1: start
Step2: Input the array A[n]
Step3: [Compute the sorting]
Repeat For I0 to n-1
Step4: [Compare the adjacent elements]
Repeat For J0 to n-1-I
Step5: If (A[J]>A[J+1])
[Interchange A[J] and A[J+1]]
TempA[J]
A[J]A[J+1]
A[J+1]temp
[End If]
[End step3 for loop]
[End step4 for loop]
Step6: [Display output]
Repeat For I0 to n-1
Output A[I]
[End for]
Step9: stop
Analysis of bubble sort:
 Best case: If the given array of elements is in the ascending order, the
outer for loop will be executed n-1 times. The inner for loop and if
statement will be executed n-1 times for the first iteration of the outer
for loop, n-2 times for the second iteration of the outer for loop and so
on . Only one time during the n-1th iteration of the outer for loop. The
interchange part will not be executed even once.
 Worst case: : If the given array of elements is in reverse order, the outer
for loop will be executed n-1 times. The inner for loop, if statement and
interchange part will be executed n-1 times for the first iteration of the
outer for loop, n-2 times for the second iteration of the outer for loop
and so on. Only one time during the n-1th iteration of the outer for loop.
Hence maximum number of comparisons and interchange operations.
 Advantages:
1. Simple to understand and implement.
2. Very straight forward.
3. Better than selection sort.
 Disadvantages:
1. It runs slowly and hence it is not efficient, because more efficient sorting
techniques are available.
2. Even if array is sorted, n-1 comparisons are required.
Computer sciencebubble sorting algorithm

More Related Content

Similar to Computer sciencebubble sorting algorithm (20)

Selection Sort with Improved Asymptotic Time Bounds
Selection Sort with Improved Asymptotic Time Bounds
theijes
 
DSA-sortijejjejjdjjdjdjjsjsjsjsjsjsjng.pptx
DSA-sortijejjejjdjjdjdjjsjsjsjsjsjsjng.pptx
suryatom5775
 
simple-sorting algorithms
simple-sorting algorithms
Ravirajsinh Chauhan
 
Sorting algorithms bubble sort to merge sort.pdf
Sorting algorithms bubble sort to merge sort.pdf
AyeshaMazhar21
 
Chapter 3 - Elementary Searching and Sorting Algorithms.ppt
Chapter 3 - Elementary Searching and Sorting Algorithms.ppt
AbdisaAwel
 
Comparative Performance Analysis & Complexity of Different Sorting Algorithm
Comparative Performance Analysis & Complexity of Different Sorting Algorithm
paperpublications3
 
my docoment
my docoment
NeeshanYonzan
 
Algorithm By AMT.pptx
Algorithm By AMT.pptx
AungMyintTun3
 
Chapter3.pptx
Chapter3.pptx
ASMAALWADEE2
 
Selection sort
Selection sort
asra khan
 
Sorting algorithums > Data Structures & Algorithums
Sorting algorithums > Data Structures & Algorithums
Ain-ul-Moiz Khawaja
 
SIMPLE SORTING MUKUND
SIMPLE SORTING MUKUND
Mukund Trivedi
 
Alternate Sort
Alternate Sort
IOSR Journals
 
Daa chapter5
Daa chapter5
B.Kirron Reddi
 
Data Structures 6
Data Structures 6
Dr.Umadevi V
 
Dsa – data structure and algorithms sorting
Dsa – data structure and algorithms sorting
sajinis3
 
Algo PPT.pdf
Algo PPT.pdf
sheraz7288
 
Data Structures_ Sorting & Searching
Data Structures_ Sorting & Searching
ThenmozhiK5
 
DS - Unit 2 FINAL (2).pptx
DS - Unit 2 FINAL (2).pptx
prakashvs7
 
Sorting pnk
Sorting pnk
pinakspatel
 
Selection Sort with Improved Asymptotic Time Bounds
Selection Sort with Improved Asymptotic Time Bounds
theijes
 
DSA-sortijejjejjdjjdjdjjsjsjsjsjsjsjng.pptx
DSA-sortijejjejjdjjdjdjjsjsjsjsjsjsjng.pptx
suryatom5775
 
Sorting algorithms bubble sort to merge sort.pdf
Sorting algorithms bubble sort to merge sort.pdf
AyeshaMazhar21
 
Chapter 3 - Elementary Searching and Sorting Algorithms.ppt
Chapter 3 - Elementary Searching and Sorting Algorithms.ppt
AbdisaAwel
 
Comparative Performance Analysis & Complexity of Different Sorting Algorithm
Comparative Performance Analysis & Complexity of Different Sorting Algorithm
paperpublications3
 
Algorithm By AMT.pptx
Algorithm By AMT.pptx
AungMyintTun3
 
Selection sort
Selection sort
asra khan
 
Sorting algorithums > Data Structures & Algorithums
Sorting algorithums > Data Structures & Algorithums
Ain-ul-Moiz Khawaja
 
Dsa – data structure and algorithms sorting
Dsa – data structure and algorithms sorting
sajinis3
 
Data Structures_ Sorting & Searching
Data Structures_ Sorting & Searching
ThenmozhiK5
 
DS - Unit 2 FINAL (2).pptx
DS - Unit 2 FINAL (2).pptx
prakashvs7
 

Recently uploaded (20)

Chalukyas of Gujrat, Solanki Dynasty NEP.pptx
Chalukyas of Gujrat, Solanki Dynasty NEP.pptx
Dr. Ravi Shankar Arya Mahila P. G. College, Banaras Hindu University, Varanasi, India.
 
Exploring Ocean Floor Features for Middle School
Exploring Ocean Floor Features for Middle School
Marie
 
Introduction to Generative AI and Copilot.pdf
Introduction to Generative AI and Copilot.pdf
TechSoup
 
Analysis of Quantitative Data Parametric and non-parametric tests.pptx
Analysis of Quantitative Data Parametric and non-parametric tests.pptx
Shrutidhara2
 
Sustainable Innovation with Immersive Learning
Sustainable Innovation with Immersive Learning
Leonel Morgado
 
ABCs of Bookkeeping for Nonprofits TechSoup.pdf
ABCs of Bookkeeping for Nonprofits TechSoup.pdf
TechSoup
 
june 10 2025 ppt for madden on art science is over.pptx
june 10 2025 ppt for madden on art science is over.pptx
roger malina
 
Final Sketch Designs for poster production.pptx
Final Sketch Designs for poster production.pptx
bobby205207
 
MATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKAN
aditya23173
 
Overview of Off Boarding in Odoo 18 Employees
Overview of Off Boarding in Odoo 18 Employees
Celine George
 
Rai dyansty Chach or Brahamn dynasty, History of Dahir History of Sindh NEP.pptx
Rai dyansty Chach or Brahamn dynasty, History of Dahir History of Sindh NEP.pptx
Dr. Ravi Shankar Arya Mahila P. G. College, Banaras Hindu University, Varanasi, India.
 
PEST OF WHEAT SORGHUM BAJRA and MINOR MILLETS.pptx
PEST OF WHEAT SORGHUM BAJRA and MINOR MILLETS.pptx
Arshad Shaikh
 
Paper 109 | Archetypal Journeys in ‘Interstellar’: Exploring Universal Themes...
Paper 109 | Archetypal Journeys in ‘Interstellar’: Exploring Universal Themes...
Rajdeep Bavaliya
 
Ray Dalio How Countries go Broke the Big Cycle
Ray Dalio How Countries go Broke the Big Cycle
Dadang Solihin
 
Battle of Bookworms 2025 - U25 Literature Quiz by Pragya
Battle of Bookworms 2025 - U25 Literature Quiz by Pragya
Pragya - UEM Kolkata Quiz Club
 
How to Configure Vendor Management in Lunch App of Odoo 18
How to Configure Vendor Management in Lunch App of Odoo 18
Celine George
 
How to Manage & Create a New Department in Odoo 18 Employee
How to Manage & Create a New Department in Odoo 18 Employee
Celine George
 
BUSINESS QUIZ PRELIMS | QUIZ CLUB OF PSGCAS | 9 SEPTEMBER 2024
BUSINESS QUIZ PRELIMS | QUIZ CLUB OF PSGCAS | 9 SEPTEMBER 2024
Quiz Club of PSG College of Arts & Science
 
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
aditya23173
 
FEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdf
FEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdf
ChristinaFortunova
 
Exploring Ocean Floor Features for Middle School
Exploring Ocean Floor Features for Middle School
Marie
 
Introduction to Generative AI and Copilot.pdf
Introduction to Generative AI and Copilot.pdf
TechSoup
 
Analysis of Quantitative Data Parametric and non-parametric tests.pptx
Analysis of Quantitative Data Parametric and non-parametric tests.pptx
Shrutidhara2
 
Sustainable Innovation with Immersive Learning
Sustainable Innovation with Immersive Learning
Leonel Morgado
 
ABCs of Bookkeeping for Nonprofits TechSoup.pdf
ABCs of Bookkeeping for Nonprofits TechSoup.pdf
TechSoup
 
june 10 2025 ppt for madden on art science is over.pptx
june 10 2025 ppt for madden on art science is over.pptx
roger malina
 
Final Sketch Designs for poster production.pptx
Final Sketch Designs for poster production.pptx
bobby205207
 
MATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKAN
aditya23173
 
Overview of Off Boarding in Odoo 18 Employees
Overview of Off Boarding in Odoo 18 Employees
Celine George
 
PEST OF WHEAT SORGHUM BAJRA and MINOR MILLETS.pptx
PEST OF WHEAT SORGHUM BAJRA and MINOR MILLETS.pptx
Arshad Shaikh
 
Paper 109 | Archetypal Journeys in ‘Interstellar’: Exploring Universal Themes...
Paper 109 | Archetypal Journeys in ‘Interstellar’: Exploring Universal Themes...
Rajdeep Bavaliya
 
Ray Dalio How Countries go Broke the Big Cycle
Ray Dalio How Countries go Broke the Big Cycle
Dadang Solihin
 
Battle of Bookworms 2025 - U25 Literature Quiz by Pragya
Battle of Bookworms 2025 - U25 Literature Quiz by Pragya
Pragya - UEM Kolkata Quiz Club
 
How to Configure Vendor Management in Lunch App of Odoo 18
How to Configure Vendor Management in Lunch App of Odoo 18
Celine George
 
How to Manage & Create a New Department in Odoo 18 Employee
How to Manage & Create a New Department in Odoo 18 Employee
Celine George
 
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
aditya23173
 
FEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdf
FEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdf
ChristinaFortunova
 
Ad

Computer sciencebubble sorting algorithm

  • 2. SORTING  Sorting is process of arranging the elements in a particular order. The order may be ascending order or descending order. The advantage of sorting is effective data accessing.  Types of sorting: There are 2 types of sorting. 1. Internal sorting 2. External sorting  Internal sorting: If all the elements (records) to be sorted are in the main memory then such a sorting is called internal sorting.  External sorting: If some of the elements (records) to be sorted are in the secondary storage or disk such a sorting is called External sorting.
  • 3. Types of sorting techniques: 1. Bubble sort (Exchange sort or Sinking sort). 2. Selection sort. 3. Insertion sort. 4. Quick sort. 5. Merge sort. 6. Heap sort. 7. Shell sort 8. Radix sort. 9. Address calculation sort.
  • 4. Efficiency of a sorting technique:  How to select a sorting technique for a given set of elements?  There are number of sorting techniques available to sort a given array of data items. Each sorting technique has its own advantages and disadvantages. Different techniques are useful in different applications.  There are 3 most important factors are counted while selecting a sorting technique, which are. 1. Coding time: The amount of time invested in writing full length sorting program. 2. Execution time (Time complicity): The amounts of time required execute the sorting program. This normally frequency of execution of statements in a program i.e. number of times statements are executed. 3. Memory requirement (Space complicity): The amount of memory required to store the entire sorting program in main memory while execution.
  • 5. Analysis of a sorting technique:  Analysis of a sorting technique depends of three factors, which are code time, time complicity and space complicity. Among these 3 factors while analyzing a sorting technique we mainly concentrate more on the time complicity.  The time complicity is amount of time required to execute the sorting program. Which is analyzed in terms of 3 cases 1. Best case 2. Worst case 3. Average case
  • 6. BUBBLE SORT  It is most popular sorting technique among all other techniques because is very simple to understand and implement. It is also called exchange or sinking sort  Working of Bubble Sort  The algorithm begins by comparing the element at the bottom of the array with next element. If the first element is grater the second element, then are swapped or exchanged.  This process in then repeated for next two elements i.e. for second and third element. After n-1 comparisons the largest of all data items bubbles up to the top of the array.  The first n-1 comparisons constitute first pass. During second pass number of comparison is one les than previous pass i.e. there are n-2 comparisons in the second pass. During second pass second largest element bubbles up to the last but one position.
  • 7.  Consider following array A of elements. A A[0] A[1] A[2] A[3] A[4] Begin the sort by comparing first two elements Compare A[0] and A[1]. Since 30>10, interchange Compare A[1] and A[2]. Since 30>5, interchange Pass 1 Compare A[2] and A[3]. Since 30>20, interchange Compare A[3] and A[4]. Since 30>15, interchange Largest element 30 has bubble up to last position 30 10 5 20 15 30 10 5 20 15 10 30 5 20 15 10 5 30 20 15 10 5 20 30 15 10 5 20 15 30
  • 8. Compare A[0] and A[1]. Since 10>5, interchange Compare A[1] and A[2]. Since 10<20, no interchange Pass 2 Compare A[2] and A[3]. Since 20>15, interchange Second largest element bubbles up to the position last but one. Compare A[0] and A[1]. Since 5<10, no interchange Pass 3 Compare A[1] and A[2]. Since 10<15, no interchange Third largest element is in its right position. Compare A[0] and A[2]. Since 5>10, no interchange Pass 4 Final sorted array after n-1 passes. 10 5 20 15 30 5 10 20 15 30 5 10 20 15 30 5 10 15 20 30 5 10 15 20 30 5 10 15 20 30 5 10 15 20 30 5 10 15 20 30 5 10 15 20 30
  • 9. Algorithm: Algorithm: BUBBLE_SORT(A, n) This algorithm sort a given array A[n] using bubble sort technique. Variables I and J are used to index the array and temp is a temporary variable. Step1: start Step2: Input the array A[n] Step3: [Compute the sorting] Repeat For I0 to n-1 Step4: [Compare the adjacent elements] Repeat For J0 to n-1-I Step5: If (A[J]>A[J+1]) [Interchange A[J] and A[J+1]] TempA[J] A[J]A[J+1] A[J+1]temp [End If] [End step3 for loop] [End step4 for loop] Step6: [Display output] Repeat For I0 to n-1 Output A[I] [End for] Step9: stop
  • 10. Analysis of bubble sort:  Best case: If the given array of elements is in the ascending order, the outer for loop will be executed n-1 times. The inner for loop and if statement will be executed n-1 times for the first iteration of the outer for loop, n-2 times for the second iteration of the outer for loop and so on . Only one time during the n-1th iteration of the outer for loop. The interchange part will not be executed even once.  Worst case: : If the given array of elements is in reverse order, the outer for loop will be executed n-1 times. The inner for loop, if statement and interchange part will be executed n-1 times for the first iteration of the outer for loop, n-2 times for the second iteration of the outer for loop and so on. Only one time during the n-1th iteration of the outer for loop. Hence maximum number of comparisons and interchange operations.
  • 11.  Advantages: 1. Simple to understand and implement. 2. Very straight forward. 3. Better than selection sort.  Disadvantages: 1. It runs slowly and hence it is not efficient, because more efficient sorting techniques are available. 2. Even if array is sorted, n-1 comparisons are required.

Editor's Notes

  • #7: Therefore array n elements requires n-1 passes