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

PPT
Sorting algorithms
PPT
Data Structure (MC501)
PPTX
Data structure using c module 3
PPT
Lecture_4 (Sorting Algorithms) before mids - Copy.ppt
PPTX
Unit III Version I.pptx
PDF
Ijcse13 05-01-048
PDF
Ijcse13 05-01-048
PPTX
Sorting-Algorithms-A-Comprehensive-Guide.pptx
Sorting algorithms
Data Structure (MC501)
Data structure using c module 3
Lecture_4 (Sorting Algorithms) before mids - Copy.ppt
Unit III Version I.pptx
Ijcse13 05-01-048
Ijcse13 05-01-048
Sorting-Algorithms-A-Comprehensive-Guide.pptx

Similar to Computer sciencebubble sorting algorithm (20)

DOCX
Sorting
PDF
Selection Sort with Improved Asymptotic Time Bounds
PPTX
DSA-sortijejjejjdjjdjdjjsjsjsjsjsjsjng.pptx
PPT
simple-sorting algorithms
PDF
Sorting algorithms bubble sort to merge sort.pdf
PPT
Chapter 3 - Elementary Searching and Sorting Algorithms.ppt
PDF
Comparative Performance Analysis & Complexity of Different Sorting Algorithm
PPTX
my docoment
PPTX
Algorithm By AMT.pptx
PPTX
Chapter3.pptx
DOC
Selection sort
PPT
Sorting algorithums > Data Structures & Algorithums
PPT
SIMPLE SORTING MUKUND
PDF
Alternate Sort
PDF
Daa chapter5
PPT
Data Structures 6
PPTX
Dsa – data structure and algorithms sorting
PDF
Algo PPT.pdf
PPTX
Data Structures_ Sorting & Searching
PPTX
DS - Unit 2 FINAL (2).pptx
Sorting
Selection Sort with Improved Asymptotic Time Bounds
DSA-sortijejjejjdjjdjdjjsjsjsjsjsjsjng.pptx
simple-sorting algorithms
Sorting algorithms bubble sort to merge sort.pdf
Chapter 3 - Elementary Searching and Sorting Algorithms.ppt
Comparative Performance Analysis & Complexity of Different Sorting Algorithm
my docoment
Algorithm By AMT.pptx
Chapter3.pptx
Selection sort
Sorting algorithums > Data Structures & Algorithums
SIMPLE SORTING MUKUND
Alternate Sort
Daa chapter5
Data Structures 6
Dsa – data structure and algorithms sorting
Algo PPT.pdf
Data Structures_ Sorting & Searching
DS - Unit 2 FINAL (2).pptx
Ad

Recently uploaded (20)

PDF
medical_surgical_nursing_10th_edition_ignatavicius_TEST_BANK_pdf.pdf
PPTX
What’s under the hood: Parsing standardized learning content for AI
PDF
advance database management system book.pdf
PDF
HVAC Specification 2024 according to central public works department
PDF
LEARNERS WITH ADDITIONAL NEEDS ProfEd Topic
PPTX
Module on health assessment of CHN. pptx
DOCX
Cambridge-Practice-Tests-for-IELTS-12.docx
PDF
MBA _Common_ 2nd year Syllabus _2021-22_.pdf
PDF
FOISHS ANNUAL IMPLEMENTATION PLAN 2025.pdf
PDF
David L Page_DCI Research Study Journey_how Methodology can inform one's prac...
PDF
Empowerment Technology for Senior High School Guide
PDF
MICROENCAPSULATION_NDDS_BPHARMACY__SEM VII_PCI .pdf
PDF
Uderstanding digital marketing and marketing stratergie for engaging the digi...
PPTX
B.Sc. DS Unit 2 Software Engineering.pptx
PDF
My India Quiz Book_20210205121199924.pdf
PDF
Environmental Education MCQ BD2EE - Share Source.pdf
PPTX
Virtual and Augmented Reality in Current Scenario
PDF
Complications of Minimal Access-Surgery.pdf
PPTX
Introduction to pro and eukaryotes and differences.pptx
PDF
BP 505 T. PHARMACEUTICAL JURISPRUDENCE (UNIT 1).pdf
medical_surgical_nursing_10th_edition_ignatavicius_TEST_BANK_pdf.pdf
What’s under the hood: Parsing standardized learning content for AI
advance database management system book.pdf
HVAC Specification 2024 according to central public works department
LEARNERS WITH ADDITIONAL NEEDS ProfEd Topic
Module on health assessment of CHN. pptx
Cambridge-Practice-Tests-for-IELTS-12.docx
MBA _Common_ 2nd year Syllabus _2021-22_.pdf
FOISHS ANNUAL IMPLEMENTATION PLAN 2025.pdf
David L Page_DCI Research Study Journey_how Methodology can inform one's prac...
Empowerment Technology for Senior High School Guide
MICROENCAPSULATION_NDDS_BPHARMACY__SEM VII_PCI .pdf
Uderstanding digital marketing and marketing stratergie for engaging the digi...
B.Sc. DS Unit 2 Software Engineering.pptx
My India Quiz Book_20210205121199924.pdf
Environmental Education MCQ BD2EE - Share Source.pdf
Virtual and Augmented Reality in Current Scenario
Complications of Minimal Access-Surgery.pdf
Introduction to pro and eukaryotes and differences.pptx
BP 505 T. PHARMACEUTICAL JURISPRUDENCE (UNIT 1).pdf
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