Bubble sort is a sorting algorithm that sequentially sorts a data array from smallest to largest or vice versa by iterating through the array multiple times and swapping adjacent elements that are out of order. Pseudocode is provided to represent the steps of a bubble sort algorithm, which includes initializing variables, using nested for loops to compare and swap array elements, and continuing iterations until no swaps are needed. The document also discusses common calculation functions like totaling, counting, and finding the maximum, minimum, and average values in an array. Examples are given for how to use a while loop to apply these calculations to the weights of people in an elevator.
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 ratings0% found this document useful (0 votes)
13 views10 pages
Bubble Sort and Solution Methods
Bubble sort is a sorting algorithm that sequentially sorts a data array from smallest to largest or vice versa by iterating through the array multiple times and swapping adjacent elements that are out of order. Pseudocode is provided to represent the steps of a bubble sort algorithm, which includes initializing variables, using nested for loops to compare and swap array elements, and continuing iterations until no swaps are needed. The document also discusses common calculation functions like totaling, counting, and finding the maximum, minimum, and average values in an array. Examples are given for how to use a while loop to apply these calculations to the weights of people in an elevator.
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/ 10
Topic
Bubble sort algorithms
Bubble sort • Take a look at a hard copy of a dictionary. Say you were looking for the word ‘process’. You would first flip through the contents until you reach the letter ‘p’, then look for words starting with ‘pr’, and then start visually searching until you spot the word ‘process’. Because the words are ordered alphabetically that makes it possible to locate words based on their starting characters. This process of ordering elements in a list is called sorting. • A bubble sort is a sorting algorithm used to sort a data array sequentially, from smallest to largest or largest to smallest. Pencils in a random order Pencils rearranged in size order • In the previous slide, each time you exchanged pencil positions you carried out a swap, and each time you went through the set of pencils from left to right you completed a pass. • Through the iteration of comparing and swapping through multiple passes, you will find that the largest pencils are pushed to the end of the line, and they start to arrange themselves sequentially so that they no longer require comparison. Pseudocode Representing a bubble sort DECLARE ArrayToSort: ARRAY [1: 10] OF INTEGER THEN DECLARE SizeOfArray: INTEGER temp ← ArraytoSort[j+1] DECLARE Swapped: BOOLEAN ArrayToSort [j+1] ← ArrayToSort[j] DECLARE i: INTEGER ArrayToSort [j] ← temp DECLARE j: INTEGER Swapped ← True DECLARE temp: INTEGER ENDIF SizeOfArray ← LENGTH(ArrayToSort) NEXT j FOR i ← 1 TO SizeOfArray IF (NOT Swapped) Swapped ← False THEN continue FOR j ← 1 to SizeOfArray ENDIF IF ArrayToSort [j] > ArraytoSort[j+1] NEXT i Performing calculations
• Just like there are standard algorithms, such as
the linear search and bubble sort, there are some standard calculation functions that come in handy while writing algorithmic solutions. Three such functions are: • Totalling • Counting • Finding the maximum, minimum and average value in a list Totalling
• An elevator can accommodate people until it
reaches a maximum load of 200kg. The range (kg) of a person is within 25kg – 60kg. Those in the elevator should be at least 7 people. Use the while loop to input weight of people into an array, sum these weights and stop the inputting of weight as soon as it reaches 200kg. Counting
• An elevator can accommodate people until it
reaches a maximum load of 200kg. The range (kg) of a person is within 25kg – 60kg. Those in the elevator should be at least 7 people. Use the while loop to input weight of people into an array, count those that are less that 40kg and output the numbers. Average
• An elevator can accommodate people until it
reaches a maximum load of 200kg. The range (kg) of a person is within 25kg – 60kg. Those in the elevator should be at least 7 people. Use the while loop to input weight of people into an array. Find the average weight of the first three people in the elevator. Maximum
• An elevator can accommodate people until it
reaches a maximum load of 200kg. The range (kg) of a person is within 25kg – 60kg. Those in the elevator should be at least 7 people. Use the while loop to input weight of people into an array. Find the person with the biggest weight in the elevator.
Get the entire Solution Manual for Introduction to Algorithms, third edition By Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest and Clifford Stein in PDF with one simple click.
Solution Manual for Introduction to Algorithms, third edition By Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest and Clifford Stein instant download
Solution Manual for Introduction to Algorithms, third edition By Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest and Clifford Stein - Read Now Or Download For A Complete Experience
Solution Manual for Introduction to Algorithms, third edition By Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest and Clifford Stein - Read Online Or Download Now
Solution Manual for Introduction to Algorithms, third edition By Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest and Clifford Stein instant download