Excel algorithm assignment sample

Bubble Sort Algorithm in Excel: Step-by-Step Worked Example

This sample shows how to complete a bubble sort algorithm assignment in Excel using 20 random numbers, a VBA macro, sorted output, a bar chart, and a short real-world explanation of how sorting algorithms can be used.

Bubble sort in Excel VBA macro code Random numbers 1 to 100 Bar chart example Written explanation

Use this as a learning example. If your course has a specific assignment guide, dataset, macro requirement, or chart format, adjust the work to match your instructor’s directions.

Video Walkthrough

Watch the walkthrough to see how the numbers are entered, how the VBA code runs, how the sorted result appears in Column A, and how the bar chart is inserted.

Assignment Requirements Covered in This Example

Random Number Setup

Column A uses 20 values between 1 and 100. In a blank workbook, these can be typed manually or generated with =RANDBETWEEN(1,100).

Bubble Sort Macro

The VBA macro compares neighboring cells in A1:A20 and swaps them when the upper number is larger than the lower number.

Lowest-to-Highest Output

After the macro finishes, Column A is arranged in ascending order, from the smallest number to the largest number.

Bar Chart

A bar chart is inserted from the sorted values so the final data column has a clear visual representation.

Corrected Bubble Sort VBA Code for A1:A20

The data range in this assignment is A1:A20, so the loop should start at row 1 and stop before comparing beyond row 20. Paste this code into a standard VBA module.

Sub BubbleSort()
    Dim i As Integer, j As Integer
    Dim Temp As Variant

    For i = 1 To 19
        For j = 1 To 20 - i
            If Cells(j, 1).Value > Cells(j + 1, 1).Value Then
                Temp = Cells(j, 1).Value
                Cells(j, 1).Value = Cells(j + 1, 1).Value
                Cells(j + 1, 1).Value = Temp
            End If
        Next j
    Next i

    MsgBox "Sort Complete!"
End Sub

Step-by-Step Excel Solution

  1. Open Microsoft Excel and create a new blank spreadsheet.
  2. In cells A1:A20, enter random numbers between 1 and 100. You can type the values manually or use =RANDBETWEEN(1,100).
  3. If you use RANDBETWEEN, copy cells A1:A20 and paste them as values so the numbers do not keep changing.
  4. Press Alt + F11 to open the Visual Basic for Applications editor.
  5. Click Insert, then choose Module.
  6. Paste the bubble sort VBA code into the module.
  7. Close the VBA editor and return to Excel.
  8. Press Alt + F8, select BubbleSort, and click Run.
  9. The macro compares neighboring values in Column A. If the upper value is larger than the value below it, the two values are swapped.
  10. When the macro finishes, Excel displays Sort Complete! and the numbers in A1:A20 are sorted from lowest to highest.
  11. Highlight the sorted numbers in A1:A20, go to Insert, and choose a bar chart or column chart.
  12. Add a clear chart title, such as Sorted Random Numbers, so the visual representation matches the assignment requirement.

Example Sorted Output

If the original random numbers were 72, 15, 94, 37, 8, 63, 21, 55, 89, 4, 46, 100, 31, 68, 12, 77, 25, 59, 41, 83, the sorted output would be:

Position Sorted Number Position Sorted Number
141155
281259
3121363
4151468
5211572
6251677
7311783
8371889
9411994
104620100

250-Word Real-World Application Explanation

Bubble sort is a simple sorting algorithm that compares two neighboring values and swaps them when they are in the wrong order. Although it is not the fastest sorting method for large datasets, it is useful for understanding how algorithms process data step by step. In a real-world example, bubble sort could be applied to organize a small list of customer order totals from lowest to highest. A small business owner might export daily sales values into Excel and use a sorting algorithm to arrange the amounts in ascending order. This would make it easier to identify the smallest and largest transactions, check for unusual values, and prepare the data for reporting.

For example, if a store has 20 customer purchases from one day, the bubble sort algorithm can compare each purchase amount with the next one. If a larger amount appears before a smaller amount, the two values switch places. After several passes through the list, the smallest values move toward the top and the largest values move toward the bottom. This organized list can then be displayed in a chart so the business owner can quickly see patterns in the sales data.

This type of algorithm is also useful in education because it demonstrates important programming ideas such as loops, comparisons, conditional statements, and variable swapping. Even though modern software uses more efficient sorting methods, bubble sort helps students understand the basic logic behind automated data organization.

Bubble Sort in Excel FAQs

Why does this VBA code use For i = 1 To 19?

There are 20 numbers, so after 19 passes the list will be sorted. The final value naturally falls into place after the repeated comparisons.

Why does the inner loop use For j = 1 To 20 - i?

The inner loop compares each value with the value directly below it. Using 20 - i prevents the macro from comparing beyond row 20.

Can I use Excel’s built-in Sort button instead?

For normal spreadsheet work, yes. For this assignment, the goal is to apply the bubble sort algorithm, so the macro demonstrates the algorithm logic instead of only using Excel’s built-in sorting tool.

Can Statskan customize this for my exact assignment?

Yes. Send the full instructions, required algorithm guide, spreadsheet requirements, chart requirements, due date, and any sample data so the support can be scoped correctly.

Need This Bubble Sort Excel Assignment Customized?

If your instructor requires a specific algorithm format, chart style, explanation length, or Excel file layout, send the instructions and get help matching the exact requirement.