This document discusses bubble sort, an algorithm for sorting a list of elements in increasing order. Bubble sort works by iterating through the list n-1 times, where n is the number of elements. It compares adjacent elements and swaps them if they are in the wrong order. The algorithm makes n(n-1)/2 comparisons and about n(n-1)/4 assignments on average. For a list of 1000 elements, bubble sort would make 500,000 comparisons and 250,000 assignments.
This document discusses bubble sort, an algorithm for sorting a list of elements in increasing order. Bubble sort works by iterating through the list n-1 times, where n is the number of elements. It compares adjacent elements and swaps them if they are in the wrong order. The algorithm makes n(n-1)/2 comparisons and about n(n-1)/4 assignments on average. For a list of 1000 elements, bubble sort would make 500,000 comparisons and 250,000 assignments.
This document discusses bubble sort, an algorithm for sorting a list of elements in increasing order. Bubble sort works by iterating through the list n-1 times, where n is the number of elements. It compares adjacent elements and swaps them if they are in the wrong order. The algorithm makes n(n-1)/2 comparisons and about n(n-1)/4 assignments on average. For a list of 1000 elements, bubble sort would make 500,000 comparisons and 250,000 assignments.
This document discusses bubble sort, an algorithm for sorting a list of elements in increasing order. Bubble sort works by iterating through the list n-1 times, where n is the number of elements. It compares adjacent elements and swaps them if they are in the wrong order. The algorithm makes n(n-1)/2 comparisons and about n(n-1)/4 assignments on average. For a list of 1000 elements, bubble sort would make 500,000 comparisons and 250,000 assignments.
Chapter 14 Bubble Sort Objectives • Explore how to sort an array using bubble sort
Java Programming: From Problem Analysis to Program Design, 5e 2
Sorting a List • Bubble sort – Suppose list[0...n - 1] is a list of n elements, indexed 0 to n - 1 – We want to rearrange, that is, sort the elements of list in increasing order – The bubble sort algorithm works as follows: • In a series of n - 1 iterations, the successive elements list[index] and list[index + 1] of list are compared • If list[index] is greater than list[index + 1], then the elements list[index] and list[index + 1] are swapped, that is, interchanged
Java Programming: From Problem Analysis to Program Design, 5e 3
Bubble Sort
Java Programming: From Problem Analysis to Program Design, 5e 4
Bubble Sort (continued)
Java Programming: From Problem Analysis to Program Design, 5e 5
Bubble Sort (continued)
Java Programming: From Problem Analysis to Program Design, 5e 6
Bubble Sort (continued)
Java Programming: From Problem Analysis to Program Design, 5e 7
Bubble Sort (continued)
Java Programming: From Problem Analysis to Program Design, 5e 8
Bubble Sort (continued)
Java Programming: From Problem Analysis to Program Design, 5e 9
Bubble Sort (continued)
• It is known that for a list of length n, bubble sort
makes n(n – 1) / 2 key comparisons and on average about n(n – 1) / 4 item assignments • Therefore, if n = 1000, then to sort the list, bubble sort makes 500,000 key comparisons and about 250,000 item assignments
Java Programming: From Problem Analysis to Program Design, 5e 10