0% found this document useful (0 votes)
46 views

Advanced Array Concepts - Bubble Sort

The document discusses sorting array elements using the bubble sort algorithm. It begins by defining sorting and ascending/descending order. It then explains that the bubble sort algorithm repeatedly compares and swaps adjacent elements, allowing smaller elements to "bubble" to the top of the array, eventually sorting it. The algorithm is demonstrated through a bubble sort code example. Finally, the document notes that bubble sort can also be used to sort arrays of objects by comparing and swapping based on a particular object field.

Uploaded by

giselejoyp
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)
46 views

Advanced Array Concepts - Bubble Sort

The document discusses sorting array elements using the bubble sort algorithm. It begins by defining sorting and ascending/descending order. It then explains that the bubble sort algorithm repeatedly compares and swaps adjacent elements, allowing smaller elements to "bubble" to the top of the array, eventually sorting it. The algorithm is demonstrated through a bubble sort code example. Finally, the document notes that bubble sort can also be used to sort arrays of objects by comparing and swapping based on a particular object field.

Uploaded by

giselejoyp
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/ 10

Advanced Array

Concepts
Objective
Sort array elements using the bubble
sort algorithm
- Sorting (ascending/descending)
- Sorting array of objects

2
Sorting Array Elements Using the
Bubble Sort Algorithm
• Sorting
– The process of arranging a series of objects in
some logical order
• Ascending order
– Begin with the object that has the lowest value
• Descending order
– Begin with the object that has the largest value

3
Using the Bubble Sort Algorithm
• An algorithm is a process or set of steps that solve
a problem.
• In the ascending bubble sort algorithm, you
repeatedly compare pairs of items, swapping
them if they are out of order, and eventually
creating a sorted list.
• The bubble sort is neither the fastest nor most
efficient sorting technique, but it is one of the
simplest to comprehend and provides deeper
understanding of array element manipulation. 4
Using the Bubble Sort Algorithm
• Bubble sort
–You continue to compare pairs of
items, swapping them if they are out of
order

–The smallest items “bubble” to the top


of the list, eventually creating a sorted
list
5
Using the Bubble Sort Algorithm
(cont’d.)
for (int a = 0; a < someNums.length - 1; ++a)
for (int b = 0; b < someNums.length - 1; ++b)
if (someNums[b] > someNums[b + 1]) {
int temp;
temp = someNums[b];
someNums[b] = someNums[b + 1];
someNums[b + 1] = temp;
}

Ascending bubble sort of the someNums array elements


6
Using the Bubble Sort Algorithm
(cont’d.)

7
Sorting Arrays of Objects
• You can sort arrays of objects in
much the same way that you sort
arrays of primitive types
–Major difference
• Make the comparison that determines
whether to swap two array elements
• Sort based on a particular object field

8
Sorting Arrays of Objects

9
Lab. Activities

10

You might also like