0% found this document useful (0 votes)
7 views5 pages

Bubble Sort

The document describes bubble sort, an algorithm for sorting a set of numbers in ascending order. It works by comparing adjacent elements and swapping them if they are in the wrong order. The algorithm requires the numbers to be sorted in place through multiple passes over the data, with each pass involving fewer comparisons since the highest values bubble up to the end.

Uploaded by

gangothrid03
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views5 pages

Bubble Sort

The document describes bubble sort, an algorithm for sorting a set of numbers in ascending order. It works by comparing adjacent elements and swapping them if they are in the wrong order. The algorithm requires the numbers to be sorted in place through multiple passes over the data, with each pass involving fewer comparisons since the highest values bubble up to the end.

Uploaded by

gangothrid03
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Sorting

Index
1 97 A[1] 97

Array, A
2 82 A[2] 82
3 76 A[3] 76
4 29 A[4] 29
5 17 A[5] 17

Sorting :- Arranging a set of items in a particular order.


Ascending or Descending order.
Bubble Sort :- Comparing adjacent elements and arranging them
in order.
Problem :- Sort a set of N numbers in Ascending order
Input : Value of N, N numbers { A[1..N] of N nos}
Output: N numbers in sorted order
index Comparing Elements

1 97 82 82 82 82 (1,2) (2,3) (3,4) (4,5)


2 82 97 76 76 76 J <- 1 to 4
3 76 76 97 29 29 Pass-1 J <- 1 to (5-1)
4 29 29 29 97 17 ( j, j+1)
5 17 17 17 17 97

1 82 76 76 76 (1,2) (2,3) (3,4)


2 76 82 29 29 J <- 1 to 3
3 29 29 82 17 Pass-2 J <- 1 to (5-2)
4 17 17 17 82 ( j, j+1)
5 97 97 97 97

1 76 29 29 (1,2) (2,3)
2 29 76 17 J <- 1 to 2
3 17 17 76 Pass-3 J <- 1 to (5-3)
4 82 82 82 ( j, j+1)
5 97 97 97

1 29 17 (1,2)
2 17 29 J <- 1 to 1
3 76 76 Pass-4 J <- 1 to (5-4)
4 82 82 ( j, j+1)
5 97 97
N numbers Pass

J <- 1 to (N-1) Pass-1

J <- 1 to (N-2) Pass-2 No. of No. of


Numbers Passes
5 4
N N-1

i <- 1 to N-1
J <- 1 to (N-3) Pass-3 J <- 1 to ( N-i )
If A[j] > A[j+1) then
Interchange A[j] and A[j+1]

J <- 1 to (N-4) Pass-4


...
...
...
J <- 1 to N-(N-1) Pass-(N-1)
Bubble Sort
Problem: Arrange a set of N numbers in ascending order
Input : Value of N, N numbers { ie. An array A[1..N] of N elements}
Output : Sorted list of numbers
Pseudo Code
Start
Input N
For i = 1 to N step 1 // A[1] .. A[N]
Input A[ i ]
EndFor
For i = 1 to N-1 Step 1 // N-1 passes
For j = 1 to N-i Step 1
If A[j] > A[j+1] Then
Let temp = A[j] // Interchange
Let A[j] = A[j+1]
Let A[j+1] = temp
Endif
EndFor // inner loop
EndFor
For i = 1 to N step 1 // Output Sorted numbers
Print A[ i ]
EndFor
Stop
Bubble Sort
Problem: Arrange a set of N numbers in ascending order
Input : Value of N, N numbers { ie. An array A[0..N-1] of N elements}
Output : Sorted list of numbers
Pseudo Code
Start
Input N
For i = 0 to N-1 Step 1 // A[0] .. A[N-1]
Input A[ i ]
EndFor
For i = 1 to N-1 Step 1 // N-1 passes
For j = 0 to N-i-1 Step 1
If A[j] > A[j+1] Then
Let temp = A[j] // Interchange
Let A[j] = A[j+1]
Let A[j+1] = temp
Endif
EndFor // inner loop
EndFor
For i = 0 to N-1 step 1 // Output Sorted numbers
Print A[ i ]
EndFor
Stop

You might also like