0% found this document useful (0 votes)
19 views31 pages

Bubble Sort

Sorting is the process of arranging elements in a collection in a specific order, which can be for ordering or categorizing purposes. Various sorting algorithms exist, including Bubble Sort, Selection Sort, and Quick Sort, each with distinct methods of organizing data. The document illustrates the Bubble Sort algorithm in detail, explaining its mechanics and providing examples of its application.

Uploaded by

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

Bubble Sort

Sorting is the process of arranging elements in a collection in a specific order, which can be for ordering or categorizing purposes. Various sorting algorithms exist, including Bubble Sort, Selection Sort, and Quick Sort, each with distinct methods of organizing data. The document illustrates the Bubble Sort algorithm in detail, explaining its mechanics and providing examples of its application.

Uploaded by

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

Data Structures

Sorting Algorithms
Sorting
• Sorting is the process of placing elements from a collection in some
kind of order.
• A sorting algorithm is an algorithm that puts elements of a list in a
certain order.
• Sorting is one of the most important operations performed by
computers.
• Its process of arranging items systematically, and has two common,
yet distinct meanings:
1. ordering: arranging items in a sequence ordered by some criterion;
2. categorizing: grouping items with similar properties.
Sorting
• Sorting = ordering.

• Sorted = ordered based on a particular way.

• Generally, collections of data are presented in a sorted manner.

• Examples of Sorting:
• Words in a dictionary are sorted (and case distinctions are ignored).

• Files in a directory are often listed in sorted order.

• The index of a book is sorted (and case distinctions are ignored).


Sorting
• Many banks provide statements that list checks in increasing
order (by check number).
• In a newspaper, the calendar of events in a schedule is generally sorted by date.

• Musical compact disks in a record store are generally sorted by recording artist.

• Why?
• Imagine finding the phone number of your friend in your mobile phone, but the
phone book is not sorted.
Sorting Applications
• To prepare a list of student ID, names, and scores in a table
(sorted by ID or name) for easy checking.

• To prepare a list of scores before letter grade assignment.

• To produce a list of horses after a race (sorted by the


finishing times) for payoff calculation.

• To prepare an originally unsorted array for ordered binary


searching.
Type of Sorting Algorithmic
1. Bubble Sort

2. Selection Sort

3. Insertion Sort

4. Merge Sort

5. Quick Sort

CIS 068
Other Sorting Algorithms (2nd Year Work)

• Heap Sort

• Counting Sort

• Radix Sort

• Shell Sort
Bubble Sort
• Bubble sort examines the array from start to finish, comparing elements as it
goes.

• Any time it finds a larger element before a smaller element, it swaps the two.

• In this way, the larger elements are passed towards the end.

• The largest element of the array therefore "bubbles" to the end of the array.

• Then it repeats the process for the unsorted portion of the array until the
whole array is sorted.
Bubble Sort

• Bubble Sort is an algorithm which is used to sort N elements that are in an Array

• Bubble Sort compares all the element one by one and sort them based on their
values.

• It is called Bubble sort, because with each iteration the smaller element in the
list bubbles up towards the first place, just like a water bubble rises up to the
water surface.

• Sorting takes place by stepping through all the data items one-by-one in pairs
and comparing adjacent data items and swapping each pair that is out of order
Bubble Sort
• Algorithm
• Define the entire array as the unsorted portion of the array.
• While the unsorted portion of the array has more than one
element:
1. For every element in the unsorted portion, swap with the next
neighbor if it is larger than the neighbor.
2. Reduce the size of the unsorted portion of the array by 1.
Bubble Sorting Example
• Sorting takes an unordered collection and makes
it an ordered one.
1 2 3 4 5 6

77 42 35 12 101 5

1 2 3 4 5 6

5 12 35 42 77 101
"Bubbling Up" the Largest Element
• Traverse a collection of elements
• Move from the front to the end
• “Bubble” the largest value to the end using pair-wise comparisons and
swapping

1 2 3 4 5 6

77 42 35 12 101 5
"Bubbling Up" the Largest Element
• Traverse a collection of elements
• Move from the front to the end
• “Bubble” the largest value to the end using pair-wise comparisons and
swapping

1 2 3 4 5 6

7742 Swap4277 35 12 101 5


"Bubbling Up" the Largest Element
• Traverse a collection of elements
• Move from the front to the end
• “Bubble” the largest value to the end using pair-wise comparisons and
swapping

1 2 3 4 5 6

42 7735 Swap 3577 12 101 5


"Bubbling Up" the Largest Element
• Traverse a collection of elements
• Move from the front to the end
• “Bubble” the largest value to the end using pair-wise comparisons and
swapping

1 2 3 4 5 6

42 35 7712 Swap 12
77 101 5
"Bubbling Up" the Largest Element
• Traverse a collection of elements
• Move from the front to the end
• “Bubble” the largest value to the end using pair-wise comparisons and
swapping

1 2 3 4 5 6

42 35 12 77 101 5

No need to swap
"Bubbling Up" the Largest Element
• Traverse a collection of elements
• Move from the front to the end
• “Bubble” the largest value to the end using pair-wise comparisons and
swapping

1 2 3 4 5 6

42 35 12 77 1015 Swap 101


5
"Bubbling Up" the Largest Element
• Traverse a collection of elements
• Move from the front to the end
• “Bubble” the largest value to the end using pair-wise comparisons and
swapping

1 2 3 4 5 6

42 35 12 77 5 101

Largest value correctly placed


Items of Interest
• Notice that only the largest value is correctly
placed
• All other values are still out of order
• So we need to repeat this process

1 2 3 4 5 6

42 35 12 77 5 101

Largest value correctly placed


“Bubbling” All the Elements
0 1 2 3 4 5
42 35 12 77 5 101

1 2 3 4 5 6
35 12 42 5 77 101

2 2 3 4 5 6
N-1

12 35 5 42 77 101

3 2 3 4 5 6
12 5 35 42 77 101

4 2 3 4 5 6
5 12 35 42 77 101
Bubble Sort Example
9, 6, 2, 12, 11, 9, 3, 7
the
6,
Bubblesort
exchanging
second
9,
Bubblesortcompares
when
and as
2,
comparesthe
necessary.
it is
12,
thenumbers
numbersin

larger
Here
they
the
are
11, 9, 3, 7
inpairs
pairsfrom
first
fromleft
leftto
number
exchanged.
is
toright
exchanging when necessary. Here the first number is comparedto
right
compared to
the second and as it is larger they are exchanged.
Now
6,
Nowthe
larger and 2,
thenext
nextpair
so
pairof
this 9,
ofnumbers
pair is 12,
numbersare
also 11, 9, 3, 7
arecompared.
compared. Again
exchanged.
larger and so this pair is also exchanged.
Againthethe99isisthe
the

In
6,
Inthe
thethird
exchange 2,
is made. 9,
thirdcomparison,
comparison,the
We 12,
the99isisnot
move on to 11, 9, 3, 7
notlarger
largerthan
compare
thanthe
the
the12
next
12so
sono
pair
exchange is made. We move on to compare the next pair without
any
no
without
anychange
changeto tothe
thelist.
list.
The
6,
The12 2,
12isislarger
largerthan9,
thanthe
the11 11,
11so
sothey
theyare 12, 9, 3, 7
areexchanged.
exchanged.

The
Theend
The

endof
6,
Thetwelve
ofthe
2,
twelveisisgreater
thelist
listhas
9,
greaterthan
hasbeen
thanthe
beenreached
11,
the99so
reachedso
sothey
sothis
9, 12, 3, 7
theyare
areexchanged
exchanged
thisisisthe
theend
endof ofthe
thefirst
firstpass.
pass. The
The

6, 2, 9, 11, 9, 3, 12, 7
twelve
twelveatatthe
theend
endof ofthe
thelist
listmust
mustbe belargest
largestnumber
numberin inthe
thelist
listand
andsosoisisnow
nowinin
the The
correct 12 is greater
position.
The 12 We
is greater than
now the 3
start so
a they
new are
pass exchanged.
from left to right.
the correct position. Wethan
now the 3 so
start theypass
a new are exchanged.
from left to right.

The
6,
The12 2,
12isisgreater 9,
greaterthan
thanthe 11,
the77so
sothey
theyare 9, 3, 7, 12
areexchanged.
exchanged.
Bubble Sort Example
First Pass
6, 2, 9, 11, 9, 3, 7, 12
Second Pass
6, 6,
2, 2, 9, 9,
11, 3,
11,
9, 7,
11,
3, 11,
7, 12
Notice
Noticethat
thatthis
thistime
timewe
wedodonot
nothave
havetotocompare
comparethe
thelast
lasttwo
two
numbers
numbers as we know the 12 is in position. This pass thereforeonly
as we know the 12 is in position. This pass therefore only
requires 6 comparisons.
requires 6 comparisons.
Bubble Sort Example
First Pass
6, 2, 9, 11, 9, 3, 7, 12
Second Pass

Third Pass
2, 6, 9, 9, 3, 7, 11, 12
2, 6, 9, 3,
9, 9,
7, 9,
3, 7, 11, 12
This
Thistime
timethe
the11
11and
and12
12are
arein
inposition.
position. This
Thispass
passtherefore
thereforeonly
only
requires
requires55comparisons.
comparisons.
Bubble Sort Example
First Pass
6, 2, 9, 11, 9, 3, 7, 12
Second Pass

Third Pass
2, 6, 9, 9, 3, 7, 11, 12
Fourth Pass
2, 6, 9, 3, 7, 9, 11, 12
2, 6, 3,
9, 9,
7, 9,
3, 7, 9, 11, 12
Each
Eachpass
passrequires
requiresfewer
fewercomparisons.
comparisons. This
Thistime
timeonly
only44are
areneeded.
needed.
Bubble Sort Example
First Pass
6, 2, 9, 11, 9, 3, 7, 12
Second Pass

Third Pass
2, 6, 9, 9, 3, 7, 11, 12
Fourth Pass
2, 6, 9, 3, 7, 9, 11, 12
Fifth Pass
2, 6, 3, 7, 9, 9, 11, 12
2, 6,
3, 3,
6, 7, 9, 9, 11, 12
The
Thelist
listisisnow
nowsorted
sortedbut
butthe
thealgorithm
algorithmdoes
doesnot
notknow
knowthis
thisuntil
untilitit
completes
completesaapasspasswith
withno
noexchanges.
exchanges.
Bubble Sort Example
First Pass
6, 2, 9, 11, 9, 3, 7, 12
Second Pass

Third Pass
2, 6, 9, 9, 3, 7, 11, 12
Fourth Pass
2, 6, 9, 3, 7, 9, 11, 12
Fifth Pass
2,
This
Thispass
6,
passno
3,
noexchanges
7,
exchangesarearemade
9,
madesosothe
9,
thealgorithm
11,
algorithmknows
knowsthe
12
thelist
listisis
sorted.
sorted. ItItcan
cantherefore
thereforesave
savetime
timeby
bynot
notdoing
doingthe
thefinal
finalpass.
pass. With
2, 3, 6, 7, 9, 9, 11, 12
With
other lists this check could save much more work.
other lists this check could save much more work.
Sixth Pass

2, 3, 6, 7, 9, 9, 11, 12
Bubble Sort Example
Quiz Time
1. Which number is definitely in its correct position at the
end of the first pass?
Answer: The last number must be the largest.
2. How does the number of comparisons required change as
the pass number increases?
Answer: Each pass requires one fewer comparison than the last.
3. How does the algorithm know when the list is sorted?
Answer: When a pass with no exchanges occurs.
4. What is the maximum number of comparisons required
for a list of 10 numbers?
Answer: 9 comparisons, then 8, 7, 6, 5, 4, 3, 2, 1 so total 45
Bubble Sort: Example

1 40 2 1 43 3 65 0 -1 58 3 42 4

2 2 1 40 3 43 0 -1 58 3 42 4 65

3 1 2 3 40 0 -1 43 3 42 4 58 65

4 1 2 3 0 -1 40 3 42 4 43 58 65

• Notice that at least one element will be in the correct


position each iteration.
Bubble Sort: Example
5 1 2 0 -1 3 3 40 4 42 43 58 65

6 1 0 -1 2 3 3 4 40 42 43 58 65

7 0 -1 1 2 3 3 4 40 42 43 58 65

8 -1 0 1 2 3 3 4 40 42 43 58 65
Bubble Sort
for (i=0; i<n-1; i++) {
for (j=0; j<n-1-i; j++)
/* compare the two neighbors */
if (a[j+1] < a[j]) {
/* swap a[j] and a[j+1] */
tmp = a[j];
a[j] = a[j+1];
a[j+1] = tmp;
}
}
Bubble Sort Code
void bubbleSort (int a[ ] , int size)
{
int i, j, temp;
for ( i = 0; i < size; i++ ) /* controls passes through the list */
{
for ( j = 0; j < size - 1; j++ ) /* performs adjacent comparisons */
{
if ( a[ j ] > a[ j+1 ] ) /* determines if a swap should occur */
{
temp = a[ j ]; /* swap is performed */
a[ j ] = a[ j + 1 ];
a[ j+1 ] = temp;
}
}
}
}

You might also like